B-Qiushi Big Brother and Flower
Line tree entry question, need to understand lazy thinking. Line tree This thing, to understand or not difficult, is the code implementation details. The slag has been written several times or can't remember.
DEBUG LIST (Reversed):
A) When reading the data is 0-based from a[0]~a[n-1] but is assigned in buildtree using 1-based tree[i].val = a[l] = A[r]
b) It is easy to get the wrong place, the data is the parameter l,r or the node l,r (TREE[IDX].L, TREE[IDX].R)
c) Lazy tag decomposition of the specific operation (the lazy tag meaning can be maintained after the interval sum recorded after the tag, only need to decompose to the sub-node, my is this, also can be not maintained this line of the tag, need to maintain the segment during decomposition): the tag pushdown to two sub-nodes, maintaining the sum of the sub-segments, maintaining the lazy tag of the sub-segments, and placing the segment lazy tag at 0
0) The update operation can be combined with the query operation
#include <iostream>#include<cstdio>#include<cstdlib>using namespacestd;Const intMAXN = 1e5 + -;structtree{intL, R; Long LongVal, lazy; voidPushdown (Long Longv) {Val+ = v* (r-l+1); Lazy+=v; }}tree[5*MAXN];intN, M;intA[MAXN];voidBuildtree (intLintRintidx) {//if (L > R) return 0;TREE[IDX].L =l; TREE[IDX].R=R; if(L = = r) Tree[idx].val =A[l]; Else { intMid = (L + r)/2; Buildtree (L, Mid, Idx*2); Buildtree (Mid+1, R, idx*2+1); Tree[idx].val= tree[idx*2].val + tree[idx*2+1].val; }}Long LongQueryintLintRLong LongVintidx) {//if (L > R) return 0; if(L = = Tree[idx].l && r = =TREE[IDX].R) {Tree[idx].lazy+=v; Tree[idx].val+ = v* (r-l+1); returnTree[idx].val; } Else { intMid = (TREE[IDX].L + TREE[IDX].R)/2;//A long long t = 0;Tree[idx].val + = (r-l+1)*v; if(Tree[idx].lazy! =0) {Tree[idx*2].pushdown (Tree[idx].lazy); Tree[idx*2+1].pushdown (Tree[idx].lazy); Tree[idx].lazy=0; } if(R <=mid)returnQuery (l, R, V, idx*2); Else if(L >= mid+1) returnQuery (l, R, V, idx*2+1); Else returnQuery (L, Mid, V, idx*2) + Query (mid+1, R, V, idx*2+1); }}intMain () {#ifdef LOCAL freopen ("b.in","R", stdin);#endifscanf ("%d", &N); for(inti =0; I < n; i++) scanf ("%d", A +i); scanf ("%d", &m); Buildtree (1N1); while(m--) { intL, R, V; scanf ("%d%d%d", &l, &r, &v); printf ("%lld\n", query (l, R, V,1)); } return 0;}
UESTC Training for Data structures