Segment Tree Construction
Because the tree array does not need to construct this process, the construction of the segment tree is first
is to use recursion: First set left=1,right=n, then each recursion, left, mid and mid+1, right. The code is as follows:
void build(int left,int right,int index) { tree[index].left=left; tree[index].right=right; if(left==right) return ; int mid=(right+left)/2; build(left,mid,index*2); build(mid+1,right,index*2+1); }
``
Line segment Tree Single point modification
A single point of modification is to each node, see this node represents the range includes not including this point, including on the plus.
void my_plus(int index,int dis,int k) { tree[index].num+=k; if(tree[index].left==tree[index].right) return ; if(dis<=tree[index*2].right) my_plus(index*2,dis,k); if(dis>=tree[index*2+1].left) my_plus(index*2+1,dis,k); }
Tree-like array single-point modification
Here's a key thing, called Lowbit,lowbit, is to remove all the highs and lows of a binary number, leaving only the lowest bits of 1, such as Lowbit (5) =lowbit (0101 (binary)) =0001 (binary)
If you change the value of x, you should add your own lowbit, always add to n, these nodes are added, such as a total of 8 number 3rd number to add K, then c[0011]+=k;
C[0011+0001] (c[0100]) +=k;
C[0100+0100] (c[1000]) +=k;
This allows you to maintain a tree-like array
void add(int x,int k) { while(x<=n) { tree[x]+=k; x+=lowbit(x); } }
Segment Tree interval Query
The interval query is that there are three options for each interval found:
1, if the interval is completely included in the target range, then add the interval and then return;
2, if the interval of the right> target interval left, then query this interval;
3, if this interval of the left< target range right, also query this interval;
void search(int index,int l,int r) { if(tree[index].left>=l && tree[index].right<=r) { ans+=tree[index].num; return ; } if(tree[index*2].right>=l) search(index*2,l,r); if(tree[index*2+1].left<=r) search(index*2+1,l,r); }
Tree-like array interval query
is the prefix and, for example, a query of the X-to-y interval, then the and-from 1 to Y and-from 1 to X.
From 1 to Y the and the method is, the Y to 2, and then always minus lowbit (y), until 0
For example, 1 to 7 and
ans+=c[0111];ans+=c[0111-0001(0110)];ans+=c[0110-0010(0100)];ans+=c[0100-0100(c[0]无意义,结束)] int sum(int x) { int ans=0; while(x!=0) { ans+=tree[x]; x-=lowbit(x); } return ans; }
Segment Tree Interval modification
Similar to the segment tree interval query, it is divided into three kinds
1, if the current interval is exactly the interval to add, then this interval, that is, the node Plus, and then return;
2, if the interval of the right> target interval left, then query this interval;
3, if this interval of the left< target range right, also query this interval;
void pls(int index,int l,int r,int k) { if(tree[index].left>=l && tree[index].right<=r) { tree[index].num+=k; return ; } if(tree[index*2].right>=l) pls(index*2,l,r,k); if(tree[index*2+1].left<=r) pls(index*2+1,l,r,k); }
Tree-like array interval modification
It's going to be fun. If you add a k to the X-to-y interval, you add a k from X to N, and then from Y+1 to N plus a-K
The movement of the addition or the i+=lowbit (i);
void add(int x,int k) { while(x<=n) { tree[x]+=k; x+=lowbit(x); } }
Line segment Tree Single point query
is from the root node, always search to the target node, and then all along the way to add the better.
void search(int index,int dis) { ans+=tree[index].num; if(tree[index].left==tree[index].right) return ; if(dis<=tree[index*2].right) search(index*2,dis); if(dis>=tree[index*2+1].left) search(index*2+1,dis); }
Tree-like array single-point query
From X point, always X-=lowbit (x), plus along the way.
int search(int x) { int ans=0; while(x!=0) { ans+=tree[x]; x-=lowbit(x); } return ans; }
The comparison between segment tree and tree-like array