#include<bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct Treap{
struct node;
using pnode = node*;
struct node{
int val,freq,sz,prio;
pnode l,r;
node(int _val,int _freq=1):val(_val),freq(_freq),sz(_freq),prio(rng()),l(nullptr),r(nullptr){}
};
pnode rt;
Treap():rt(nullptr){}
static int get_val(pnode t){
return t?t->val:0;
}
static int get_sz(pnode t){
return t?t->sz:0;
}
static void pull(pnode t){
if(!t)return;
t->sz=get_sz(t->l)+t->freq+get_sz(t->r);
}
static void merge(pnode &t,pnode l,pnode r){
if(!l)return void(t=r);
if(!r)return void(t=l);
if(l->prio>r->prio)merge(l->r,l->r,r),t=l;
else merge(r->l,l,r->l),t=r;
pull(t);
}
void merge(pnode l,pnode r){
merge(rt,l,r);
}
static void split(pnode t,pnode &l,pnode &r,int k){
if(!t)return void(l=r=nullptr);
int x=t->freq+get_sz(t->l);
if(x>k)split(t->l,l,t->l,k),r=t;
else split(t->r,t->r,r,k-x),l=t;
pull(t);
}
void split(pnode &l,pnode &r,int k){
split(rt,l,r,k);
}
static void split_first(pnode t,pnode &l,pnode &r){
if(!t)return void(l=r=nullptr);
if(t->l)split_first(t->l,l,t->l),r=t;
else l=t,r=t->r,t->r=nullptr;
pull(t);
}
static void split_by_val(pnode t,pnode &l,pnode &r,int key){
if(!t)return void(l=r=nullptr);
if(t->val>=key)split_by_val(t->l,l,t->l,key),r=t;
else split_by_val(t->r,t->r,r,key),l=t;
pull(t);
}
void split_by_val(pnode &l,pnode &r,int key){
split_by_val(rt,l,r,key);
}
static int get_first(pnode t){
if(!t)return 0;
if(t->l)return get_first(t->l);
return t->val;
}
int get_first(){
return get_first(rt);
}
pair<int,int> get(pnode t,int k){
if(k<=get_sz(t->l))return get(t->l,k);
if(k>t->freq+get_sz(t->l))return get(t->r,k-t->freq-get_sz(t->l));
return {t->val,k-get_sz(t->l)};
}
pair<int,int> get(int k){
return get(rt,k);
}
void insert(int val,int freq=1){
merge(rt,new node(val,freq));
}
int size(){
return get_sz(rt);
}
};
using node = Treap::node;
using pnode = Treap::pnode;
const int N=2e5+5;
const int Q=1e6+5;
int n,q;
int a[N],nxt[N],inv[N],pre[N];
stack<int> s;
Treap t[N],tr;
bool done=false;
vector<tuple<int,int,int>> qr;
int ans[Q];
void insert_main_tree(int id,int freq){
pnode t1,t2;
tr.split_by_val(t1,t2,id);
tr.merge(t1,new node(id,freq));
tr.merge(tr.rt,t2);
}
void work(){
if(done)return;
pnode t1,t2,t3;
tr.split(t1,t2,n/2);
int rem=n/2-Treap::get_sz(t1);
if(!rem)return done=true,tr.merge(t1,t2);
Treap::split_first(t2,t2,t3);
int id=inv[t2->val];
int tar=nxt[id];
t[id].split(t[id].rt,t2,rem);
tr.merge(t1,new node(a[id],t[id].size()));
id=inv[Treap::get_first(t2)];
tr.merge(tr.rt,t3);
for(int i=id;t2;i=nxt[i]){
Treap::split(t2,t[i].rt,t2,nxt[i]-i);
insert_main_tree(a[i],t[i].size());
}
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> q;
for(int i=1;i<=n;i++)cin >> a[i],inv[a[i]]=i;
for(int i=1;i<=n;i++){
while(!s.empty()&&a[i]>a[s.top()]){
int j=s.top();
s.pop();
nxt[j]=i;
}
if(s.empty()){
t[i].insert(a[i]);
pre[i]=i;
}else{
pre[i]=pre[s.top()];
t[pre[i]].insert(a[i]);
}
s.emplace(i);
}
inv[0]=n+1;
while(!s.empty()){
nxt[s.top()]=n+1;
s.pop();
}
for(int i=1;i<=n;i++)if(pre[i]==i)tr.insert(a[i],t[i].size());
for(int i=0;i<q;i++){
int x,y;
cin >> x >> y;
qr.emplace_back(x,y,i);
}
sort(qr.begin(),qr.end());
int cnt=0;
for(auto [x,y,i]:qr){
while(!done&&cnt<x){
work();
cnt++;
}
auto [val,rem]=tr.get(y);
ans[i]=t[inv[val]].get(rem).first;
}
for(int i=0;i<q;i++)cout << ans[i] << "\n";
}
Compilation message
Main.cpp: In function 'void work()':
Main.cpp:114:9: warning: unused variable 'tar' [-Wunused-variable]
114 | int tar=nxt[id];
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
309 ms |
30768 KB |
Output is correct |
2 |
Correct |
292 ms |
30360 KB |
Output is correct |
3 |
Correct |
294 ms |
30688 KB |
Output is correct |
4 |
Correct |
278 ms |
28840 KB |
Output is correct |
5 |
Correct |
294 ms |
30240 KB |
Output is correct |
6 |
Correct |
288 ms |
29056 KB |
Output is correct |
7 |
Correct |
304 ms |
30464 KB |
Output is correct |
8 |
Correct |
273 ms |
30404 KB |
Output is correct |
9 |
Correct |
275 ms |
29900 KB |
Output is correct |
10 |
Correct |
272 ms |
29364 KB |
Output is correct |
11 |
Correct |
281 ms |
29396 KB |
Output is correct |
12 |
Correct |
259 ms |
28540 KB |
Output is correct |
13 |
Correct |
291 ms |
29748 KB |
Output is correct |
14 |
Correct |
281 ms |
29740 KB |
Output is correct |
15 |
Correct |
288 ms |
30128 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
253 ms |
28684 KB |
Output is correct |
18 |
Correct |
267 ms |
28844 KB |
Output is correct |
19 |
Correct |
1 ms |
6488 KB |
Output is correct |
20 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
381 ms |
62436 KB |
Output is correct |
2 |
Correct |
334 ms |
57768 KB |
Output is correct |
3 |
Correct |
310 ms |
50460 KB |
Output is correct |
4 |
Correct |
306 ms |
44600 KB |
Output is correct |
5 |
Correct |
315 ms |
46512 KB |
Output is correct |
6 |
Correct |
284 ms |
43132 KB |
Output is correct |
7 |
Correct |
352 ms |
49220 KB |
Output is correct |
8 |
Correct |
308 ms |
47532 KB |
Output is correct |
9 |
Correct |
304 ms |
46200 KB |
Output is correct |
10 |
Correct |
289 ms |
44720 KB |
Output is correct |
11 |
Correct |
281 ms |
41644 KB |
Output is correct |
12 |
Correct |
284 ms |
43180 KB |
Output is correct |
13 |
Correct |
303 ms |
45824 KB |
Output is correct |
14 |
Correct |
273 ms |
42784 KB |
Output is correct |
15 |
Correct |
301 ms |
47532 KB |
Output is correct |
16 |
Correct |
33 ms |
17244 KB |
Output is correct |
17 |
Correct |
271 ms |
53120 KB |
Output is correct |
18 |
Correct |
253 ms |
40684 KB |
Output is correct |
19 |
Correct |
82 ms |
24768 KB |
Output is correct |
20 |
Correct |
94 ms |
25396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
26064 KB |
Output is correct |
2 |
Correct |
94 ms |
22556 KB |
Output is correct |
3 |
Correct |
138 ms |
23292 KB |
Output is correct |
4 |
Correct |
72 ms |
17496 KB |
Output is correct |
5 |
Correct |
110 ms |
20820 KB |
Output is correct |
6 |
Correct |
79 ms |
17856 KB |
Output is correct |
7 |
Correct |
106 ms |
19704 KB |
Output is correct |
8 |
Correct |
85 ms |
18448 KB |
Output is correct |
9 |
Correct |
93 ms |
19780 KB |
Output is correct |
10 |
Correct |
61 ms |
16636 KB |
Output is correct |
11 |
Correct |
63 ms |
16836 KB |
Output is correct |
12 |
Correct |
60 ms |
16584 KB |
Output is correct |
13 |
Correct |
73 ms |
16832 KB |
Output is correct |
14 |
Correct |
78 ms |
16816 KB |
Output is correct |
15 |
Correct |
60 ms |
16576 KB |
Output is correct |
16 |
Correct |
17 ms |
11856 KB |
Output is correct |
17 |
Correct |
50 ms |
21448 KB |
Output is correct |
18 |
Correct |
46 ms |
16656 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
309 ms |
30768 KB |
Output is correct |
2 |
Correct |
292 ms |
30360 KB |
Output is correct |
3 |
Correct |
294 ms |
30688 KB |
Output is correct |
4 |
Correct |
278 ms |
28840 KB |
Output is correct |
5 |
Correct |
294 ms |
30240 KB |
Output is correct |
6 |
Correct |
288 ms |
29056 KB |
Output is correct |
7 |
Correct |
304 ms |
30464 KB |
Output is correct |
8 |
Correct |
273 ms |
30404 KB |
Output is correct |
9 |
Correct |
275 ms |
29900 KB |
Output is correct |
10 |
Correct |
272 ms |
29364 KB |
Output is correct |
11 |
Correct |
281 ms |
29396 KB |
Output is correct |
12 |
Correct |
259 ms |
28540 KB |
Output is correct |
13 |
Correct |
291 ms |
29748 KB |
Output is correct |
14 |
Correct |
281 ms |
29740 KB |
Output is correct |
15 |
Correct |
288 ms |
30128 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
253 ms |
28684 KB |
Output is correct |
18 |
Correct |
267 ms |
28844 KB |
Output is correct |
19 |
Correct |
1 ms |
6488 KB |
Output is correct |
20 |
Correct |
1 ms |
6488 KB |
Output is correct |
21 |
Correct |
381 ms |
62436 KB |
Output is correct |
22 |
Correct |
334 ms |
57768 KB |
Output is correct |
23 |
Correct |
310 ms |
50460 KB |
Output is correct |
24 |
Correct |
306 ms |
44600 KB |
Output is correct |
25 |
Correct |
315 ms |
46512 KB |
Output is correct |
26 |
Correct |
284 ms |
43132 KB |
Output is correct |
27 |
Correct |
352 ms |
49220 KB |
Output is correct |
28 |
Correct |
308 ms |
47532 KB |
Output is correct |
29 |
Correct |
304 ms |
46200 KB |
Output is correct |
30 |
Correct |
289 ms |
44720 KB |
Output is correct |
31 |
Correct |
281 ms |
41644 KB |
Output is correct |
32 |
Correct |
284 ms |
43180 KB |
Output is correct |
33 |
Correct |
303 ms |
45824 KB |
Output is correct |
34 |
Correct |
273 ms |
42784 KB |
Output is correct |
35 |
Correct |
301 ms |
47532 KB |
Output is correct |
36 |
Correct |
33 ms |
17244 KB |
Output is correct |
37 |
Correct |
271 ms |
53120 KB |
Output is correct |
38 |
Correct |
253 ms |
40684 KB |
Output is correct |
39 |
Correct |
82 ms |
24768 KB |
Output is correct |
40 |
Correct |
94 ms |
25396 KB |
Output is correct |
41 |
Correct |
133 ms |
26064 KB |
Output is correct |
42 |
Correct |
94 ms |
22556 KB |
Output is correct |
43 |
Correct |
138 ms |
23292 KB |
Output is correct |
44 |
Correct |
72 ms |
17496 KB |
Output is correct |
45 |
Correct |
110 ms |
20820 KB |
Output is correct |
46 |
Correct |
79 ms |
17856 KB |
Output is correct |
47 |
Correct |
106 ms |
19704 KB |
Output is correct |
48 |
Correct |
85 ms |
18448 KB |
Output is correct |
49 |
Correct |
93 ms |
19780 KB |
Output is correct |
50 |
Correct |
61 ms |
16636 KB |
Output is correct |
51 |
Correct |
63 ms |
16836 KB |
Output is correct |
52 |
Correct |
60 ms |
16584 KB |
Output is correct |
53 |
Correct |
73 ms |
16832 KB |
Output is correct |
54 |
Correct |
78 ms |
16816 KB |
Output is correct |
55 |
Correct |
60 ms |
16576 KB |
Output is correct |
56 |
Correct |
17 ms |
11856 KB |
Output is correct |
57 |
Correct |
50 ms |
21448 KB |
Output is correct |
58 |
Correct |
46 ms |
16656 KB |
Output is correct |
59 |
Correct |
1 ms |
6492 KB |
Output is correct |
60 |
Correct |
1 ms |
6492 KB |
Output is correct |
61 |
Correct |
951 ms |
66172 KB |
Output is correct |
62 |
Correct |
769 ms |
58764 KB |
Output is correct |
63 |
Correct |
878 ms |
59688 KB |
Output is correct |
64 |
Correct |
679 ms |
50600 KB |
Output is correct |
65 |
Correct |
779 ms |
51776 KB |
Output is correct |
66 |
Correct |
712 ms |
50136 KB |
Output is correct |
67 |
Correct |
735 ms |
48700 KB |
Output is correct |
68 |
Correct |
741 ms |
47712 KB |
Output is correct |
69 |
Correct |
665 ms |
49040 KB |
Output is correct |
70 |
Correct |
613 ms |
45864 KB |
Output is correct |
71 |
Correct |
451 ms |
44468 KB |
Output is correct |
72 |
Correct |
568 ms |
45588 KB |
Output is correct |
73 |
Correct |
500 ms |
44720 KB |
Output is correct |
74 |
Correct |
576 ms |
42968 KB |
Output is correct |
75 |
Correct |
564 ms |
43108 KB |
Output is correct |
76 |
Correct |
33 ms |
16792 KB |
Output is correct |
77 |
Correct |
366 ms |
51272 KB |
Output is correct |
78 |
Correct |
355 ms |
44128 KB |
Output is correct |
79 |
Correct |
1 ms |
6492 KB |
Output is correct |
80 |
Correct |
1 ms |
6492 KB |
Output is correct |