1 struct node { 2 int l, r; 3 int lazy; 4 LL sum; 5 LL add; 6 }tree[MAXN]; 7 int a[100005]; 8 void build(int v, int l, int r) { 9 tree[v].l = l; 10 tree[v].r = r; 11 tree[v].lazy = 0; 12 tree[v].add = 0; 13 if(l == r) { 14 //tree[v].lazy = 1; 15 //tree[v].sum = a[l-1]; 16 return; 17 } 18 int mid = (l + r) / 2; 19 build(2*v, l, mid); 20 build(2*v+1, mid+1, r); 21 tree[v].sum = tree[2*v].sum + tree[2*v+1].sum; 22 } 23 void update(int v, int l, int r, LL a) { 24 if(tree[v].l == l && tree[v].r == r) { 25 tree[v].lazy = 1; 26 tree[v].add += a; 27 return ; 28 } 29 tree[v].sum += (r - l + 1) * a; 30 int mid = (tree[v].l + tree[v].r) / 2; 31 if(r <= mid) { 32 update(2*v, l ,r, a); 33 } else { 34 if(l > mid) { 35 update(2*v+1, l, r, a); 36 } else { 37 update(2*v, l, mid, a); 38 update(2*v+1, mid+1, r, a); 39 } 40 } 41 } 42 LL query(int v, int l, int r) { 43 LL ans = 0; 44 if(tree[v].lazy) { 45 ans += tree[v].add * (r - l + 1); 46 } 47 if(tree[v].l == l && tree[v].r == r) { 48 ans += tree[v].sum; 49 return ans; 50 } 51 int mid = (tree[v].l + tree[v].r) / 2; 52 if(r <= mid) { 53 return ans + query(2*v, l ,r); 54 } else { 55 if(l > mid) { 56 return ans + query(2*v+1, l, r); 57 } else { 58 return ans + query(2*v, l, mid) + query(2*v+1, mid+1, r); 59 } 60 } 61 }
时间: 2024-10-03 22:15:51