-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegment.cpp
279 lines (226 loc) · 5.71 KB
/
segment.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include<bits/stdc++.h>
using namespace std;
#define MAX 10000
int ara[MAX];
struct node
{
int sum;
}tree[4*MAX];
int lazy[4*MAX];
node merge(node a,node b)
{
node ret;
ret.sum=a.sum+b.sum;
return ret;
}
void lazyUpdate(int n,int st,int ed)
{
if(lazy[n]==0) return;
tree[n].sum+=(ed-st+1)*lazy[n];
if(st!=ed)
{
lazy[2*n]+=lazy[n];
lazy[2*n+1]+=lazy[n];
}
lazy[n]=0;
}
void build(int n,int st,int ed)
{
lazy[n]=0;
if(st==ed) {
tree[n].sum=ara[st];return;
}
int mid=(st+ed)/2;
build(2*n,st,mid);
build(2*n+1,mid+1,ed);
tree[n]=merge(tree[2*n],tree[2*n+1]);
}
node query(int n,int st,int ed,int i,int j)
{
lazyUpdate(n,st,ed);
if(st>=i && ed<=j) return tree[n];
int mid=(st+ed)/2;
if(mid<i) return query(2*n+1,mid+1,ed,i,j);
else if(mid>=j) return query(2*n,st,mid,i,j);
else return merge(query(2*n+1,mid+1,ed,i,j),query(2*n,st,mid,i,j));
}
void update(int n,int st,int ed,int i,int j,int v)
{
lazyUpdate(n,st,ed);
if(st>j || ed<i) return;
if(st>=i && ed<=j)
{
lazy[n]+=v;
lazyUpdate(n,st,ed);
return;
}
int mid=(st+ed)/2;
update(2*n,st,mid,i,j,v);
update(2*n+1,mid+1,ed,i,j,v);
tree[n]=merge(tree[2*n],tree[2*n+1]);
}
int main()
{
int i,n,q,c,l,r,v;
cin>>n>>q;
for(i=1;i<=n;i++) cin>>ara[i];
build(1,1,n);
for(i=0;i<q;i++)
{
cin>>c;
if(c==1)
{
cin>>l>>r>>v;
update(1,1,n,l,r,v);
}
else
{
cin>>l>>r;
node x=query(1,1,n,l,r);
cout<<x.sum<<endl;
}
}
}
struct data {
int minimum, maximum;
} tree[MAX*4];
data Merge(data l, data r) {
data ret;
ret.minimum = min(l.minimum, r.minimum);
ret.maximum = max(l.maximum, r.maximum);
return ret;
}
void init(int node, int beg, int endd) {
if(beg == endd) {
tree[node] = { arr[beg], arr[beg] };
return;
}
int left = node*2;
int right = node*2+1;
int mid = (beg+endd) / 2;
init(left, beg, mid);
init(right, mid+1, endd);
tree[node] = Merge(tree[left], tree[right]);
}
// beg, endd = tree range
// x, y = query range
data query(int node, int beg, int endd, int x, int y) {
if(x > y) return { INT_MAX, INT_MIN }; // dummy
if(beg == x && endd == y) return tree[node];
int left = node*2;
int right = node*2+1;
int mid = (beg+endd) / 2;
data l = query(left, beg, mid, x, min(y, mid));
data r = query(right, mid+1, endd, max(x, mid+1), y);
return Merge(l, r);
}
void update(int node, int beg, int endd, int x, int val) {
if(beg == x && endd == x) {
tree[node] = { val, val };
return;
}
int left = node*2;
int right = node*2+1;
int mid = (beg+endd) / 2;
if(x <= mid) update(left, beg, mid, x, val);
else update(right, mid+1, endd, x, val);
tree[node] = Merge(tree[left], tree[right]);
}
void update(int at,int L,int R,int l,int r)
{
if(r<L || R<l) return;
if(l<=L && R<=r) { toggle[at]^=1; return; }
int mid=(L+R)/2;
update(at*2,L,mid,l,r);
update(at*2+1,mid+1,R,l,r);
}
int query(int at,int L,int R,int pos)
{
//cout<<"loop"<<endl;
if(pos<L || R<pos) return 0;
if(L==pos && pos==R) {
return toggle[at];}
int mid=(L+R)/2;
//cout<<toggle[at]<<endl;
if(pos<=mid) return query(at*2,L,mid,pos)^toggle[at];
else return query(at*2+1,mid+1,R,pos)^toggle[at];
}
Code: (Toggle Highlighting) (Open in Compiler)
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100003;
//int n=7, arr[] = { 0, 2, 3, 7, 8, 6, 5, 11 };
int n,arr[MAX*4];
struct data {
int minimum, maximum;
} tree[MAX*4];
data Merge(data l, data r) {
data ret;
ret.minimum = min(l.minimum, r.minimum);
ret.maximum = max(l.maximum, r.maximum);
return ret;
}
void init(int node, int beg, int endd) {
if(beg == endd) {
tree[node] = { arr[beg], arr[beg] };
return;
}
int left = node*2;
int right = node*2+1;
int mid = (beg+endd) / 2;
init(left, beg, mid);
init(right, mid+1, endd);
tree[node] = Merge(tree[left], tree[right]);
}
// beg, endd = tree range
// x, y = query range
data query(int node, int beg, int endd, int x, int y) {
if(x > y) return { INT_MAX, INT_MIN }; // dummy
if(beg == x && endd == y) return tree[node];
int left = node*2;
int right = node*2+1;
int mid = (beg+endd) / 2;
data l = query(left, beg, mid, x, min(y, mid));
data r = query(right, mid+1, endd, max(x, mid+1), y);
return Merge(l, r);
}
void update(int node, int beg, int endd, int x, int val) {
if(beg == x && endd == x) {
tree[node] = { val, val };
return;
}
int left = node*2;
int right = node*2+1;
int mid = (beg+endd) / 2;
if(x <= mid) update(left, beg, mid, x, val);
else update(right, mid+1, endd, x, val);
tree[node] = Merge(tree[left], tree[right]);
}
int main() {
/*init(1, 1, n);
data d = query(1, 1, n, 1, 6);
cout << d.minimum << " " << d.maximum << endl;
update(1, 1, n, 4, 11);
d = query(1, 1, n, 2, 6);
cout << d.minimum << " " << d.maximum << endl;*/
int t,d,i,a,b;
scanf("%d",&t);
for(int k=1;k<=t;k++)
{
int ans=0;
scanf("%d %d",&n,&d);
for(i=1;i<=n;i++)
{
scanf("%d",&arr[i]);
}
init(1,1,n);
printf("Case %d:\n",k);
for(i=1;i<=d;i++)
{
scanf("%d %d",&a,&b);
data df = query(1, 1, n,a,b);
printf("%d\n",df.minimum);
}
}
return 0;
}