-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallsegment.cpp
286 lines (219 loc) · 5.85 KB
/
allsegment.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
278
279
280
281
282
283
///////////////////////////Normal/////////////////////////////////////////////
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]);
}
////////////////////////////////Lazy without propagation////////////////////////////
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];
}
///////////////////////////////Lazy With propagation///////////////////////////////
#include<bits/stdc++.h>
using namespace std;
#define MAX 101010
#define sf(a) scanf("%lld",&a)
#define pf(a) printf("%lld\n",a)
#define light(a) printf("Case %lld:\n",a)
#define li long long int
struct node
{
li sum;
}tree[4*MAX];
li lazy[4*MAX];
node merge(node a,node b)
{
node ret;
ret.sum=a.sum+b.sum;
return ret;
}
void lazyUpdate(li n,li st,li 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(li n,li st,li ed)
{
lazy[n]=0LL;
if(st==ed) {
tree[n].sum=0LL;
return;
}
li 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(li n,li st,li ed,li i,li j)
{
lazyUpdate(n,st,ed);
if(st>=i && ed<=j) return tree[n];
li 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(li n,li st,li ed,li i,li j,li v)
{
lazyUpdate(n,st,ed);
if(st>j || ed<i) return;
if(st>=i && ed<=j)
{
lazy[n]+=v;
lazyUpdate(n,st,ed);
return;
}
li 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]);
}
////////////////////////////segment tree(indexing)//////////////////////////////////
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200003;
int n, arr[MAX] ;
int tree[MAX*4];
vector<int> v;
void init(int node, int beg, int endd) {
if(beg == endd) {
tree[node] = 1;
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] = tree[left]+tree[right];
}
// beg, endd = tree range
// x, y = query range
int query(int node, int beg, int endd,int pos) {
if(beg == endd) return beg;
int left = node*2;
int right = node*2+1;
int mid = (beg+endd) / 2;
if(tree[left]>=pos) return query(left, beg, mid,pos);
else query(right, mid+1, endd ,pos-tree[left]);
}
void update(int node, int beg, int endd, int x, int val) {
if(beg == x && endd == x) {
tree[node] = 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] = tree[left] + tree[right];
}
int main()
{
int t,n,q,a,l,i,m,sizes;
char ch[3];
scanf("%d",&t);
for(int k=1;k<=t;k++)
{
init(1,1,MAX-1);
printf("Case %d:\n", k);
scanf("%d %d",&n,&q);
//memset(tree,0,sizeof(tree));
memset(arr,0,sizeof(arr));
//memset(tree,0,sizeof(tree));
v.clear();
v.push_back(1);
for(i=1;i<=n;i++)
{
scanf("%d",&a);
v.push_back(a);
}
for(i=0;i<q;i++){
scanf("%s %d",&ch,&a);
//cin>>ch>>a;
if(ch[0]=='c')
{
if(a>n)
{
printf("none\n");
}
else
{
l=query(1,1,MAX-1,a);
update(1,1,MAX-1,l,0);
printf("%d\n",v[l]);
n--;
}
}
else
{
v.push_back(a);
n++;
}
}
}
return 0;
}