Learning line Tree The next day, this problem belongs to the second simple line segment tree, the first simple is a single point update, this belongs to the interval update.
The interval update is lazy thought, I come to my shallow understanding to talk about lazy thought:
In the data structure, the tree structure can be stored linearly (linear table) or tree-like (linked list)
Tree shape
struct node{ int data; struct node* lchild; struct node* rchild;} Btree,*btree;
BTree = (BTree) malloc (BTree);
It's like this ... Most of the summer vacation past forget ... It doesn't matter ....
Then the order is stored in the order table, the first node of the left child node is i*2, right child node is i*2+1, this is a property.
This problem is the use of the tree structure of the order table.
There seems to be nothing to say, simple template problem, look at the code bar:
1#include <cstdio>2 #defineN 1000033 using namespacestd;4 structnod5 {6 intData,l,r,lazy;7}tree[4*N];8 9 voidPUSH_UP (inti)Ten { OneTree[i].data = tree[i*2].data+tree[i*2+1].data; A } - - voidBuild_tree (intIintLintR) the { -TREE[I].L =l; -TREE[I].R =R; -Tree[i].lazy =-1; + if(l==R) { -Tree[i].data =1; + return; A } at intMid = (R + L)/2; -Build_tree (i*2, l,mid); -Build_tree (i*2+1, mid+1, R); - push_up (i); - } - in voidPush_down (inti) - { totree[i*2].data = tree[i].lazy* (tree[i*2].r-tree[i*2].l+1); +tree[i*2+1].lazy = tree[i*2].lazy =Tree[i].lazy; -tree[i*2+1].data = tree[i].lazy* (tree[i*2+1].r-tree[i*2+1].l+1); theTree[i].lazy =-1; * } $ Panax Notoginseng voidUpdata (intIintVintLintR) - { the if(l<=tree[i].l&&tree[i].r<=R) + { ATree[i].data = (tree[i].r-tree[i].l+1)*v; theTree[i].lazy =v; + return; - } $ if(tree[i].lazy!=-1) Push_down (i); $ intMid = (TREE[I].L+TREE[I].R)/2; - if(l<=mid) -Updata (i*2, v,l,r); the if(R >mid) -Updata (i*2+1, v,l,r);Wuyi push_up (i); the } - Wu intQueryintIintLintR) - { About if(l<=tree[i].l&&tree[i].r<=R) $ { - returnTree[i].data; - } - if(tree[i].lazy!=-1) A Push_down (i); + intMid= (TREE[I].L+TREE[I].R)/2; the if(r<=mid) - returnQuery (i*2, l,r); $ if(l>=mid+1) the returnQuery (i*2+1, l,r); the returnQuery (i*2, L,r) +query (i*2+1, l,r); the } the - intMain () in { the intNoc,ug; thescanf"%d",&NOC); AboutUG =NOC; the while(noc--) the { the intn,q,x,y,z; +scanf"%d",&n); -Build_tree (1,1, n); thescanf"%d",&q);Bayi for(intI=1; i<=q;i++) the { thescanf"%d%d%d",&x,&y,&z); -Updata (1, z,x,y); - } theprintf"Case%d:the Total value of the hook is%d.\n", ug-noc,tree[1].data); the } the}
"Original" hdu1698 Just a Hook (segment tree → interval update, interval query)