-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLazySegmentTree.cpp
More file actions
178 lines (147 loc) · 6.07 KB
/
Copy pathLazySegmentTree.cpp
File metadata and controls
178 lines (147 loc) · 6.07 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
173
174
175
176
177
178
#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
#define loop(i,s,e) for(ll i = s; s<e ? i < e : i >= e; s<e ? i++ : i--)
#define forrr(i,s,e) for(ll i = s; i >= e; i--)
#define forr(i,s,e) for(ll i = s; i < e; i++)
#define vi vector<int>
#define vl vector<ll>
#define vvl vector<vl>
#define pll pair<ll, ll>
#define vp vector<pll>
#define ss second
#define ff first
#define pb push_back
#define pf push_front
#define all(a) a.begin(), a.end()
#define Rand(arr, n) generate_n(arr.begin(), n, random)
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define testt ll t = 1; cin>>t; while(t--)
#define getMax(x) max_element(x.begin(),x.end())
#define getMin(x) min_element(x.begin(),x.end())
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
using namespace std;
template<class T> void _print(T t) {cerr << t;}
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template<typename F, typename S> ostream& operator <<(ostream& ostream, pair<F, S>& p) { cout << p.first << " " << p.second<<" "; return ostream; }
template<typename T> ostream& operator <<(ostream& ostream, vector<T>& v) { for(auto& element : v) cout << element << " "; return ostream;}
template<typename T> ostream& operator <<(ostream& ostream, vector<vector<T>>& v) {for(auto& row : v){ for(auto& cell : row) cout << cell << " "; cout << "\n";} return ostream;}
template<typename F, typename S> istream& operator >>(istream& istream, pair<F, S>& p) { cin >> p.first >> p.second; return istream;}
template<typename T> istream& operator >>(istream& istream, vector<T>& v) { for(auto& element : v) cin >> element; return istream; }
//vl prime(1000001);
int random(int s, int e){return s + rand() % (e - s + 1);}
int flip(int n){ forr(i,0,31){ int num = 1<<i; if(num > n) break; n = n xor num;} return n; }
ll power(ll x, ll y){ ll res = 1; while (y > 0){ if (y & 1) res = (ll)(res*x); y = y>>1; x = (ll)(x*x); } return res; }
vp countArr(vl &arr, ll n){ vp v1; forr(i,0,n-1){ ll cnt = 1; while(i < n-1 && arr[i] == arr[i+1]){ cnt++, i++; } v1.pb({arr[i], cnt}); } if((n > 1 && arr[n-1] != arr[n-2]) || n == 1) v1.pb({arr[n-1], 1}); return v1;}
ll gcd(ll a, ll b){if(b == 0) return a; return gcd(b, a%b);};
vl factors(ll n){ vl fac; for (ll i = 1; i * i <= n; i++){ if (n % i == 0){ fac.pb(i); if (i * i != n)fac.pb(n / i); } } return fac; }
ll maxPow2(ll n){n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; return (n + 1);}
//vl sieve(){vl primes; for(ll i=0;i<1000001;i++) prime[i]=true; for(ll i=2;i<1000001;i++){ if(!prime[i]) continue; primes.pb(i); for(ll j=2;i*j<1000001;j++) prime[i*j]=false;} return primes;}
vp primeFactors(ll n){ vp v; for (ll j = 2; j <= sqrtl(n); j++){ ll cnt = 0; while(n%j == 0) n /= j, cnt++; if(cnt)v.pb({j,cnt}); } if(n!=1) v.pb({n,1}); return v;}
// ll dx[] = {0, 0, 1, 1, 1, -1, -1, -1};
// ll dy[] = {1, -1, 1, -1, 0, 1, -1, 0};
/*
add x in l r
divide by 2 in l r
*/
const int N = 1e5 + 2;
ll trees[N*4], parity[N*4];
vvl lazy(N*4, vl(3));
void build(int node, int st, int en, vl &arr){ //built in O(2n -1) time
if(st == en){
trees[node] = arr[st];
if(arr[st]%2) parity[node] += 1;
return;
}
int mid = (st+en)/2;
build(2*node, st, mid, arr);
build(2*node + 1, mid+1, en, arr);
trees[node] = trees[2*node] + trees[2*node + 1];
parity[node] = parity[2*node] + parity[2*node + 1];
}
void change(ll node){
lazy[node][1] += lazy[node/2][1];
lazy[node][1] %= 2;
if(lazy[node/2][2]){
lazy[node][1] += lazy[node][0];
lazy[node][1] %= 2;
lazy[node][0] = 0;
}
lazy[node][2] |= lazy[node/2][2];
lazy[node][0] += lazy[node/2][0];
}
void lazyUpdate(ll node, ll st, ll en){
if(lazy[node] == vl{0,0,0}) return;
if(lazy[node][1]) parity[node] = (en-st+1) - parity[node];
if(lazy[node][2]) trees[node] = parity[node];
trees[node] += (en-st+1)*lazy[node][0];
if(lazy[node][0]%2) parity[node] = (en-st+1) - parity[node];
if(st != en){
change(2*node);
change(2*node+1);
}
lazy[node] = vl{0,0,0};
}
ll query(int node, int st, int en, ll &l, ll &r){
lazyUpdate(node, st, en);
if(st > r || en < l)
return 0;
if(l <= st && r >= en) // l... st...en ...r
return trees[node];
int mid = (st + en)/2;
ll q1 = query(2*node, st, mid, l, r);
ll q2 = query(2*node + 1, mid+1, en, l, r);
trees[node] = trees[2*node] + trees[2*node + 1];
parity[node] = parity[2*node] + parity[2*node + 1];
return q1 + q2;
}
void update(int node, int st, int en, ll &l, ll &r, ll &val, ll &type){
lazyUpdate(node, st, en);
if(st > r || en < l) return;
if(l <= st && r >= en){
if(type == 1) lazy[node][2] = 1;
if(type == 2) lazy[node][0] += val;
lazyUpdate(node, st, en);
return;
}
int mid = (st + en)/2;
update(2*node, st, mid, l, r, val, type);
update(2*node + 1, mid+1, en, l, r, val, type);
trees[node] = trees[2*node] + trees[2*node + 1];
parity[node] = parity[2*node] + parity[2*node + 1];
}
void solve(){
ll n,q; cin>>n>>q;
vl arr(n); cin>>arr;
build(1,0,n-1,arr);
while(q--){
ll type; cin>>type;
ll l, r; cin>>l>>r;
l--, r--;
ll x = 0;
if(type == 3){
cout<<query(1, 0, n-1, l, r)<<endl;
}
else if(type == 2){
cin>>x;
update(1, 0, n-1, l, r, x, type);
}
else{
update(1,0,n-1,l,r,x,type);
}
}
}
int main(){
fastio(); srand(time(NULL));
// testt{ solve(); }
solve();
return 0;
}
/* use __lg(number) to get nearest power of 2 -> 8,9,10..15 returns 3, 16-31 returns 4*/