-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlazy_prop.cpp
98 lines (89 loc) · 1.96 KB
/
lazy_prop.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(ll i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define pb push_back
#define ll long long
#define mp make_pair
#define F first
#define S second
#define MAXN 100005
// sum of elements in a range, replace int by ll if req
int tree[4*MAXN];
int lazy[4*MAXN]={0};
int arr[5] = {5,2,3,1,4};
void build(int index, int l, int r){
if(l>r){
return;
}
if(l==r){
tree[index]=arr[l];
return;
}
int mid = (l+r)>>1;
build(2*index, l, mid);
build(2*index+1, mid+1, r);
tree[index]=tree[2*index]+tree[2*index+1];
}
void range_update(int index, int l, int r, int x, int y, int val){
if(lazy[index]!=0){
tree[index]+=(r-l+1)*lazy[index];
if(l!=r){
lazy[2*index]+=lazy[index];
lazy[2*index+1]+=lazy[index];
}
lazy[index]=0;
}
if(y<x || r<l || l>y || r<x){
return;
}
if(x<=l && y>=r){
tree[index]+=(r-l+1)*val;
if(l!=r){
lazy[2*index]+=val;
lazy[2*index+1]+=val;
}
return;
}
int mid = (l+r)>>1;
range_update(2*index, l, mid, x, y, val);
range_update(2*index+1, mid+1, r, x, y, val);
tree[index]=tree[2*index]+tree[2*index+1];
}
int query(int index, int x, int y, int l, int r){
// work on lazy stored values
if(lazy[index]!=0){
tree[index]+=(r-l+1)*lazy[index];
if(l!=r){
lazy[2*index]+=lazy[index];
lazy[2*index+1]+=lazy[index];
}
lazy[index]=0;
}
if(y<x || r<l || l>y || r<x){
return 0;
}
if(x<=l && y>=r){
return tree[index];
}
int mid = (l+r)>>1;
int z1 = query(2*index, x, y, l, mid);
int z2 = query(2*index+1, x, y, mid+1, r);
return z1+z2;
}
int main(){
// build segment tree
build(1, 0, 4);
int x=1, y=3, ans;
// query on range (1, 3)
ans = query(1, x, y, 0, 4);
cout<<ans<<endl;
// inc the elements in the range [2, 4] by 2
range_update(1, 0, 4, 2, 4, 2);
ans = query(1, x, y, 0, 4);
cout<<ans<<endl;
return 0;
}