#include<bits/stdc++.h>
using namespace std;
const long long inf = (int) 1e9 + 10;
// #define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 2e5+10;
const int maxq = 1e6+10;
int n, q, tra[4*maxn], trsz[4*maxn], a[maxn], pos[maxn], sz[maxn];
vector<pair<int,int>> qrys[maxn];
int ans[maxq];
void builda(int no, int l, int r) {
if(l == r) {
tra[no] = a[l];
return;
}
int lc=2*no,rc=2*no+1,mid=(l+r)>>1;
builda(lc,l,mid);
builda(rc,mid+1,r);
tra[no] = max(tra[lc],tra[rc]);
}
int qrya(int no, int l, int r, int L, int R) {
if(l > R || r < L) return 0;
if(l >= L && r <= R) return tra[no];
int lc=2*no,rc=2*no+1,mid=(l+r)>>1;
return max(qrya(lc,l,mid,L,R),qrya(rc,mid+1,r,L,R));
}
void updsz(int no, int l, int r, int pos, int val) {
if(l > pos || r < pos) return;
if(l == r) {
trsz[no] = val;
return;
}
int lc=2*no,rc=2*no+1,mid=(l+r)>>1;
updsz(lc,l,mid,pos,val);
updsz(rc,mid+1,r,pos,val);
trsz[no] = trsz[lc]+trsz[rc];
}
int qrysz(int no, int l, int r, int L, int R) {
if(l > R || r < L) return 0;
if(l >= L && r <= R) return trsz[no];
int lc=2*no,rc=2*no+1,mid=(l+r)>>1;
return qrysz(lc,l,mid,L,R)+qrysz(rc,mid+1,r,L,R);
}
void adjust(int x) {
int l = pos[x];
int r = pos[x]+sz[x]-1;
auto mx = qrya(1,1,n,l,r);
if(mx == x) return;
sz[x] = pos[mx]-pos[x];
updsz(1,1,n,x,sz[x]);
sz[mx] = r-pos[mx]+1;
updsz(1,1,n,mx,sz[mx]);
adjust(x);
}
//last one with sum <= n/2
void cut(int no, int l, int r, int val) {
if(l == r) {
if(val == sz[l]) return;
int x = a[pos[l]+val];
sz[x] = pos[l]+sz[l]-1-pos[x]+1;
updsz(1,1,n,x,sz[x]);
sz[l] = val;
updsz(1,1,n,l,sz[l]);
adjust(x);
return;
}
int lc=2*no,rc=2*no+1,mid=(l+r)>>1;
if(val-trsz[lc] <= 0) cut(lc,l,mid,val);
else cut(rc,mid+1,r,val-trsz[lc]);
}
int find(int no, int l, int r, int val) {
if(l == r) {
return a[pos[l]+val-1];
}
int lc=2*no,rc=2*no+1,mid=(l+r)>>1;
if(val-trsz[lc] <= 0) return find(lc,l,mid,val);
else return find(rc,mid+1,r,val-trsz[lc]);
}
void solve() {
cin >> n >> q;
for(int i = 1; i <= n; i++) {
cin >> a[i];
pos[a[i]] = i;
}
builda(1,1,n);
sz[a[1]] = n;
updsz(1,1,n,a[1],sz[a[1]]);
adjust(a[1]);
for(int i = 1; i <= q; i++) {
int t,j; cin >> t >> j;
if(t > n) t = n;
qrys[t].pb(mp(j,i));
}
for(int i = 0; i <= n; i++) {
for(auto X : qrys[i]) {
int id = X.sc;
int j = X.fr;
ans[id] = find(1,1,n,j);
}
cut(1,1,n,n/2);
}
for(int i = 1; i <= q; i++) {
cout << ans[i] << endl;
}
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
243 ms |
28596 KB |
Output is correct |
2 |
Correct |
317 ms |
27892 KB |
Output is correct |
3 |
Correct |
246 ms |
26884 KB |
Output is correct |
4 |
Correct |
207 ms |
27496 KB |
Output is correct |
5 |
Correct |
255 ms |
29552 KB |
Output is correct |
6 |
Correct |
205 ms |
29768 KB |
Output is correct |
7 |
Correct |
246 ms |
29644 KB |
Output is correct |
8 |
Correct |
226 ms |
28024 KB |
Output is correct |
9 |
Correct |
217 ms |
27140 KB |
Output is correct |
10 |
Correct |
217 ms |
26932 KB |
Output is correct |
11 |
Correct |
220 ms |
26924 KB |
Output is correct |
12 |
Correct |
211 ms |
24696 KB |
Output is correct |
13 |
Correct |
250 ms |
25520 KB |
Output is correct |
14 |
Correct |
227 ms |
29436 KB |
Output is correct |
15 |
Correct |
272 ms |
27612 KB |
Output is correct |
16 |
Correct |
3 ms |
5032 KB |
Output is correct |
17 |
Correct |
252 ms |
25668 KB |
Output is correct |
18 |
Correct |
208 ms |
25508 KB |
Output is correct |
19 |
Correct |
3 ms |
5032 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
383 ms |
43596 KB |
Output is correct |
2 |
Correct |
382 ms |
42628 KB |
Output is correct |
3 |
Correct |
324 ms |
36308 KB |
Output is correct |
4 |
Correct |
273 ms |
34304 KB |
Output is correct |
5 |
Correct |
308 ms |
34988 KB |
Output is correct |
6 |
Correct |
271 ms |
35436 KB |
Output is correct |
7 |
Correct |
325 ms |
38892 KB |
Output is correct |
8 |
Correct |
308 ms |
39760 KB |
Output is correct |
9 |
Correct |
278 ms |
34676 KB |
Output is correct |
10 |
Correct |
286 ms |
38400 KB |
Output is correct |
11 |
Correct |
256 ms |
32684 KB |
Output is correct |
12 |
Correct |
257 ms |
32600 KB |
Output is correct |
13 |
Correct |
287 ms |
38020 KB |
Output is correct |
14 |
Correct |
247 ms |
35744 KB |
Output is correct |
15 |
Correct |
307 ms |
40832 KB |
Output is correct |
16 |
Correct |
26 ms |
12364 KB |
Output is correct |
17 |
Correct |
335 ms |
36188 KB |
Output is correct |
18 |
Correct |
204 ms |
28276 KB |
Output is correct |
19 |
Correct |
77 ms |
17044 KB |
Output is correct |
20 |
Correct |
112 ms |
18268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
13092 KB |
Output is correct |
2 |
Correct |
88 ms |
12184 KB |
Output is correct |
3 |
Correct |
97 ms |
12616 KB |
Output is correct |
4 |
Correct |
51 ms |
11816 KB |
Output is correct |
5 |
Correct |
74 ms |
11952 KB |
Output is correct |
6 |
Correct |
58 ms |
11972 KB |
Output is correct |
7 |
Correct |
67 ms |
11776 KB |
Output is correct |
8 |
Correct |
72 ms |
11688 KB |
Output is correct |
9 |
Correct |
67 ms |
12224 KB |
Output is correct |
10 |
Correct |
43 ms |
11852 KB |
Output is correct |
11 |
Correct |
49 ms |
11560 KB |
Output is correct |
12 |
Correct |
42 ms |
11744 KB |
Output is correct |
13 |
Correct |
51 ms |
11724 KB |
Output is correct |
14 |
Correct |
46 ms |
11976 KB |
Output is correct |
15 |
Correct |
42 ms |
11188 KB |
Output is correct |
16 |
Correct |
17 ms |
8532 KB |
Output is correct |
17 |
Correct |
76 ms |
10968 KB |
Output is correct |
18 |
Correct |
39 ms |
9732 KB |
Output is correct |
19 |
Correct |
3 ms |
5032 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
243 ms |
28596 KB |
Output is correct |
2 |
Correct |
317 ms |
27892 KB |
Output is correct |
3 |
Correct |
246 ms |
26884 KB |
Output is correct |
4 |
Correct |
207 ms |
27496 KB |
Output is correct |
5 |
Correct |
255 ms |
29552 KB |
Output is correct |
6 |
Correct |
205 ms |
29768 KB |
Output is correct |
7 |
Correct |
246 ms |
29644 KB |
Output is correct |
8 |
Correct |
226 ms |
28024 KB |
Output is correct |
9 |
Correct |
217 ms |
27140 KB |
Output is correct |
10 |
Correct |
217 ms |
26932 KB |
Output is correct |
11 |
Correct |
220 ms |
26924 KB |
Output is correct |
12 |
Correct |
211 ms |
24696 KB |
Output is correct |
13 |
Correct |
250 ms |
25520 KB |
Output is correct |
14 |
Correct |
227 ms |
29436 KB |
Output is correct |
15 |
Correct |
272 ms |
27612 KB |
Output is correct |
16 |
Correct |
3 ms |
5032 KB |
Output is correct |
17 |
Correct |
252 ms |
25668 KB |
Output is correct |
18 |
Correct |
208 ms |
25508 KB |
Output is correct |
19 |
Correct |
3 ms |
5032 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
383 ms |
43596 KB |
Output is correct |
22 |
Correct |
382 ms |
42628 KB |
Output is correct |
23 |
Correct |
324 ms |
36308 KB |
Output is correct |
24 |
Correct |
273 ms |
34304 KB |
Output is correct |
25 |
Correct |
308 ms |
34988 KB |
Output is correct |
26 |
Correct |
271 ms |
35436 KB |
Output is correct |
27 |
Correct |
325 ms |
38892 KB |
Output is correct |
28 |
Correct |
308 ms |
39760 KB |
Output is correct |
29 |
Correct |
278 ms |
34676 KB |
Output is correct |
30 |
Correct |
286 ms |
38400 KB |
Output is correct |
31 |
Correct |
256 ms |
32684 KB |
Output is correct |
32 |
Correct |
257 ms |
32600 KB |
Output is correct |
33 |
Correct |
287 ms |
38020 KB |
Output is correct |
34 |
Correct |
247 ms |
35744 KB |
Output is correct |
35 |
Correct |
307 ms |
40832 KB |
Output is correct |
36 |
Correct |
26 ms |
12364 KB |
Output is correct |
37 |
Correct |
335 ms |
36188 KB |
Output is correct |
38 |
Correct |
204 ms |
28276 KB |
Output is correct |
39 |
Correct |
77 ms |
17044 KB |
Output is correct |
40 |
Correct |
112 ms |
18268 KB |
Output is correct |
41 |
Correct |
96 ms |
13092 KB |
Output is correct |
42 |
Correct |
88 ms |
12184 KB |
Output is correct |
43 |
Correct |
97 ms |
12616 KB |
Output is correct |
44 |
Correct |
51 ms |
11816 KB |
Output is correct |
45 |
Correct |
74 ms |
11952 KB |
Output is correct |
46 |
Correct |
58 ms |
11972 KB |
Output is correct |
47 |
Correct |
67 ms |
11776 KB |
Output is correct |
48 |
Correct |
72 ms |
11688 KB |
Output is correct |
49 |
Correct |
67 ms |
12224 KB |
Output is correct |
50 |
Correct |
43 ms |
11852 KB |
Output is correct |
51 |
Correct |
49 ms |
11560 KB |
Output is correct |
52 |
Correct |
42 ms |
11744 KB |
Output is correct |
53 |
Correct |
51 ms |
11724 KB |
Output is correct |
54 |
Correct |
46 ms |
11976 KB |
Output is correct |
55 |
Correct |
42 ms |
11188 KB |
Output is correct |
56 |
Correct |
17 ms |
8532 KB |
Output is correct |
57 |
Correct |
76 ms |
10968 KB |
Output is correct |
58 |
Correct |
39 ms |
9732 KB |
Output is correct |
59 |
Correct |
3 ms |
5032 KB |
Output is correct |
60 |
Correct |
3 ms |
4948 KB |
Output is correct |
61 |
Correct |
653 ms |
47668 KB |
Output is correct |
62 |
Correct |
548 ms |
46712 KB |
Output is correct |
63 |
Correct |
585 ms |
44236 KB |
Output is correct |
64 |
Correct |
438 ms |
44284 KB |
Output is correct |
65 |
Correct |
476 ms |
46924 KB |
Output is correct |
66 |
Correct |
420 ms |
44232 KB |
Output is correct |
67 |
Correct |
390 ms |
43776 KB |
Output is correct |
68 |
Correct |
391 ms |
43060 KB |
Output is correct |
69 |
Correct |
425 ms |
44308 KB |
Output is correct |
70 |
Correct |
351 ms |
41552 KB |
Output is correct |
71 |
Correct |
321 ms |
42756 KB |
Output is correct |
72 |
Correct |
359 ms |
40364 KB |
Output is correct |
73 |
Correct |
344 ms |
41336 KB |
Output is correct |
74 |
Correct |
376 ms |
44016 KB |
Output is correct |
75 |
Correct |
355 ms |
41400 KB |
Output is correct |
76 |
Correct |
26 ms |
11980 KB |
Output is correct |
77 |
Correct |
411 ms |
35360 KB |
Output is correct |
78 |
Correct |
268 ms |
33592 KB |
Output is correct |
79 |
Correct |
3 ms |
4948 KB |
Output is correct |
80 |
Correct |
3 ms |
5032 KB |
Output is correct |