Segment Tree (interval overlay template)

Source: Internet
Author: User

Segment Tree

A segment tree is a binary search tree, similar to an interval tree, which divides an interval into a number of cell ranges, each of which corresponds to a leaf node in a segment tree.
For each non-leaf node in the segment tree [A, b], its left son represents a range of [A, (A+B)/2], and the right son represents a range of [(A+B)/2+1,b]. Therefore, the segment tree is a balanced binary tree, the last number of child nodes is N, that is, the length of the entire segment interval.
The segment tree allows you to quickly find the number of occurrences of a node in several segments, with a time complexity of O (Logn). The spatial complexity of the non-optimized is 2N, so it is sometimes necessary to make space compressed by discretization.

Title Link: http://acm.zzuli.edu.cn/zzuliacm/problem.php?id=1877

Code:

1#include <cstdio>2#include <cmath>3#include <cstring>4#include <string>5#include <algorithm>6#include <queue>7#include <stack>8#include <map>9#include <Set>Ten#include <vector> One#include <iostream> A using namespacestd; - #defineFor0 (i, n) for (int i=0; i< (n); ++i) - #defineFor1 (i,a,n) for (int i= (a); i<= (n); ++i) the #defineFor2 (i,a,n) for (int i= (a);i< (n); ++i) - #defineFor3 (i,a,n) for (int i= (a); i>= (n);-I.) - #defineFor4 (i,a,n) for (int i= (a);i> (n); - #defineCC (i,a) memset (i,a,sizeof (i)) + #defineLL Long Long - #defineMOD 1000000007 + #defineINF 0x3f3f3f3f A  at #defineEPS 1e-6 - #defineN 100010 - #defineLson p<<1 - #defineRson p<<1|1 -  - structNum in { -     intL,r; to }a[n]; + intV[n]; -  the structnode * { $     intl,r,x;Panax Notoginseng     intmid () { -         return(L+R)/2; the     } +     intLen () { A         return(r-l+1); the     } +}tree[n<<2]; -  $ voidBuildtree (intPintLintR) $ { -Tree[p].l=L; -Tree[p].r=R; thetree[p].x=0; -     if(l==R)Wuyi         return ; the  - Buildtree (Lson,l,tree[p].mid ()); WuBuildtree (Rson,tree[p].mid () +1, R); - } About  $ voidUpdateintPintLintR) - { -     if(Tree[p].l==l && tree[p].r==R) { -tree[p].x++; A         return ; +     } the     if(R <=Tree[p].mid ()) - Update (LSON,L,R); $     Else if(L >Tree[p].mid ()) the Update (RSON,L,R); the     Else{ the Update (Lson,l,tree[p].mid ()); theUpdate (Rson,tree[p].mid () +1, R); -     } in } the  the voidUpintPintLintR) About { the     if(l==R) the         return ; thetree[lson].x + =tree[p].x; +tree[rson].x + =tree[p].x; -  the Up (Lson,l,tree[p].mid ());BayiUp (Rson,tree[p].mid () +1, R); the  thetree[p].x =min (tree[lson].x,tree[rson].x); - } -  the intQueryintPintLintR) the { the     if(Tree[p].l==l && tree[p].r==R) the         returntree[p].x; -     if(R <=Tree[p].mid ()) the         returnquery (lson,l,r); the     Else if(L >Tree[p].mid ()) the         returnquery (rson,l,r);94     Else the         returnMin (Query (Lson,l,tree[p].mid ()), Query (Rson,tree[p].mid () +1, R)); the  the }98  About intMain () - {101     intT;102scanf"%d",&t);103      while(t--){104         intn,m,k=0, ans; thescanf"%d%d",&n,&m);106Memset (A,0,sizeof(a));107memset (V,0,sizeof(v));108Buildtree (1,1, n);109  the          for(intI=1; i<=m; i++){111scanf"%d%d",&a[i].l,&A[I].R); theUpdate1, A[I].L,A[I].R);113         } the  theUp1,1, n); the 117          for(intI=1; i<=m; i++){118Ans=query (1, A[I].L,A[I].R);119             if(Ans >=2) -v[k++] =i;121         }122printf"%d\n", k);123          for(intI=0; i<k; i++)124printf"%d%c", V[i], i==k-1?'\ n':' '); the     }126     return 0;127}

Additional version: Https://hrbust-acm-team.gitbooks.io/acm-book/content/data_structure/ds_part3.html

Segment Tree (interval overlay template)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.