http://blog.csdn.net/libin56842/article/details/8530197
The basics can be seen in this article above
Style:
MAXN is the maximum range of topics, and the number of nodes to open 4 times times, exactly said ...
Lson and Rson identify the left child and right child of the node.
PUSHUP (int RT) is to update the information of the current node to the parent node.
Pushdown (int rt) Updates the current node information to the child's node.
RT represents the root (root) of the current subtree, which is the current node.
Thought:
For each non-leaf node labeled nodes [A, b], the range of the child's expression is [A, (A+B)/2], and the right child represents [(A+b)/2,b].
Structure:
Discretization and Segment trees:
Title: There are several segments on the x-axis to find the total length of the segment coverage.
General Solution: Set the coordinate range [Min,max], initialized to 0, and then each segment is stained to 1, the last statistic 1, applicable to the number of segments, the range is small.
Discretization of the solution: Discretization is a one by one mapping relationship, will be a large coordinate and small coordinates for one by one mapping, suitable for a small number of segments, large interval range.
For example: [10000,22000],[30300,55000],[44000,60000],[55000,60000].
First step: Sort 10000 22000 30300 44000 55000 60000
Part II: Ref. 1 2 3 4 5 6
The third part: Use the number to replace the original number, that is, the decimal generation large number.
[10000,22000]~[1,2]
[30300,55000]~[3,5]
[44000,60000]~[4,6]
[55000,60000]~[5,6]
Then use the decimal method to solve the common procedure, and finally replace the return.
The solution of Segment tree: Segment tree through the establishment of line segments, the original dyeing O (n) complexity reduced to log (n), applicable to the number of segments, the range of small interval.
Discrete segment tree: It is suitable for the case that the number of segments is large and the range of interval is big.
Structure:
Dynamic Data Structures:
struct node{
Node* left;
node* right;
......
}
Static global array emulation (full binary tree):
struct node{
int left;
int right;
......
}TREE[MAXN]
Http://www.xuebuyuan.com/1470670.html
Line segment trees are used in four main ways
Single-point update:
Template:
structnode{intL,r,c;} T[MAXN*4];voidPushup (intRT) {T[RT].C= t[rt<<1].C + t[(rt<<1)+1].c;}voidBuildintLintRintx) {T[X].L=l; T[X].R=R; T[X].C=0; if(L = = r)return; intMid = (l+r) >>1; Build (L,mid,x<<1); Build (Mid+1, R, (x<<1) +1);}voidUpdateintValintLintx) { if(T[X].L = = T[X].R && T[X].L = =l) {t[x].c+=Val; return; } intMid = (t[x].l + T[X].R) >>1; if(L >mid) {Update (val,l, (x<<1) +1); } Else{update (val,l,x<<1); } pushup (x);}intN,m,ans;voidQueryintLintRintx) { if(T[X].L = = L && T[X].R = =R) {ans+=t[x].c; return; } intMid = (t[x].l + T[X].R) >>1; if(L >mid) {query (L,r, (x<<1)+1); } Else if(r<=mid) {query (L,r, (x<<1)); } Else{query (L,mid, (x<<1)); Query (Mid+1, R, (x<<1)+1); }}
HDU 1166
#include <iostream>#include<string>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<algorithm>#include<stack>#include<queue>#include<cctype>#include<vector>#include<iterator>#include<Set>#include<map>#include<sstream>using namespacestd;#defineMem (A, B) memset (A,b,sizeof (a))#definePF printf#defineSF scanf#defineSPF sprintf#definePB Push_back#defineDebug printf ("!\n")#defineMAXN 55555#defineMAX (A, b) a>b?a:b#defineBlank pf ("\ n")#defineLL Long Long#defineAll (x) X.begin (), X.end ()#defineINS (x) Inserter (X,x.begin ())#definePqueue priority_queue#defineINF 0x3f3f3f3fstructnode{intL,r,c;} T[MAXN*4];voidPushup (intRT) {T[RT].C= t[rt<<1].C + t[(rt<<1)+1].c;}voidBuildintLintRintx) {T[X].L=l; T[X].R=R; T[X].C=0; if(L = = r)return; intMid = (l+r) >>1; Build (L,mid,x<<1); Build (Mid+1, R, (x<<1) +1);}voidUpdateintValintLintx) { if(T[X].L = = T[X].R && T[X].L = =l) {t[x].c+=Val; return; } intMid = (t[x].l + T[X].R) >>1; if(L >mid) {Update (val,l, (x<<1) +1); } Else{update (val,l,x<<1); } pushup (x);}intN,m,ans;voidQueryintLintRintx) { if(T[X].L = = L && T[X].R = =R) {ans+=t[x].c; return; } intMid = (t[x].l + T[X].R) >>1; if(L >mid) {query (L,r, (x<<1)+1); } Else if(r<=mid) {query (L,r, (x<<1)); } Else{query (L,mid, (x<<1)); Query (Mid+1, R, (x<<1)+1); }}intMain () {intT,i,kase=1; Chard[Ten]; SF ("%d",&t); while(t--) {mem (T,0); PF ("Case %d:\n", kase++); SF ("%d",&N); Build (1N1); for(i=1; i<=n;i++) { inttmp; SF ("%d",&tmp); Update (Tmp,i,1); } while(SF ("%s", d)! =EOF) { if(d[0] =='E') Break; intx, y; SF ("%d%d", &x, &y); if(d[0] =='Q') {ans=0; Query (x, Y,1); PF ("%d\n", ans); } if(d[0] =='S') Update (-Y,X,1); if(d[0] =='A') Update (Y,X,1); } } return 0;}
acm-segment Tree