-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseg_tree.cpp
More file actions
172 lines (149 loc) · 3.47 KB
/
seg_tree.cpp
File metadata and controls
172 lines (149 loc) · 3.47 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// 0-based indexed segment tree , with last element of range of element included
#define var ll
// struct var{
// //var as per needed in question (var present in the node of the segment tree)
// ll x;
// };
struct seg_tree{
ll size;
vector<var> a;
vector<ll> lazy;
vector<bool> clazy;
//declare invariant for calc
var invariant = INF;
void init(ll n){
size = 1;
while(size < n) size*=2;
a.assign(2*size , INF);
lazy.assign(2*size ,0);
clazy.assign(2*size , false);
}
var merge( var b , var c){
//merge nodes of the seg_tree
// minimum:var a = min(b , c);
return a;
}
// apply operation defines what we are doing in range update , for adding a+=operation(a,b); , assignment: a = operation(a,b);
void apply_operation(ll &a , ll b){
//addition:a +=b;
//assignment: a=b;
}
void propagate(ll node , ll lx , ll rx){
apply_operation(a[node] , lazy[node]);
if(lx != rx){
if(lazy[node]){
apply_operation(lazy[2*node+1] , lazy[node]);
apply_operation(lazy[2*node+2] , lazy[node]);
clazy[2*node+1] = true;
clazy[2*node+2] = true;
}
}
lazy[node] = 0;
clazy[node]=false;
}
void build( vll &arr , ll l , ll r , ll node){
if(l == r){
if(l < (ll)arr.size()){
//set value;
a[node] = arr[l];
}
return;
}
ll mid = (l+r)/2;
build(arr , l , mid , 2*node+1 );
build(arr , mid+1 , r , 2*node+2);
a[node] = merge( a[2*node+1], a[2*node+2]);
}
void modify(ll l , ll r , ll v , ll node , ll lx , ll rx){
if(clazy[node]){
propagate(node , lx , rx);
}
if(lx > r || l > rx) return;
if(lx >= l && rx <= r){
//addition:lazy[node]+=v
//assignment:lazy[node]=v;
lazy[node]+=v;
clazy[node] = true;
propagate(node , lx , rx);
return;
}
ll mid= (lx + rx)/2;
modify(l , r , v , 2*node+1 , lx , mid);
modify(l , r ,v , 2*node+2 , mid+1 , rx);
a[node] = merge( a[2*node+1] , a[2*node+2]);
}
var get(ll i , ll node, ll lx , ll rx){
if(clazy[node]){
propagate(node , lx , rx);
}
if(rx == lx){
return a[node];
}
ll mid = (lx+rx)/2;
ll res;
if(i <= mid){
return get(i , 2*node+1 , lx , mid);
}else{
return get(i , 2*node+2 , mid+1 , rx);
}
}
void set(ll l , ll r , ll v , ll node , ll pos ){
if(clazy[node]){
propagate(node , l , r);
}
if(l == r){
//set value
//addition:lazy[node]+=v
//assignment:lazy[node]=v;
clazy[node] = 1;
lazy[node] += v;
propagate(node , l , r);
return;
}
ll mid = (l+r)/2;
if( pos <= mid){
set(l , mid , v , 2*node+1 , pos);
}else{
set(mid+1 , r , v , 2*node+2 , pos);
}
a[node]= merge(a[2*node+1], a[2*node+2]);
}
var calc(ll l , ll r , ll lx , ll rx , ll node){
if(clazy[node]){
propagate(node , lx , rx);
}
if(r < lx || l > rx){
return INF;
}
if( l <= lx && r >= rx){
return a[node];
}
ll mid = (lx+rx)/2;
var sum1 = calc(l , r , lx , mid , 2*node+1 );
var sum2 = calc(l , r , mid+1 , rx , 2*node+2);
return merge(sum1 , sum2);
}
//0 BASED INDEXED , QUERY, AND STUFF WILL BE FROM 0 to n-1, if q is 1 to n, then l--, r--, REMEMBER U DUMB
void build( vll &arr ){
build(arr , 0 , size-1 , 0);
}
var calc(ll l , ll r){
var ans = calc(l , r , 0 , size-1 , 0);
return ans;
}
void set(ll i , ll v){
set(0 , size-1 , v, 0 , i);
}
void modify(ll l , ll r , ll v){
modify(l , r ,v , 0 , 0 , size-1);
}
var get(ll i){
return get(i , 0 , 0, size-1);
}
void prints(void){
cout << size << endl;
printv(a);
printv(lazy);
printv(clazy);
}
};