-
Notifications
You must be signed in to change notification settings - Fork 2
/
SegTREE.cpp
209 lines (193 loc) · 5.51 KB
/
SegTREE.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define si(x) scanf("%d", &x)
#define sc(x) scanf("%c", &x)
#define sl(x) scanf("%lld", &x)
#define pl(x) printf("%lld\n", x)
#define pi(x) printf("%d\n", x)
#define setbits __builtin_popcountll
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define speed ios::sync_with_stdio(false)
// SEGMENT TREE - Range Minimum Query, Range Maximum Query and Range Sum Query
struct newtype{
ll min;
ll max;
ll sum;
};
typedef struct newtype node;
ll arr[100005];
node segTree[4 * 100005];
void buildST(int left, int right, int index){
if(left == right){
segTree[index].min = arr[left];
segTree[index].max = arr[left];
segTree[index].sum = arr[left];
}
else{
int mid = (left + right) / 2;
buildST(left, mid, 2 * index);
buildST(mid + 1, right, (2 * index) + 1);
segTree[index].max = max(segTree[2 * index].max, segTree[(2 * index) + 1].max);
segTree[index].min = min(segTree[2 * index].min, segTree[(2 * index) + 1].min);
segTree[index].sum = segTree[2 * index].sum + segTree[(2 * index) + 1].sum;
}
}
void updateST(int left, int right, int index, int upindex, ll upvalue){
if(left == right){
segTree[index].min = upvalue;
segTree[index].max = upvalue;
segTree[index].sum = upvalue;
}
else{
int mid = (left + right) / 2;
if(upindex >= left && upindex <= mid){
updateST(left, mid, 2 * index, upindex, upvalue);
}
else{
updateST(mid + 1, right, (2 * index) + 1, upindex, upvalue);
}
segTree[index].max = max(segTree[2 * index].max, segTree[(2 * index) + 1].max);
segTree[index].min = min(segTree[2 * index].min, segTree[(2 * index) + 1].min);
segTree[index].sum = segTree[2 * index].sum + segTree[(2 * index) + 1].sum;
}
}
ll querymaxST(int qleft, int qright, int left, int right, int index){
if(qleft > right || qright < left || qright < qleft){
return INT_MIN;
}
else if(qleft <= left && qright >= right){
return segTree[index].max;
}
else{
int mid = (left + right) / 2;
ll p = querymaxST(qleft, qright, left, mid, 2 * index);
ll q = querymaxST(qleft, qright, mid + 1, right, (2 * index) + 1);
return max(p, q);
}
}
ll queryminST(int qleft, int qright, int left, int right, int index){
if(qleft > right || qright < left || qright < qleft){
return INT_MAX;
}
else if(qleft <= left && qright >= right){
return segTree[index].min;
}
else{
int mid = (left + right) / 2;
ll p = queryminST(qleft, qright, left, mid, 2 * index);
ll q = queryminST(qleft, qright, mid + 1, right, (2 * index) + 1);
return min(p, q);
}
}
ll querysumST(int qleft, int qright, int left, int right, int index){
if(qleft > right || qright < left || qright < qleft){
return 0;
}
else if(qleft <= left && qright >= right){
return segTree[index].sum;
}
else{
int mid = (left + right) / 2;
ll p = querysumST(qleft, qright, left, mid, 2 * index);
ll q = querysumST(qleft, qright, mid + 1, right, (2 * index) + 1);
return p + q;
}
}
// SEGMENT TREE - LAZY PROPAGATION - NUMBER OF EVEN AND ODD NUMBERS IN A RANGE
struct newtype{
ll even;
ll odd;
};
typedef struct newtype node;
ll arr[100005];
node segTree[4 * 100005];
ll lazy[4 * 100005];
void buildST(int left, int right, int index){
if(left == right){
if(arr[left % 2] == 0){
segTree[index].even = 1;
segTree[index].odd = 0;
}
else{
segTree[index].odd = 1;
segTree[index].even = 0;
}
}
else{
ll mid = (left + right) / 2;
buildST(left, mid, 2 * index);
buildST(mid + 1, right, (2 * index) + 1);
segTree[index].even = segTree[2 * index].even + segTree[(2 * index) + 1].even;
segTree[index].odd = segTree[2 * index].odd + segTree[(2 * index) + 1].odd;
}
}
void updateST(int left, int right, int index, int a, int b, ll value){
if(lazy[index] != 0){ // node needs an update
if(lazy[index] % 2 == 1){
ll temp = segTree[index].even;
segTree[index].even = segTree[index].odd;
segTree[index].odd = temp;
}
if(left != right){
lazy[2 * index] += lazy[index];
lazy[(2 * index) + 1] += lazy[index];
lazy[2 * index] %= 2;
lazy[(2 * index) + 1] %= 2;
}
lazy[index] = 0;
}
if(left > right || b < left || right < a){
return;
}
if(a <= left && right <= b){
if(value % 2 == 1){
ll temp = segTree[index].even;
segTree[index].even = segTree[index].odd;
segTree[index].odd = temp;
}
if(left != right){
lazy[2 * index] += value;
lazy[(2 * index) + 1] += value;
lazy[2 * index] %= 2;
lazy[(2 * index) + 1] %= 2;
}
return;
}
ll mid = (left + right) / 2;
updateST(left, mid, 2 * index, a, b, value);
updateST(mid + 1, right, (2 * index) + 1, a, b, value);
segTree[index].even = segTree[2 * index].even + segTree[(2 * index) + 1].even;
segTree[index].odd = segTree[2 * index].odd + segTree[(2 * index) + 1].odd;
}
node queryST(int qleft, int qright, int left, int right, int index){
if(left > right || qright < left || right < qleft){
node R; R.even = 0; R.odd = 0; return R;
}
if(lazy[index] != 0){ // node needs an update
if(lazy[index] % 2 == 1){
ll temp = segTree[index].even;
segTree[index].even = segTree[index].odd;
segTree[index].odd = temp;
}
if(left != right){
lazy[2 * index] += lazy[index];
lazy[(2 * index) + 1] += lazy[index];
lazy[2 * index] %= 2;
lazy[(2 * index) + 1] %= 2;
}
lazy[index] = 0;
}
if(qleft <= left && right <= qright){
return segTree[index];
}
ll mid = (left + right) / 2;
node L = queryST(qleft, qright, left, mid, 2 * index);
node R = queryST(qleft, qright, mid + 1, right, (2 * index) + 1);
node A;
A.even = L.even + R.even;
A.odd = L.odd + R.odd;
return A;
}