#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (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();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
235 ms |
42056 KB |
Output is correct |
2 |
Correct |
252 ms |
40384 KB |
Output is correct |
3 |
Correct |
250 ms |
39600 KB |
Output is correct |
4 |
Correct |
205 ms |
38328 KB |
Output is correct |
5 |
Correct |
222 ms |
43504 KB |
Output is correct |
6 |
Correct |
226 ms |
41360 KB |
Output is correct |
7 |
Correct |
232 ms |
45060 KB |
Output is correct |
8 |
Correct |
207 ms |
39132 KB |
Output is correct |
9 |
Correct |
206 ms |
38512 KB |
Output is correct |
10 |
Correct |
227 ms |
38644 KB |
Output is correct |
11 |
Correct |
208 ms |
38200 KB |
Output is correct |
12 |
Correct |
259 ms |
36160 KB |
Output is correct |
13 |
Correct |
230 ms |
36572 KB |
Output is correct |
14 |
Correct |
218 ms |
41832 KB |
Output is correct |
15 |
Correct |
221 ms |
39136 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
262 ms |
37160 KB |
Output is correct |
18 |
Correct |
191 ms |
36972 KB |
Output is correct |
19 |
Correct |
3 ms |
5032 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
391 ms |
61916 KB |
Output is correct |
2 |
Correct |
382 ms |
60844 KB |
Output is correct |
3 |
Correct |
315 ms |
51704 KB |
Output is correct |
4 |
Correct |
288 ms |
49840 KB |
Output is correct |
5 |
Correct |
316 ms |
50592 KB |
Output is correct |
6 |
Correct |
287 ms |
50948 KB |
Output is correct |
7 |
Correct |
328 ms |
57052 KB |
Output is correct |
8 |
Correct |
349 ms |
57500 KB |
Output is correct |
9 |
Correct |
272 ms |
50304 KB |
Output is correct |
10 |
Correct |
295 ms |
55960 KB |
Output is correct |
11 |
Correct |
249 ms |
47720 KB |
Output is correct |
12 |
Correct |
259 ms |
47820 KB |
Output is correct |
13 |
Correct |
282 ms |
55528 KB |
Output is correct |
14 |
Correct |
252 ms |
51480 KB |
Output is correct |
15 |
Correct |
327 ms |
58660 KB |
Output is correct |
16 |
Correct |
29 ms |
17996 KB |
Output is correct |
17 |
Correct |
335 ms |
53916 KB |
Output is correct |
18 |
Correct |
222 ms |
40900 KB |
Output is correct |
19 |
Correct |
75 ms |
24532 KB |
Output is correct |
20 |
Correct |
94 ms |
26680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
17228 KB |
Output is correct |
2 |
Correct |
92 ms |
16864 KB |
Output is correct |
3 |
Correct |
94 ms |
16728 KB |
Output is correct |
4 |
Correct |
56 ms |
16556 KB |
Output is correct |
5 |
Correct |
87 ms |
16436 KB |
Output is correct |
6 |
Correct |
58 ms |
16592 KB |
Output is correct |
7 |
Correct |
70 ms |
16360 KB |
Output is correct |
8 |
Correct |
70 ms |
16304 KB |
Output is correct |
9 |
Correct |
76 ms |
16700 KB |
Output is correct |
10 |
Correct |
45 ms |
16464 KB |
Output is correct |
11 |
Correct |
51 ms |
16456 KB |
Output is correct |
12 |
Correct |
43 ms |
16520 KB |
Output is correct |
13 |
Correct |
47 ms |
16284 KB |
Output is correct |
14 |
Correct |
47 ms |
16872 KB |
Output is correct |
15 |
Correct |
41 ms |
15292 KB |
Output is correct |
16 |
Correct |
18 ms |
11708 KB |
Output is correct |
17 |
Correct |
77 ms |
15300 KB |
Output is correct |
18 |
Correct |
38 ms |
12628 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
235 ms |
42056 KB |
Output is correct |
2 |
Correct |
252 ms |
40384 KB |
Output is correct |
3 |
Correct |
250 ms |
39600 KB |
Output is correct |
4 |
Correct |
205 ms |
38328 KB |
Output is correct |
5 |
Correct |
222 ms |
43504 KB |
Output is correct |
6 |
Correct |
226 ms |
41360 KB |
Output is correct |
7 |
Correct |
232 ms |
45060 KB |
Output is correct |
8 |
Correct |
207 ms |
39132 KB |
Output is correct |
9 |
Correct |
206 ms |
38512 KB |
Output is correct |
10 |
Correct |
227 ms |
38644 KB |
Output is correct |
11 |
Correct |
208 ms |
38200 KB |
Output is correct |
12 |
Correct |
259 ms |
36160 KB |
Output is correct |
13 |
Correct |
230 ms |
36572 KB |
Output is correct |
14 |
Correct |
218 ms |
41832 KB |
Output is correct |
15 |
Correct |
221 ms |
39136 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
262 ms |
37160 KB |
Output is correct |
18 |
Correct |
191 ms |
36972 KB |
Output is correct |
19 |
Correct |
3 ms |
5032 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
391 ms |
61916 KB |
Output is correct |
22 |
Correct |
382 ms |
60844 KB |
Output is correct |
23 |
Correct |
315 ms |
51704 KB |
Output is correct |
24 |
Correct |
288 ms |
49840 KB |
Output is correct |
25 |
Correct |
316 ms |
50592 KB |
Output is correct |
26 |
Correct |
287 ms |
50948 KB |
Output is correct |
27 |
Correct |
328 ms |
57052 KB |
Output is correct |
28 |
Correct |
349 ms |
57500 KB |
Output is correct |
29 |
Correct |
272 ms |
50304 KB |
Output is correct |
30 |
Correct |
295 ms |
55960 KB |
Output is correct |
31 |
Correct |
249 ms |
47720 KB |
Output is correct |
32 |
Correct |
259 ms |
47820 KB |
Output is correct |
33 |
Correct |
282 ms |
55528 KB |
Output is correct |
34 |
Correct |
252 ms |
51480 KB |
Output is correct |
35 |
Correct |
327 ms |
58660 KB |
Output is correct |
36 |
Correct |
29 ms |
17996 KB |
Output is correct |
37 |
Correct |
335 ms |
53916 KB |
Output is correct |
38 |
Correct |
222 ms |
40900 KB |
Output is correct |
39 |
Correct |
75 ms |
24532 KB |
Output is correct |
40 |
Correct |
94 ms |
26680 KB |
Output is correct |
41 |
Correct |
108 ms |
17228 KB |
Output is correct |
42 |
Correct |
92 ms |
16864 KB |
Output is correct |
43 |
Correct |
94 ms |
16728 KB |
Output is correct |
44 |
Correct |
56 ms |
16556 KB |
Output is correct |
45 |
Correct |
87 ms |
16436 KB |
Output is correct |
46 |
Correct |
58 ms |
16592 KB |
Output is correct |
47 |
Correct |
70 ms |
16360 KB |
Output is correct |
48 |
Correct |
70 ms |
16304 KB |
Output is correct |
49 |
Correct |
76 ms |
16700 KB |
Output is correct |
50 |
Correct |
45 ms |
16464 KB |
Output is correct |
51 |
Correct |
51 ms |
16456 KB |
Output is correct |
52 |
Correct |
43 ms |
16520 KB |
Output is correct |
53 |
Correct |
47 ms |
16284 KB |
Output is correct |
54 |
Correct |
47 ms |
16872 KB |
Output is correct |
55 |
Correct |
41 ms |
15292 KB |
Output is correct |
56 |
Correct |
18 ms |
11708 KB |
Output is correct |
57 |
Correct |
77 ms |
15300 KB |
Output is correct |
58 |
Correct |
38 ms |
12628 KB |
Output is correct |
59 |
Correct |
3 ms |
4948 KB |
Output is correct |
60 |
Correct |
3 ms |
4948 KB |
Output is correct |
61 |
Correct |
778 ms |
69096 KB |
Output is correct |
62 |
Correct |
649 ms |
69560 KB |
Output is correct |
63 |
Correct |
629 ms |
65744 KB |
Output is correct |
64 |
Correct |
455 ms |
66520 KB |
Output is correct |
65 |
Correct |
492 ms |
69948 KB |
Output is correct |
66 |
Correct |
477 ms |
66504 KB |
Output is correct |
67 |
Correct |
466 ms |
65676 KB |
Output is correct |
68 |
Correct |
452 ms |
64140 KB |
Output is correct |
69 |
Correct |
480 ms |
67020 KB |
Output is correct |
70 |
Correct |
431 ms |
61828 KB |
Output is correct |
71 |
Correct |
354 ms |
63692 KB |
Output is correct |
72 |
Correct |
383 ms |
61028 KB |
Output is correct |
73 |
Correct |
362 ms |
62408 KB |
Output is correct |
74 |
Correct |
373 ms |
66616 KB |
Output is correct |
75 |
Correct |
350 ms |
61764 KB |
Output is correct |
76 |
Correct |
29 ms |
17612 KB |
Output is correct |
77 |
Correct |
461 ms |
53188 KB |
Output is correct |
78 |
Correct |
260 ms |
48656 KB |
Output is correct |
79 |
Correct |
2 ms |
4948 KB |
Output is correct |
80 |
Correct |
3 ms |
4948 KB |
Output is correct |