#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#define int long long
#define mp make_pair
#define pb push_back
using namespace std;
using namespace __gnu_pbds;
mt19937 rnd(time(nullptr));
constexpr int N = (int)1e6 + 111;
constexpr int INF = (int)1e9 + 111;
int T[4*N];
int w[4*N];
void push(int v,int l,int r){
if(w[v] == 0)
return;
int m = (l+r)>>1;
T[v<<1] += w[v];
T[v<<1|1] += w[v];
w[v<<1] += w[v];
w[v<<1|1] += w[v];
w[v] = 0;
return;
}
void upd(int v,int l,int r,int tl,int tr,int x){
if(l > r || tl > tr){
return;
}
if(l == tl && r == tr){
T[v] += x;
w[v] += x;
return;
}
push(v,l,r);
int m = (l+r)>>1;
upd(v<<1,l,m,tl,min(tr,m),x);
upd(v<<1|1,m+1,r,max(tl,m+1),tr,x);
T[v] = min(T[v<<1],T[v<<1|1]);
return;
}
int n,q;
void upd(int l,int r,int x){
upd(1,1,n,l,r,x);
return;
}
int get(int v,int l,int r,int tl,int tr){
if(l > r || tl > tr)
return INF;
if(l == tl && r == tr)
return T[v];
push(v,l,r);
int m = (l+r)>>1;
return min(get(v<<1,l,m,tl,min(tr,m)),get(v<<1|1,m+1,r,max(m+1,tl),tr));
}
int get(int l,int r){
return get(1,1,n,l,r);
}
int getPos(int v,int l,int r,int x){ /// get a[pos] <= x
if(l > r)
return -1;
if(l == r)
return l;
int m = (l+r)>>1;
push(v,l,r);
if(T[v<<1] <= x)
return getPos(v<<1,l,m,x);
else
return getPos(v<<1|1,m+1,r,x);
}
int getSuff(int r){
return get(r,r);
}
int a[N];
int t[N],P[N];
set<int> st;
int parent[N];
vector<pair<int,pair<int,int>>> Q;
int ans[N];
int len[N];
void __attribute__ (( optimize("Ofast","unroll-loops"), target("avx2") )) solve(){
n = (int)10000, q = (int)10000;
for(int i = 0; i <= n; i++) T[i] = 0;
st.clear();
Q.clear();
cin >> n >> q;
int rv[n+1];
for(int i = 0; i < n; i++){
cin >> a[i];
rv[a[i]] = i;
}
int nxt[n+1];
vector<pair<int,int>> RightBiggest;
for(int i = n-1; i >= 0; i--){
while(!RightBiggest.empty() && RightBiggest.back().first < a[i])
RightBiggest.pop_back();
if(!RightBiggest.empty())
nxt[i] = RightBiggest.back().second;
else
nxt[i] = n;
RightBiggest.pb(mp(a[i],i));
}
for(int i = 0; i < q; i++){
cin >> t[i] >> P[i];
Q.pb(mp(t[i],mp(P[i],i)));
}
sort(Q.begin(),Q.end());
vector<pair<int,int>> v;
bool used[n+1];
for(int i = 0; i <= n; i++){
used[i] = false;
}
for(int i = 0; i < n; i++){
if(used[i])
continue;
int __r = nxt[i];
if(i < n/2)
__r = min(__r,n/2);
for(int j = i; j < __r; j++) used[j] = 1;
st.insert(a[i]);
len[a[i]] = __r - i;
upd(1,a[i],__r-i);
}
int cur_t = 1;
bool all = false;
for(auto& query : Q){
int& t = query.first;
int& pos = query.second.first;
int& id = query.second.second;
if(t == 0){
ans[id] = a[pos-1];
continue;
}
while(!all && cur_t < t){
int y = getPos(1,1,n,n/2);
auto it = st.lower_bound(y);
it--;
int x = *it;
int tr = getSuff(y);
if(n/2 - tr <= 0){
all = true;
break;
}
int delta = n/2 - tr;
int __len = len[x];
int __pos = rv[x] + __len - delta;
upd(1,x,-delta);
len[x] -= delta;
while(__pos < rv[x] + __len){
int __r = min(nxt[__pos],rv[x] + __len);
st.insert(a[__pos]);
upd(1,a[__pos],__r-__pos);
len[a[__pos]] = __r - __pos;
__pos = __r;
}
cur_t++;
}
int x = getPos(1,1,n,n-pos);
auto it = st.lower_bound(x);
it--;
if(get(x,x) <= n-pos){
x = *it;
}
int sz = len[x];
int suffSum = 0;
it = st.lower_bound(x);
it++;
if(it != st.end()){
int y = *it;
suffSum = getSuff(y);
}
int cur_pos = (n - pos + 1) - suffSum;
cur_pos = sz - cur_pos;
int __pos = rv[x];
ans[id] = a[__pos + cur_pos];
}
for(int i = 0; i < q; i++){
cout << ans[i] << "\n";
}
return;
}
signed main(){
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int tests = 1;
// cin >> tests;
for(int test = 1; test <= tests; test++){
solve();
}
return 0;
}
/**
**/
Compilation message
Main.cpp: In function 'void push(int, int, int)':
Main.cpp:21:9: warning: unused variable 'm' [-Wunused-variable]
21 | int m = (l+r)>>1;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
614 ms |
28468 KB |
Output is correct |
2 |
Correct |
645 ms |
30616 KB |
Output is correct |
3 |
Correct |
612 ms |
30516 KB |
Output is correct |
4 |
Correct |
489 ms |
32824 KB |
Output is correct |
5 |
Correct |
522 ms |
33516 KB |
Output is correct |
6 |
Correct |
495 ms |
31896 KB |
Output is correct |
7 |
Correct |
536 ms |
32216 KB |
Output is correct |
8 |
Correct |
499 ms |
31420 KB |
Output is correct |
9 |
Correct |
494 ms |
31648 KB |
Output is correct |
10 |
Correct |
509 ms |
31452 KB |
Output is correct |
11 |
Correct |
507 ms |
31388 KB |
Output is correct |
12 |
Correct |
476 ms |
30372 KB |
Output is correct |
13 |
Correct |
499 ms |
30984 KB |
Output is correct |
14 |
Correct |
550 ms |
33836 KB |
Output is correct |
15 |
Correct |
556 ms |
34176 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
312 ms |
32360 KB |
Output is correct |
18 |
Correct |
397 ms |
32076 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
980 ms |
57720 KB |
Output is correct |
2 |
Correct |
980 ms |
60404 KB |
Output is correct |
3 |
Correct |
771 ms |
47868 KB |
Output is correct |
4 |
Correct |
584 ms |
40300 KB |
Output is correct |
5 |
Correct |
623 ms |
41144 KB |
Output is correct |
6 |
Correct |
555 ms |
41544 KB |
Output is correct |
7 |
Correct |
669 ms |
45416 KB |
Output is correct |
8 |
Correct |
664 ms |
47920 KB |
Output is correct |
9 |
Correct |
593 ms |
40572 KB |
Output is correct |
10 |
Correct |
613 ms |
44656 KB |
Output is correct |
11 |
Correct |
548 ms |
36880 KB |
Output is correct |
12 |
Correct |
568 ms |
35560 KB |
Output is correct |
13 |
Correct |
664 ms |
44484 KB |
Output is correct |
14 |
Correct |
570 ms |
41408 KB |
Output is correct |
15 |
Correct |
703 ms |
48508 KB |
Output is correct |
16 |
Correct |
23 ms |
6476 KB |
Output is correct |
17 |
Correct |
376 ms |
52964 KB |
Output is correct |
18 |
Correct |
497 ms |
30496 KB |
Output is correct |
19 |
Correct |
73 ms |
11132 KB |
Output is correct |
20 |
Correct |
168 ms |
16168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
214 ms |
13512 KB |
Output is correct |
2 |
Correct |
193 ms |
13312 KB |
Output is correct |
3 |
Correct |
198 ms |
13340 KB |
Output is correct |
4 |
Correct |
87 ms |
8720 KB |
Output is correct |
5 |
Correct |
132 ms |
9452 KB |
Output is correct |
6 |
Correct |
88 ms |
8920 KB |
Output is correct |
7 |
Correct |
117 ms |
9024 KB |
Output is correct |
8 |
Correct |
102 ms |
8908 KB |
Output is correct |
9 |
Correct |
120 ms |
9928 KB |
Output is correct |
10 |
Correct |
83 ms |
8324 KB |
Output is correct |
11 |
Correct |
92 ms |
7544 KB |
Output is correct |
12 |
Correct |
86 ms |
8256 KB |
Output is correct |
13 |
Correct |
84 ms |
8284 KB |
Output is correct |
14 |
Correct |
86 ms |
8512 KB |
Output is correct |
15 |
Correct |
75 ms |
7448 KB |
Output is correct |
16 |
Correct |
13 ms |
4300 KB |
Output is correct |
17 |
Correct |
75 ms |
12884 KB |
Output is correct |
18 |
Correct |
52 ms |
5408 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
614 ms |
28468 KB |
Output is correct |
2 |
Correct |
645 ms |
30616 KB |
Output is correct |
3 |
Correct |
612 ms |
30516 KB |
Output is correct |
4 |
Correct |
489 ms |
32824 KB |
Output is correct |
5 |
Correct |
522 ms |
33516 KB |
Output is correct |
6 |
Correct |
495 ms |
31896 KB |
Output is correct |
7 |
Correct |
536 ms |
32216 KB |
Output is correct |
8 |
Correct |
499 ms |
31420 KB |
Output is correct |
9 |
Correct |
494 ms |
31648 KB |
Output is correct |
10 |
Correct |
509 ms |
31452 KB |
Output is correct |
11 |
Correct |
507 ms |
31388 KB |
Output is correct |
12 |
Correct |
476 ms |
30372 KB |
Output is correct |
13 |
Correct |
499 ms |
30984 KB |
Output is correct |
14 |
Correct |
550 ms |
33836 KB |
Output is correct |
15 |
Correct |
556 ms |
34176 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
312 ms |
32360 KB |
Output is correct |
18 |
Correct |
397 ms |
32076 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
980 ms |
57720 KB |
Output is correct |
22 |
Correct |
980 ms |
60404 KB |
Output is correct |
23 |
Correct |
771 ms |
47868 KB |
Output is correct |
24 |
Correct |
584 ms |
40300 KB |
Output is correct |
25 |
Correct |
623 ms |
41144 KB |
Output is correct |
26 |
Correct |
555 ms |
41544 KB |
Output is correct |
27 |
Correct |
669 ms |
45416 KB |
Output is correct |
28 |
Correct |
664 ms |
47920 KB |
Output is correct |
29 |
Correct |
593 ms |
40572 KB |
Output is correct |
30 |
Correct |
613 ms |
44656 KB |
Output is correct |
31 |
Correct |
548 ms |
36880 KB |
Output is correct |
32 |
Correct |
568 ms |
35560 KB |
Output is correct |
33 |
Correct |
664 ms |
44484 KB |
Output is correct |
34 |
Correct |
570 ms |
41408 KB |
Output is correct |
35 |
Correct |
703 ms |
48508 KB |
Output is correct |
36 |
Correct |
23 ms |
6476 KB |
Output is correct |
37 |
Correct |
376 ms |
52964 KB |
Output is correct |
38 |
Correct |
497 ms |
30496 KB |
Output is correct |
39 |
Correct |
73 ms |
11132 KB |
Output is correct |
40 |
Correct |
168 ms |
16168 KB |
Output is correct |
41 |
Correct |
214 ms |
13512 KB |
Output is correct |
42 |
Correct |
193 ms |
13312 KB |
Output is correct |
43 |
Correct |
198 ms |
13340 KB |
Output is correct |
44 |
Correct |
87 ms |
8720 KB |
Output is correct |
45 |
Correct |
132 ms |
9452 KB |
Output is correct |
46 |
Correct |
88 ms |
8920 KB |
Output is correct |
47 |
Correct |
117 ms |
9024 KB |
Output is correct |
48 |
Correct |
102 ms |
8908 KB |
Output is correct |
49 |
Correct |
120 ms |
9928 KB |
Output is correct |
50 |
Correct |
83 ms |
8324 KB |
Output is correct |
51 |
Correct |
92 ms |
7544 KB |
Output is correct |
52 |
Correct |
86 ms |
8256 KB |
Output is correct |
53 |
Correct |
84 ms |
8284 KB |
Output is correct |
54 |
Correct |
86 ms |
8512 KB |
Output is correct |
55 |
Correct |
75 ms |
7448 KB |
Output is correct |
56 |
Correct |
13 ms |
4300 KB |
Output is correct |
57 |
Correct |
75 ms |
12884 KB |
Output is correct |
58 |
Correct |
52 ms |
5408 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
1496 ms |
56212 KB |
Output is correct |
62 |
Correct |
1748 ms |
57992 KB |
Output is correct |
63 |
Correct |
1547 ms |
53104 KB |
Output is correct |
64 |
Correct |
829 ms |
45784 KB |
Output is correct |
65 |
Correct |
875 ms |
49336 KB |
Output is correct |
66 |
Correct |
826 ms |
46636 KB |
Output is correct |
67 |
Correct |
797 ms |
47392 KB |
Output is correct |
68 |
Correct |
905 ms |
47396 KB |
Output is correct |
69 |
Correct |
927 ms |
45896 KB |
Output is correct |
70 |
Correct |
812 ms |
45668 KB |
Output is correct |
71 |
Correct |
729 ms |
44808 KB |
Output is correct |
72 |
Correct |
832 ms |
42680 KB |
Output is correct |
73 |
Correct |
813 ms |
43616 KB |
Output is correct |
74 |
Correct |
793 ms |
45260 KB |
Output is correct |
75 |
Correct |
864 ms |
43724 KB |
Output is correct |
76 |
Correct |
24 ms |
5572 KB |
Output is correct |
77 |
Correct |
457 ms |
51516 KB |
Output is correct |
78 |
Correct |
488 ms |
38168 KB |
Output is correct |
79 |
Correct |
1 ms |
340 KB |
Output is correct |
80 |
Correct |
1 ms |
340 KB |
Output is correct |