#include <bits/stdc++.h>
using namespace std;
#define tol(bi) (1LL<<((int)(bi)))
struct SegTree{
vector<int> segtree;
SegTree(int n){
segtree.resize(tol(ceil(log2(n)+1))-1,0);
}
void update(int node, int x){
node+=segtree.size()/2;
segtree[node]=x;
while (node){
node=(node-1)/2;
segtree[node]=segtree[node*2+1]+segtree[node*2+2];
}
}
int query(int tarl, int tarr, int l = 0, int r = -1, int node = 0){
if (r==-1) r = segtree.size()/2;
if (l>=tarl && r<=tarr) return segtree[node];
if (l>tarr || r<tarl) return 0;
int mid = l+(r-l)/2;
int lnode = query(tarl, tarr, l, mid, node*2+1);
int rnode = query(tarl, tarr, mid+1, r, node*2+2);
return lnode+rnode;
}
int find(int x){
int node = 0;
while (node*2+1<segtree.size()){
if (segtree[node*2+1]<=x){
x-=segtree[node*2+1];
node=node*2+2;
}
else {
node=node*2+1;
}
}
return node-segtree.size()/2;
}
void debug(){
for (int i = segtree.size()/2; i < segtree.size(); i++){
cout<<segtree[i]<<" ";
}
cout<<endl;
}
};
int main(){
int n,q;
cin>>n>>q;
vector<int> arr(n);
for (int i = 0; i < n; i++){
cin>>arr[i];
}
vector<int> bendensonra(n,n);
vector<int> stk;
vector<int> posses(n+1);
for (int i = n-1; i >= 0; i--){
posses[arr[i]]=i;
while (stk.size() && arr[stk.back()]<arr[i]) stk.pop_back();
if (stk.size()) bendensonra[i]=stk.back();
stk.push_back(i);
}
vector<array<int,3>> qarr(q);
for (int i = 0; i < q; ++i)
{
cin>>qarr[i][0]>>qarr[i][1];
qarr[i][2]=i;
}
sort(qarr.begin(), qarr.end());
int time = 0;
SegTree segtree(n+1);
int _pp = 0;
while (_pp<n){
int nex = bendensonra[_pp];
segtree.update(arr[_pp],nex-_pp);
_pp=nex;
}
vector<int> ansarr(q);
for (int i = 0; i < q; i++){
while (time<min(qarr[i][0],n)){
int ele = segtree.find(n/2);
int pos = posses[ele]+n/2-segtree.query(0,ele-1);
int bound = posses[ele]+segtree.query(ele,ele);
if (pos!=posses[ele]){
segtree.update(ele,pos-posses[ele]);
}
while (pos<bound && segtree.query(arr[pos],arr[pos])==0){
int nex = bendensonra[pos];
segtree.update(arr[pos],min(bound,nex)-pos);
pos=nex;
}
time++;
}
int ele = segtree.find((qarr[i][1]-1));
int pos = posses[ele]+(qarr[i][1]-1)-segtree.query(0,ele-1);
ansarr[qarr[i][2]]=arr[pos];
}
for (int i = 0; i < q; ++i)
{
cout<<ansarr[i]<<endl;
}
}
//3 1 5 4 2 6
Compilation message
Main.cpp: In member function 'int SegTree::find(int)':
Main.cpp:28:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | while (node*2+1<segtree.size()){
| ~~~~~~~~^~~~~~~~~~~~~~~
Main.cpp: In member function 'void SegTree::debug()':
Main.cpp:40:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int i = segtree.size()/2; i < segtree.size(); i++){
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1580 ms |
24176 KB |
Output is correct |
2 |
Correct |
1591 ms |
27352 KB |
Output is correct |
3 |
Correct |
1518 ms |
26276 KB |
Output is correct |
4 |
Correct |
1493 ms |
25688 KB |
Output is correct |
5 |
Correct |
1561 ms |
27172 KB |
Output is correct |
6 |
Correct |
1484 ms |
25380 KB |
Output is correct |
7 |
Correct |
1549 ms |
27472 KB |
Output is correct |
8 |
Correct |
1506 ms |
25716 KB |
Output is correct |
9 |
Correct |
1480 ms |
25184 KB |
Output is correct |
10 |
Correct |
1516 ms |
25656 KB |
Output is correct |
11 |
Correct |
1520 ms |
26072 KB |
Output is correct |
12 |
Correct |
1480 ms |
24568 KB |
Output is correct |
13 |
Correct |
1536 ms |
25436 KB |
Output is correct |
14 |
Correct |
1553 ms |
26604 KB |
Output is correct |
15 |
Correct |
1573 ms |
26144 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1501 ms |
24840 KB |
Output is correct |
18 |
Correct |
1483 ms |
24784 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1802 ms |
34200 KB |
Output is correct |
2 |
Correct |
1734 ms |
41216 KB |
Output is correct |
3 |
Correct |
1410 ms |
33000 KB |
Output is correct |
4 |
Correct |
1392 ms |
32852 KB |
Output is correct |
5 |
Correct |
1454 ms |
33568 KB |
Output is correct |
6 |
Correct |
1372 ms |
32388 KB |
Output is correct |
7 |
Correct |
1713 ms |
39944 KB |
Output is correct |
8 |
Correct |
1654 ms |
38740 KB |
Output is correct |
9 |
Correct |
1474 ms |
33508 KB |
Output is correct |
10 |
Correct |
1624 ms |
37516 KB |
Output is correct |
11 |
Correct |
1381 ms |
31600 KB |
Output is correct |
12 |
Correct |
1336 ms |
31296 KB |
Output is correct |
13 |
Correct |
1613 ms |
37144 KB |
Output is correct |
14 |
Correct |
1439 ms |
32532 KB |
Output is correct |
15 |
Correct |
1645 ms |
38484 KB |
Output is correct |
16 |
Correct |
43 ms |
6044 KB |
Output is correct |
17 |
Correct |
1584 ms |
35804 KB |
Output is correct |
18 |
Correct |
1305 ms |
29268 KB |
Output is correct |
19 |
Correct |
344 ms |
11808 KB |
Output is correct |
20 |
Correct |
423 ms |
13236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
232 ms |
5716 KB |
Output is correct |
2 |
Correct |
238 ms |
6952 KB |
Output is correct |
3 |
Correct |
224 ms |
6608 KB |
Output is correct |
4 |
Correct |
203 ms |
6092 KB |
Output is correct |
5 |
Correct |
226 ms |
6336 KB |
Output is correct |
6 |
Correct |
205 ms |
6040 KB |
Output is correct |
7 |
Correct |
220 ms |
6264 KB |
Output is correct |
8 |
Correct |
209 ms |
6384 KB |
Output is correct |
9 |
Correct |
214 ms |
6228 KB |
Output is correct |
10 |
Correct |
205 ms |
5972 KB |
Output is correct |
11 |
Correct |
222 ms |
6164 KB |
Output is correct |
12 |
Correct |
199 ms |
5972 KB |
Output is correct |
13 |
Correct |
210 ms |
6088 KB |
Output is correct |
14 |
Correct |
208 ms |
6184 KB |
Output is correct |
15 |
Correct |
210 ms |
5976 KB |
Output is correct |
16 |
Correct |
21 ms |
3188 KB |
Output is correct |
17 |
Correct |
202 ms |
6464 KB |
Output is correct |
18 |
Correct |
196 ms |
5972 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1580 ms |
24176 KB |
Output is correct |
2 |
Correct |
1591 ms |
27352 KB |
Output is correct |
3 |
Correct |
1518 ms |
26276 KB |
Output is correct |
4 |
Correct |
1493 ms |
25688 KB |
Output is correct |
5 |
Correct |
1561 ms |
27172 KB |
Output is correct |
6 |
Correct |
1484 ms |
25380 KB |
Output is correct |
7 |
Correct |
1549 ms |
27472 KB |
Output is correct |
8 |
Correct |
1506 ms |
25716 KB |
Output is correct |
9 |
Correct |
1480 ms |
25184 KB |
Output is correct |
10 |
Correct |
1516 ms |
25656 KB |
Output is correct |
11 |
Correct |
1520 ms |
26072 KB |
Output is correct |
12 |
Correct |
1480 ms |
24568 KB |
Output is correct |
13 |
Correct |
1536 ms |
25436 KB |
Output is correct |
14 |
Correct |
1553 ms |
26604 KB |
Output is correct |
15 |
Correct |
1573 ms |
26144 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1501 ms |
24840 KB |
Output is correct |
18 |
Correct |
1483 ms |
24784 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1802 ms |
34200 KB |
Output is correct |
22 |
Correct |
1734 ms |
41216 KB |
Output is correct |
23 |
Correct |
1410 ms |
33000 KB |
Output is correct |
24 |
Correct |
1392 ms |
32852 KB |
Output is correct |
25 |
Correct |
1454 ms |
33568 KB |
Output is correct |
26 |
Correct |
1372 ms |
32388 KB |
Output is correct |
27 |
Correct |
1713 ms |
39944 KB |
Output is correct |
28 |
Correct |
1654 ms |
38740 KB |
Output is correct |
29 |
Correct |
1474 ms |
33508 KB |
Output is correct |
30 |
Correct |
1624 ms |
37516 KB |
Output is correct |
31 |
Correct |
1381 ms |
31600 KB |
Output is correct |
32 |
Correct |
1336 ms |
31296 KB |
Output is correct |
33 |
Correct |
1613 ms |
37144 KB |
Output is correct |
34 |
Correct |
1439 ms |
32532 KB |
Output is correct |
35 |
Correct |
1645 ms |
38484 KB |
Output is correct |
36 |
Correct |
43 ms |
6044 KB |
Output is correct |
37 |
Correct |
1584 ms |
35804 KB |
Output is correct |
38 |
Correct |
1305 ms |
29268 KB |
Output is correct |
39 |
Correct |
344 ms |
11808 KB |
Output is correct |
40 |
Correct |
423 ms |
13236 KB |
Output is correct |
41 |
Correct |
232 ms |
5716 KB |
Output is correct |
42 |
Correct |
238 ms |
6952 KB |
Output is correct |
43 |
Correct |
224 ms |
6608 KB |
Output is correct |
44 |
Correct |
203 ms |
6092 KB |
Output is correct |
45 |
Correct |
226 ms |
6336 KB |
Output is correct |
46 |
Correct |
205 ms |
6040 KB |
Output is correct |
47 |
Correct |
220 ms |
6264 KB |
Output is correct |
48 |
Correct |
209 ms |
6384 KB |
Output is correct |
49 |
Correct |
214 ms |
6228 KB |
Output is correct |
50 |
Correct |
205 ms |
5972 KB |
Output is correct |
51 |
Correct |
222 ms |
6164 KB |
Output is correct |
52 |
Correct |
199 ms |
5972 KB |
Output is correct |
53 |
Correct |
210 ms |
6088 KB |
Output is correct |
54 |
Correct |
208 ms |
6184 KB |
Output is correct |
55 |
Correct |
210 ms |
5976 KB |
Output is correct |
56 |
Correct |
21 ms |
3188 KB |
Output is correct |
57 |
Correct |
202 ms |
6464 KB |
Output is correct |
58 |
Correct |
196 ms |
5972 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
1919 ms |
40836 KB |
Output is correct |
62 |
Correct |
1981 ms |
40904 KB |
Output is correct |
63 |
Correct |
1871 ms |
39228 KB |
Output is correct |
64 |
Correct |
1744 ms |
38412 KB |
Output is correct |
65 |
Correct |
1835 ms |
39836 KB |
Output is correct |
66 |
Correct |
1740 ms |
38452 KB |
Output is correct |
67 |
Correct |
1806 ms |
39648 KB |
Output is correct |
68 |
Correct |
1773 ms |
37968 KB |
Output is correct |
69 |
Correct |
1803 ms |
38348 KB |
Output is correct |
70 |
Correct |
1782 ms |
37828 KB |
Output is correct |
71 |
Correct |
1759 ms |
36976 KB |
Output is correct |
72 |
Correct |
1742 ms |
37408 KB |
Output is correct |
73 |
Correct |
1754 ms |
37476 KB |
Output is correct |
74 |
Correct |
1770 ms |
38660 KB |
Output is correct |
75 |
Correct |
1826 ms |
38644 KB |
Output is correct |
76 |
Correct |
43 ms |
5968 KB |
Output is correct |
77 |
Correct |
1712 ms |
36096 KB |
Output is correct |
78 |
Correct |
1687 ms |
35092 KB |
Output is correct |
79 |
Correct |
0 ms |
344 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |