#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
#include <algorithm>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
const int bSize = 11;
const int bits = 17;
const int NMAX = 200001;
const int LIMIT = NMAX * bits;
vector <pii> qq[LIMIT + 1];
int sol[NMAX * 5];
int v[NMAX];
int poz[NMAX];
int stiva[NMAX];
int vf;
int nxt[NMAX];
pii interval[NMAX];
int aib[NMAX];
void update(int node, int x){
for(; node < NMAX; node += node&(-node))
aib[node] += x;
}
int query(int node){
int x = 0;
for(; node > 0; node -= node&(-node))
x += aib[node];
return x;
}
int cb(int k){
int r = 0, pas = (1 << bits), sum = 0;
while(pas){
if(r + pas < NMAX && query(r + pas) < k){
r += pas;
}
pas /= 2;
}
r++;
return r;
}
int sz(pii x){
if(x.second == 0)
return 0;
return x.second - x.first + 1;
}
int n;
void mShuffle(){
int cine = cb(n / 2);
if(query(cine) == n / 2){
return;
}
int dr = interval[cine].second;
update(cine, -sz(interval[cine]));
int pana = query(cine - 1);
int node = n / 2;
node -= pana;
interval[cine] = {poz[cine], poz[cine] + node - 1};
update(cine, sz(interval[cine]));
int i = v[poz[cine] + node];
while(sz(interval[i]) == 0 && poz[i] <= dr){
int urm = nxt[poz[i]];
if(urm == 0)
urm = n + 1;
urm = min(urm, dr + 1);
interval[i] = {poz[i], urm - 1};
update(i, sz(interval[i]));
if(urm != n + 1)
i = v[urm];
else
break;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int q, i;
cin >> n >> q;
for(i = 1; i <= n; i++){
cin >> v[i];
poz[v[i]] = i;
while(vf && v[stiva[vf]] < v[i]){
nxt[stiva[vf]] = i;
vf--;
}
stiva[++vf] = i;
}
i = 1;
while(nxt[i] != 0){
interval[v[i]] = {i, nxt[i] - 1};
i = nxt[i];
}
interval[v[i]] = {i, n};
for(i = 1; i <= n; i++){
update(i, sz(interval[i]));
}
for(i = 1; i <= q; i++){
int x, y;
cin >> x >> y;
x = min(x, LIMIT);
qq[x].push_back({y, i});
}
for(i = 0; i <= LIMIT; i++){
for(auto x : qq[i]){
int node = x.first;
int idx = x.second;
int cine = cb(node);
int pana = query(cine - 1);
node -= pana;
sol[idx] = v[poz[cine] + node - 1];
}
mShuffle();
}
for(i = 1; i <= q; i++){
cout << sol[i] << "\n";
}
return 0;
}
Compilation message
Main.cpp: In function 'int cb(int)':
Main.cpp:44:35: warning: unused variable 'sum' [-Wunused-variable]
44 | int r = 0, pas = (1 << bits), sum = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
811 ms |
98280 KB |
Output is correct |
2 |
Correct |
810 ms |
101844 KB |
Output is correct |
3 |
Correct |
763 ms |
101772 KB |
Output is correct |
4 |
Correct |
763 ms |
102696 KB |
Output is correct |
5 |
Correct |
789 ms |
104732 KB |
Output is correct |
6 |
Correct |
830 ms |
105024 KB |
Output is correct |
7 |
Correct |
802 ms |
104652 KB |
Output is correct |
8 |
Correct |
798 ms |
103008 KB |
Output is correct |
9 |
Correct |
771 ms |
102292 KB |
Output is correct |
10 |
Correct |
765 ms |
101912 KB |
Output is correct |
11 |
Correct |
816 ms |
101932 KB |
Output is correct |
12 |
Correct |
809 ms |
99856 KB |
Output is correct |
13 |
Correct |
772 ms |
100552 KB |
Output is correct |
14 |
Correct |
803 ms |
104708 KB |
Output is correct |
15 |
Correct |
767 ms |
102736 KB |
Output is correct |
16 |
Correct |
486 ms |
80332 KB |
Output is correct |
17 |
Correct |
739 ms |
100912 KB |
Output is correct |
18 |
Correct |
712 ms |
100792 KB |
Output is correct |
19 |
Correct |
318 ms |
80216 KB |
Output is correct |
20 |
Correct |
300 ms |
80336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
933 ms |
103140 KB |
Output is correct |
2 |
Correct |
960 ms |
116112 KB |
Output is correct |
3 |
Correct |
978 ms |
109500 KB |
Output is correct |
4 |
Correct |
1158 ms |
107332 KB |
Output is correct |
5 |
Correct |
870 ms |
107808 KB |
Output is correct |
6 |
Correct |
1089 ms |
108800 KB |
Output is correct |
7 |
Correct |
783 ms |
111688 KB |
Output is correct |
8 |
Correct |
997 ms |
113080 KB |
Output is correct |
9 |
Correct |
738 ms |
107472 KB |
Output is correct |
10 |
Correct |
1018 ms |
111340 KB |
Output is correct |
11 |
Correct |
722 ms |
105832 KB |
Output is correct |
12 |
Correct |
1018 ms |
105248 KB |
Output is correct |
13 |
Correct |
1014 ms |
111164 KB |
Output is correct |
14 |
Correct |
752 ms |
109332 KB |
Output is correct |
15 |
Correct |
1085 ms |
114136 KB |
Output is correct |
16 |
Correct |
464 ms |
85708 KB |
Output is correct |
17 |
Correct |
1004 ms |
109088 KB |
Output is correct |
18 |
Correct |
873 ms |
102728 KB |
Output is correct |
19 |
Correct |
874 ms |
90220 KB |
Output is correct |
20 |
Correct |
751 ms |
91392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
549 ms |
86160 KB |
Output is correct |
2 |
Correct |
551 ms |
86452 KB |
Output is correct |
3 |
Correct |
699 ms |
86572 KB |
Output is correct |
4 |
Correct |
753 ms |
86080 KB |
Output is correct |
5 |
Correct |
692 ms |
86272 KB |
Output is correct |
6 |
Correct |
695 ms |
86208 KB |
Output is correct |
7 |
Correct |
546 ms |
86000 KB |
Output is correct |
8 |
Correct |
661 ms |
85900 KB |
Output is correct |
9 |
Correct |
526 ms |
86476 KB |
Output is correct |
10 |
Correct |
640 ms |
86020 KB |
Output is correct |
11 |
Correct |
524 ms |
85764 KB |
Output is correct |
12 |
Correct |
661 ms |
86016 KB |
Output is correct |
13 |
Correct |
635 ms |
85844 KB |
Output is correct |
14 |
Correct |
566 ms |
86248 KB |
Output is correct |
15 |
Correct |
763 ms |
85380 KB |
Output is correct |
16 |
Correct |
550 ms |
82848 KB |
Output is correct |
17 |
Correct |
707 ms |
85092 KB |
Output is correct |
18 |
Correct |
645 ms |
84520 KB |
Output is correct |
19 |
Correct |
372 ms |
80156 KB |
Output is correct |
20 |
Correct |
313 ms |
80228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
811 ms |
98280 KB |
Output is correct |
2 |
Correct |
810 ms |
101844 KB |
Output is correct |
3 |
Correct |
763 ms |
101772 KB |
Output is correct |
4 |
Correct |
763 ms |
102696 KB |
Output is correct |
5 |
Correct |
789 ms |
104732 KB |
Output is correct |
6 |
Correct |
830 ms |
105024 KB |
Output is correct |
7 |
Correct |
802 ms |
104652 KB |
Output is correct |
8 |
Correct |
798 ms |
103008 KB |
Output is correct |
9 |
Correct |
771 ms |
102292 KB |
Output is correct |
10 |
Correct |
765 ms |
101912 KB |
Output is correct |
11 |
Correct |
816 ms |
101932 KB |
Output is correct |
12 |
Correct |
809 ms |
99856 KB |
Output is correct |
13 |
Correct |
772 ms |
100552 KB |
Output is correct |
14 |
Correct |
803 ms |
104708 KB |
Output is correct |
15 |
Correct |
767 ms |
102736 KB |
Output is correct |
16 |
Correct |
486 ms |
80332 KB |
Output is correct |
17 |
Correct |
739 ms |
100912 KB |
Output is correct |
18 |
Correct |
712 ms |
100792 KB |
Output is correct |
19 |
Correct |
318 ms |
80216 KB |
Output is correct |
20 |
Correct |
300 ms |
80336 KB |
Output is correct |
21 |
Correct |
933 ms |
103140 KB |
Output is correct |
22 |
Correct |
960 ms |
116112 KB |
Output is correct |
23 |
Correct |
978 ms |
109500 KB |
Output is correct |
24 |
Correct |
1158 ms |
107332 KB |
Output is correct |
25 |
Correct |
870 ms |
107808 KB |
Output is correct |
26 |
Correct |
1089 ms |
108800 KB |
Output is correct |
27 |
Correct |
783 ms |
111688 KB |
Output is correct |
28 |
Correct |
997 ms |
113080 KB |
Output is correct |
29 |
Correct |
738 ms |
107472 KB |
Output is correct |
30 |
Correct |
1018 ms |
111340 KB |
Output is correct |
31 |
Correct |
722 ms |
105832 KB |
Output is correct |
32 |
Correct |
1018 ms |
105248 KB |
Output is correct |
33 |
Correct |
1014 ms |
111164 KB |
Output is correct |
34 |
Correct |
752 ms |
109332 KB |
Output is correct |
35 |
Correct |
1085 ms |
114136 KB |
Output is correct |
36 |
Correct |
464 ms |
85708 KB |
Output is correct |
37 |
Correct |
1004 ms |
109088 KB |
Output is correct |
38 |
Correct |
873 ms |
102728 KB |
Output is correct |
39 |
Correct |
874 ms |
90220 KB |
Output is correct |
40 |
Correct |
751 ms |
91392 KB |
Output is correct |
41 |
Correct |
549 ms |
86160 KB |
Output is correct |
42 |
Correct |
551 ms |
86452 KB |
Output is correct |
43 |
Correct |
699 ms |
86572 KB |
Output is correct |
44 |
Correct |
753 ms |
86080 KB |
Output is correct |
45 |
Correct |
692 ms |
86272 KB |
Output is correct |
46 |
Correct |
695 ms |
86208 KB |
Output is correct |
47 |
Correct |
546 ms |
86000 KB |
Output is correct |
48 |
Correct |
661 ms |
85900 KB |
Output is correct |
49 |
Correct |
526 ms |
86476 KB |
Output is correct |
50 |
Correct |
640 ms |
86020 KB |
Output is correct |
51 |
Correct |
524 ms |
85764 KB |
Output is correct |
52 |
Correct |
661 ms |
86016 KB |
Output is correct |
53 |
Correct |
635 ms |
85844 KB |
Output is correct |
54 |
Correct |
566 ms |
86248 KB |
Output is correct |
55 |
Correct |
763 ms |
85380 KB |
Output is correct |
56 |
Correct |
550 ms |
82848 KB |
Output is correct |
57 |
Correct |
707 ms |
85092 KB |
Output is correct |
58 |
Correct |
645 ms |
84520 KB |
Output is correct |
59 |
Correct |
372 ms |
80156 KB |
Output is correct |
60 |
Correct |
313 ms |
80228 KB |
Output is correct |
61 |
Correct |
1405 ms |
120656 KB |
Output is correct |
62 |
Correct |
1281 ms |
120144 KB |
Output is correct |
63 |
Correct |
1543 ms |
117192 KB |
Output is correct |
64 |
Correct |
1432 ms |
116972 KB |
Output is correct |
65 |
Correct |
1272 ms |
119952 KB |
Output is correct |
66 |
Correct |
1399 ms |
117368 KB |
Output is correct |
67 |
Correct |
919 ms |
116748 KB |
Output is correct |
68 |
Correct |
1420 ms |
116120 KB |
Output is correct |
69 |
Correct |
956 ms |
117260 KB |
Output is correct |
70 |
Correct |
1122 ms |
114640 KB |
Output is correct |
71 |
Correct |
845 ms |
116104 KB |
Output is correct |
72 |
Correct |
1241 ms |
113120 KB |
Output is correct |
73 |
Correct |
1179 ms |
114400 KB |
Output is correct |
74 |
Correct |
927 ms |
116820 KB |
Output is correct |
75 |
Correct |
1184 ms |
114280 KB |
Output is correct |
76 |
Correct |
477 ms |
85364 KB |
Output is correct |
77 |
Correct |
1250 ms |
108192 KB |
Output is correct |
78 |
Correct |
1018 ms |
108012 KB |
Output is correct |
79 |
Correct |
314 ms |
80216 KB |
Output is correct |
80 |
Correct |
322 ms |
80220 KB |
Output is correct |