#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
#define int ll
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mispertion ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define F first
#define S second
#define getlast(s) (*s.rbegin())
#define debg cout << "OK\n"
const ld PI = 3.1415926535;
const int N = 2e5+10;
const int M = 1e4 + 5;
int mod = 1e9+7;
const int infi = INT_MAX;
const ll infl = LLONG_MAX;
const int P = 467743;
int mult(int a, int b) {
return a * 1LL * b % mod;
}
int sum(int a, int b) {
a %= mod;
b %= mod;
if(a + b >= mod)
return a + b - mod;
if(a + b < 0)
return a + b + mod;
return a + b;
}
int binpow(int a, ll n) {
if (n == 0)
return 1;
if (n % 2 == 1) {
return mult(binpow(a, n - 1), a);
} else {
int b = binpow(a, n / 2);
return mult(b, b);
}
}
struct fenwick{
int bt[N];
void add(int i, int x){
for(; i < N; i = (i | (i + 1)))
bt[i] += x;
}
int get(int r){
int ret = 0;
for(; r >= 0; r = (r & (r + 1)) - 1)
ret += bt[r];
return ret;
}
int get(int l, int r){
return get(r) - get(l - 1);
}
} f;
int n, q, a[N], nxt[N], csz, ans[1000005];
set<pair<int, pii>> st;
pii otr[N];
vector<pii> que[N];
void shuffle(){
while(sz(st) > 0 && csz - (st.rbegin()->S.S - st.rbegin()->S.F + 1) >= n / 2){
csz -= (st.rbegin()->S.S - st.rbegin()->S.F + 1);
st.erase(*st.rbegin());
}
if(csz == n / 2)
return;
pair<int, pii> pr = *st.rbegin();
f.add(pr.F, -(pr.S.S - pr.S.F + 1));
st.erase(pr);
int nd = n / 2 - (csz - (pr.S.S - pr.S.F + 1));
f.add(pr.F, nd);
otr[pr.F] = {pr.S.F, pr.S.F + nd - 1};
st.insert({pr.F, {pr.S.F, pr.S.F + nd - 1}});
int nl = pr.S.F + nd, nr = pr.S.S;
while(nl <= nr){
int r = nxt[nl];
f.add(a[nl], min(r - 1, nr) - nl + 1);
otr[a[nl]] = {nl, min(r - 1, nr)};
st.insert({a[nl], {nl, min(r - 1, nr)}});
nl = r;
}
}
void solve(){
cin >> n >> q;
for(int i = 1; i <= n; i++)
cin >> a[i];
for(int i = 1; i <= q; i++){
int t, ps;
cin >> t >> ps;
t = min(t, n);
que[t].pb({ps, i});
}
a[n + 1] = n + 1;
for(int i = n; i >= 1; i--){
nxt[i] = i + 1;
while(a[i] > a[nxt[i]]){
nxt[i] = nxt[nxt[i]];
}
}
csz = n;
int i = 1;
while(i <= n){
st.insert({a[i], {i, nxt[i] - 1}});
f.add(a[i], nxt[i] - i);
otr[a[i]] = {i, nxt[i] - 1};
i = nxt[i];
}
for(int i = 0; i <= n; i++){
for(auto qu : que[i]){
int ps = qu.F;
int lo = 0, hi = n + 1;
while(lo + 1 < hi){
int m = (lo + hi) / 2;
if(f.get(m) < ps)
lo = m;
else
hi = m;
}
int ha = ps - f.get(hi - 1);
ans[qu.S] = a[otr[hi].F + ha - 1];
}
shuffle();
}
for(int i = 1; i <= q; i++)
cout << ans[i] << '\n';
}
signed main() {
mispertion;
int t = 1;
//cin >> t;
while(t--){
solve();
}
return 0;
}
// coded by mispertion :)
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
36560 KB |
Output is correct |
2 |
Correct |
251 ms |
42024 KB |
Output is correct |
3 |
Correct |
215 ms |
43056 KB |
Output is correct |
4 |
Correct |
219 ms |
39964 KB |
Output is correct |
5 |
Correct |
274 ms |
46288 KB |
Output is correct |
6 |
Correct |
241 ms |
43688 KB |
Output is correct |
7 |
Correct |
237 ms |
48592 KB |
Output is correct |
8 |
Correct |
231 ms |
41916 KB |
Output is correct |
9 |
Correct |
217 ms |
40968 KB |
Output is correct |
10 |
Correct |
236 ms |
41220 KB |
Output is correct |
11 |
Correct |
229 ms |
41032 KB |
Output is correct |
12 |
Correct |
239 ms |
38472 KB |
Output is correct |
13 |
Correct |
231 ms |
39672 KB |
Output is correct |
14 |
Correct |
237 ms |
43776 KB |
Output is correct |
15 |
Correct |
234 ms |
40780 KB |
Output is correct |
16 |
Correct |
3 ms |
7256 KB |
Output is correct |
17 |
Correct |
270 ms |
38972 KB |
Output is correct |
18 |
Correct |
194 ms |
38664 KB |
Output is correct |
19 |
Correct |
2 ms |
7224 KB |
Output is correct |
20 |
Correct |
2 ms |
7256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
426 ms |
49056 KB |
Output is correct |
2 |
Correct |
415 ms |
65660 KB |
Output is correct |
3 |
Correct |
401 ms |
52892 KB |
Output is correct |
4 |
Correct |
402 ms |
50024 KB |
Output is correct |
5 |
Correct |
340 ms |
51332 KB |
Output is correct |
6 |
Correct |
373 ms |
49256 KB |
Output is correct |
7 |
Correct |
387 ms |
57760 KB |
Output is correct |
8 |
Correct |
378 ms |
56296 KB |
Output is correct |
9 |
Correct |
363 ms |
50360 KB |
Output is correct |
10 |
Correct |
361 ms |
53280 KB |
Output is correct |
11 |
Correct |
284 ms |
44956 KB |
Output is correct |
12 |
Correct |
313 ms |
46016 KB |
Output is correct |
13 |
Correct |
354 ms |
52352 KB |
Output is correct |
14 |
Correct |
274 ms |
46972 KB |
Output is correct |
15 |
Correct |
394 ms |
54348 KB |
Output is correct |
16 |
Correct |
17 ms |
13148 KB |
Output is correct |
17 |
Correct |
393 ms |
62576 KB |
Output is correct |
18 |
Correct |
326 ms |
38924 KB |
Output is correct |
19 |
Correct |
114 ms |
20052 KB |
Output is correct |
20 |
Correct |
107 ms |
22772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
16208 KB |
Output is correct |
2 |
Correct |
89 ms |
17488 KB |
Output is correct |
3 |
Correct |
112 ms |
15700 KB |
Output is correct |
4 |
Correct |
63 ms |
13140 KB |
Output is correct |
5 |
Correct |
70 ms |
15700 KB |
Output is correct |
6 |
Correct |
60 ms |
13400 KB |
Output is correct |
7 |
Correct |
62 ms |
14932 KB |
Output is correct |
8 |
Correct |
58 ms |
13900 KB |
Output is correct |
9 |
Correct |
83 ms |
14964 KB |
Output is correct |
10 |
Correct |
41 ms |
12372 KB |
Output is correct |
11 |
Correct |
48 ms |
12988 KB |
Output is correct |
12 |
Correct |
50 ms |
12612 KB |
Output is correct |
13 |
Correct |
56 ms |
12368 KB |
Output is correct |
14 |
Correct |
39 ms |
12884 KB |
Output is correct |
15 |
Correct |
40 ms |
11936 KB |
Output is correct |
16 |
Correct |
13 ms |
9308 KB |
Output is correct |
17 |
Correct |
85 ms |
17820 KB |
Output is correct |
18 |
Correct |
42 ms |
9648 KB |
Output is correct |
19 |
Correct |
2 ms |
5212 KB |
Output is correct |
20 |
Correct |
2 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
36560 KB |
Output is correct |
2 |
Correct |
251 ms |
42024 KB |
Output is correct |
3 |
Correct |
215 ms |
43056 KB |
Output is correct |
4 |
Correct |
219 ms |
39964 KB |
Output is correct |
5 |
Correct |
274 ms |
46288 KB |
Output is correct |
6 |
Correct |
241 ms |
43688 KB |
Output is correct |
7 |
Correct |
237 ms |
48592 KB |
Output is correct |
8 |
Correct |
231 ms |
41916 KB |
Output is correct |
9 |
Correct |
217 ms |
40968 KB |
Output is correct |
10 |
Correct |
236 ms |
41220 KB |
Output is correct |
11 |
Correct |
229 ms |
41032 KB |
Output is correct |
12 |
Correct |
239 ms |
38472 KB |
Output is correct |
13 |
Correct |
231 ms |
39672 KB |
Output is correct |
14 |
Correct |
237 ms |
43776 KB |
Output is correct |
15 |
Correct |
234 ms |
40780 KB |
Output is correct |
16 |
Correct |
3 ms |
7256 KB |
Output is correct |
17 |
Correct |
270 ms |
38972 KB |
Output is correct |
18 |
Correct |
194 ms |
38664 KB |
Output is correct |
19 |
Correct |
2 ms |
7224 KB |
Output is correct |
20 |
Correct |
2 ms |
7256 KB |
Output is correct |
21 |
Correct |
426 ms |
49056 KB |
Output is correct |
22 |
Correct |
415 ms |
65660 KB |
Output is correct |
23 |
Correct |
401 ms |
52892 KB |
Output is correct |
24 |
Correct |
402 ms |
50024 KB |
Output is correct |
25 |
Correct |
340 ms |
51332 KB |
Output is correct |
26 |
Correct |
373 ms |
49256 KB |
Output is correct |
27 |
Correct |
387 ms |
57760 KB |
Output is correct |
28 |
Correct |
378 ms |
56296 KB |
Output is correct |
29 |
Correct |
363 ms |
50360 KB |
Output is correct |
30 |
Correct |
361 ms |
53280 KB |
Output is correct |
31 |
Correct |
284 ms |
44956 KB |
Output is correct |
32 |
Correct |
313 ms |
46016 KB |
Output is correct |
33 |
Correct |
354 ms |
52352 KB |
Output is correct |
34 |
Correct |
274 ms |
46972 KB |
Output is correct |
35 |
Correct |
394 ms |
54348 KB |
Output is correct |
36 |
Correct |
17 ms |
13148 KB |
Output is correct |
37 |
Correct |
393 ms |
62576 KB |
Output is correct |
38 |
Correct |
326 ms |
38924 KB |
Output is correct |
39 |
Correct |
114 ms |
20052 KB |
Output is correct |
40 |
Correct |
107 ms |
22772 KB |
Output is correct |
41 |
Correct |
96 ms |
16208 KB |
Output is correct |
42 |
Correct |
89 ms |
17488 KB |
Output is correct |
43 |
Correct |
112 ms |
15700 KB |
Output is correct |
44 |
Correct |
63 ms |
13140 KB |
Output is correct |
45 |
Correct |
70 ms |
15700 KB |
Output is correct |
46 |
Correct |
60 ms |
13400 KB |
Output is correct |
47 |
Correct |
62 ms |
14932 KB |
Output is correct |
48 |
Correct |
58 ms |
13900 KB |
Output is correct |
49 |
Correct |
83 ms |
14964 KB |
Output is correct |
50 |
Correct |
41 ms |
12372 KB |
Output is correct |
51 |
Correct |
48 ms |
12988 KB |
Output is correct |
52 |
Correct |
50 ms |
12612 KB |
Output is correct |
53 |
Correct |
56 ms |
12368 KB |
Output is correct |
54 |
Correct |
39 ms |
12884 KB |
Output is correct |
55 |
Correct |
40 ms |
11936 KB |
Output is correct |
56 |
Correct |
13 ms |
9308 KB |
Output is correct |
57 |
Correct |
85 ms |
17820 KB |
Output is correct |
58 |
Correct |
42 ms |
9648 KB |
Output is correct |
59 |
Correct |
2 ms |
5212 KB |
Output is correct |
60 |
Correct |
2 ms |
5212 KB |
Output is correct |
61 |
Correct |
787 ms |
71836 KB |
Output is correct |
62 |
Correct |
678 ms |
73348 KB |
Output is correct |
63 |
Correct |
752 ms |
67996 KB |
Output is correct |
64 |
Correct |
579 ms |
64928 KB |
Output is correct |
65 |
Correct |
560 ms |
67532 KB |
Output is correct |
66 |
Correct |
612 ms |
64840 KB |
Output is correct |
67 |
Correct |
448 ms |
63392 KB |
Output is correct |
68 |
Correct |
473 ms |
61176 KB |
Output is correct |
69 |
Correct |
541 ms |
65076 KB |
Output is correct |
70 |
Correct |
466 ms |
58540 KB |
Output is correct |
71 |
Correct |
364 ms |
60364 KB |
Output is correct |
72 |
Correct |
467 ms |
59496 KB |
Output is correct |
73 |
Correct |
424 ms |
60368 KB |
Output is correct |
74 |
Correct |
405 ms |
64020 KB |
Output is correct |
75 |
Correct |
463 ms |
60124 KB |
Output is correct |
76 |
Correct |
22 ms |
15696 KB |
Output is correct |
77 |
Correct |
686 ms |
62956 KB |
Output is correct |
78 |
Correct |
468 ms |
49596 KB |
Output is correct |
79 |
Correct |
2 ms |
12888 KB |
Output is correct |
80 |
Correct |
2 ms |
12892 KB |
Output is correct |