#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define repa(i,a,b) for(int i = (a); i >= (b); i--)
#define pll pair<lli,lli>
#define MAX 200000
//para el vector de querys
#define p second.first
#define id second.second
lli n,q,offset,apu,a,b,t,comp;
lli arr[MAX+2],p_arr[MAX+2]; //p_arr[i], poscion de numero i en el arreglo
vector<lli> res;
lli stree[MAX*4];
vector<pair<lli,pll> > querys;
pll sig[MAX+2];
stack<lli> pila;
void update(lli pos, lli ini, lli fin, lli l, lli r, lli val) {
if (ini > r || fin < l) return;
if (l <= ini && fin <= r) {
stree[pos] = val;
return;
}
lli mitad = (ini+fin)/2;
update(pos*2,ini,mitad,l,r,val);
update((pos*2)+1,mitad+1,fin,l,r,val);
stree[pos] = stree[pos*2] + stree[(pos*2)+1];
}
lli suma(lli pos, lli ini, lli fin, lli l, lli r) {
if (l > r) return 0;
if (ini > r || fin < l) return 0;
if (l <= ini && fin <= r) return stree[pos];
lli a;
lli mitad = (ini+fin)/2;
a = suma(pos*2,ini,mitad,l,r);
a += suma((pos*2)+1,mitad+1,fin,l,r);
return a;
}
lli busca(lli pos, lli ini, lli fin, lli sum) {
if (ini == fin) return ini;
lli mitad = (ini+fin)/2;
if (stree[pos*2] >= sum) return busca(pos*2,ini,mitad,sum);
else return busca((pos*2)+1,mitad+1,fin,sum-stree[pos*2]);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q;
res.resize(q+2);
offset = 1;
while (offset < n) offset *= 2;
rep(i,1,n) {
cin >> arr[i];
p_arr[ arr[i] ] = i;
}
rep(i,1,q) {
cin >> a >> b;
if (a == 0) res[i] = arr[b];
else querys.push_back({a,{b,i}});
}
sort(querys.begin(), querys.end());
//pre proceso de monotone stacks
//der a izq
pila.push(n+1);
repa(i,n,1) {
while (pila.top() < arr[i]) pila.pop();
sig[arr[i]].second = pila.top();
pila.push(arr[i]);
}
// de izq a der
while (!pila.empty()) pila.pop();
pila.push(n+1);
rep(i,1,n) {
while (pila.top() < arr[i]) pila.pop();
sig[arr[i]].first = pila.top();
pila.push(arr[i]);
}
//activa los nodos y haz el primer shuffle
lli mitad = (n/2);
lli dif,s,pos = 1;
p_arr[n+1] = n+1;
arr[n+1] = n+1;
while (pos <= n) {
s = p_arr[ sig[arr[pos]].second ];
if (pos <= mitad && s > mitad) s = mitad+1;
dif = s - pos;
update(1,1,offset,arr[pos],arr[pos],dif);
pos = s;
}
apu = 0;
t = 1;
while (apu < querys.size()) {
//responde querys
while (apu < querys.size() && querys[apu].first == t) {
a = busca(1,1,offset,querys[apu].p);
b = suma(1,1,offset,1,a-1);
b++;
b = querys[apu].p - b;
res[querys[apu].id] = arr[ p_arr[a] + b];
apu++;
}
if (apu == querys.size()) break;
//shuffle
t++;
a = busca(1,1,offset,(n/2)+1);
b = suma(1,1,offset,1,a-1);
if (b == (n/2)) {
//se termino
rep(i,apu,querys.size()-1) querys[i].first = t;
continue;
}
dif = (n/2) - b;
comp = stree[offset+a-1];
pos = p_arr[a] + dif;
update(1,1,offset,a,a,dif);
while (pos < p_arr[a]+comp) {
s = p_arr[ sig[arr[pos]].second ];
if (s >= p_arr[a]+comp) s = p_arr[a]+comp;
dif = s - pos;
update(1,1,offset,arr[pos],arr[pos],dif);
pos = s;
}
}
rep(i,1,q) cout << res[i] << "\n";
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:117:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | while (apu < querys.size()) {
| ~~~~^~~~~~~~~~~~~~~
Main.cpp:120:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | while (apu < querys.size() && querys[apu].first == t) {
| ~~~~^~~~~~~~~~~~~~~
Main.cpp:130:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
130 | if (apu == querys.size()) break;
| ~~~~^~~~~~~~~~~~~~~~
Main.cpp:7:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
7 | #define rep(i,a,b) for(int i = (a); i <= (b); i++)
| ^
Main.cpp:139:13: note: in expansion of macro 'rep'
139 | rep(i,apu,querys.size()-1) querys[i].first = t;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
408 ms |
43348 KB |
Output is correct |
2 |
Correct |
393 ms |
42904 KB |
Output is correct |
3 |
Correct |
388 ms |
41516 KB |
Output is correct |
4 |
Correct |
326 ms |
39348 KB |
Output is correct |
5 |
Correct |
401 ms |
42460 KB |
Output is correct |
6 |
Correct |
331 ms |
40284 KB |
Output is correct |
7 |
Correct |
355 ms |
42672 KB |
Output is correct |
8 |
Correct |
370 ms |
40456 KB |
Output is correct |
9 |
Correct |
333 ms |
39792 KB |
Output is correct |
10 |
Correct |
364 ms |
40632 KB |
Output is correct |
11 |
Correct |
350 ms |
40480 KB |
Output is correct |
12 |
Correct |
306 ms |
36448 KB |
Output is correct |
13 |
Correct |
361 ms |
39616 KB |
Output is correct |
14 |
Correct |
344 ms |
41588 KB |
Output is correct |
15 |
Correct |
380 ms |
40812 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
135 ms |
17704 KB |
Output is correct |
18 |
Correct |
234 ms |
28676 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 |
444 ms |
62076 KB |
Output is correct |
2 |
Correct |
409 ms |
62180 KB |
Output is correct |
3 |
Correct |
348 ms |
49560 KB |
Output is correct |
4 |
Correct |
334 ms |
49844 KB |
Output is correct |
5 |
Correct |
366 ms |
50980 KB |
Output is correct |
6 |
Correct |
338 ms |
49072 KB |
Output is correct |
7 |
Correct |
376 ms |
60764 KB |
Output is correct |
8 |
Correct |
380 ms |
58240 KB |
Output is correct |
9 |
Correct |
383 ms |
50760 KB |
Output is correct |
10 |
Correct |
358 ms |
57124 KB |
Output is correct |
11 |
Correct |
345 ms |
48684 KB |
Output is correct |
12 |
Correct |
316 ms |
48172 KB |
Output is correct |
13 |
Correct |
406 ms |
56552 KB |
Output is correct |
14 |
Correct |
325 ms |
49756 KB |
Output is correct |
15 |
Correct |
405 ms |
58616 KB |
Output is correct |
16 |
Correct |
20 ms |
9548 KB |
Output is correct |
17 |
Correct |
186 ms |
33744 KB |
Output is correct |
18 |
Correct |
292 ms |
44784 KB |
Output is correct |
19 |
Correct |
56 ms |
12352 KB |
Output is correct |
20 |
Correct |
93 ms |
21048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
10828 KB |
Output is correct |
2 |
Correct |
78 ms |
11084 KB |
Output is correct |
3 |
Correct |
106 ms |
10476 KB |
Output is correct |
4 |
Correct |
59 ms |
10212 KB |
Output is correct |
5 |
Correct |
82 ms |
10540 KB |
Output is correct |
6 |
Correct |
53 ms |
10064 KB |
Output is correct |
7 |
Correct |
61 ms |
10492 KB |
Output is correct |
8 |
Correct |
60 ms |
10120 KB |
Output is correct |
9 |
Correct |
69 ms |
10312 KB |
Output is correct |
10 |
Correct |
48 ms |
9920 KB |
Output is correct |
11 |
Correct |
49 ms |
10184 KB |
Output is correct |
12 |
Correct |
58 ms |
9936 KB |
Output is correct |
13 |
Correct |
48 ms |
10152 KB |
Output is correct |
14 |
Correct |
47 ms |
10300 KB |
Output is correct |
15 |
Correct |
45 ms |
9892 KB |
Output is correct |
16 |
Correct |
10 ms |
5588 KB |
Output is correct |
17 |
Correct |
36 ms |
8252 KB |
Output is correct |
18 |
Correct |
33 ms |
7892 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 |
408 ms |
43348 KB |
Output is correct |
2 |
Correct |
393 ms |
42904 KB |
Output is correct |
3 |
Correct |
388 ms |
41516 KB |
Output is correct |
4 |
Correct |
326 ms |
39348 KB |
Output is correct |
5 |
Correct |
401 ms |
42460 KB |
Output is correct |
6 |
Correct |
331 ms |
40284 KB |
Output is correct |
7 |
Correct |
355 ms |
42672 KB |
Output is correct |
8 |
Correct |
370 ms |
40456 KB |
Output is correct |
9 |
Correct |
333 ms |
39792 KB |
Output is correct |
10 |
Correct |
364 ms |
40632 KB |
Output is correct |
11 |
Correct |
350 ms |
40480 KB |
Output is correct |
12 |
Correct |
306 ms |
36448 KB |
Output is correct |
13 |
Correct |
361 ms |
39616 KB |
Output is correct |
14 |
Correct |
344 ms |
41588 KB |
Output is correct |
15 |
Correct |
380 ms |
40812 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
135 ms |
17704 KB |
Output is correct |
18 |
Correct |
234 ms |
28676 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
444 ms |
62076 KB |
Output is correct |
22 |
Correct |
409 ms |
62180 KB |
Output is correct |
23 |
Correct |
348 ms |
49560 KB |
Output is correct |
24 |
Correct |
334 ms |
49844 KB |
Output is correct |
25 |
Correct |
366 ms |
50980 KB |
Output is correct |
26 |
Correct |
338 ms |
49072 KB |
Output is correct |
27 |
Correct |
376 ms |
60764 KB |
Output is correct |
28 |
Correct |
380 ms |
58240 KB |
Output is correct |
29 |
Correct |
383 ms |
50760 KB |
Output is correct |
30 |
Correct |
358 ms |
57124 KB |
Output is correct |
31 |
Correct |
345 ms |
48684 KB |
Output is correct |
32 |
Correct |
316 ms |
48172 KB |
Output is correct |
33 |
Correct |
406 ms |
56552 KB |
Output is correct |
34 |
Correct |
325 ms |
49756 KB |
Output is correct |
35 |
Correct |
405 ms |
58616 KB |
Output is correct |
36 |
Correct |
20 ms |
9548 KB |
Output is correct |
37 |
Correct |
186 ms |
33744 KB |
Output is correct |
38 |
Correct |
292 ms |
44784 KB |
Output is correct |
39 |
Correct |
56 ms |
12352 KB |
Output is correct |
40 |
Correct |
93 ms |
21048 KB |
Output is correct |
41 |
Correct |
90 ms |
10828 KB |
Output is correct |
42 |
Correct |
78 ms |
11084 KB |
Output is correct |
43 |
Correct |
106 ms |
10476 KB |
Output is correct |
44 |
Correct |
59 ms |
10212 KB |
Output is correct |
45 |
Correct |
82 ms |
10540 KB |
Output is correct |
46 |
Correct |
53 ms |
10064 KB |
Output is correct |
47 |
Correct |
61 ms |
10492 KB |
Output is correct |
48 |
Correct |
60 ms |
10120 KB |
Output is correct |
49 |
Correct |
69 ms |
10312 KB |
Output is correct |
50 |
Correct |
48 ms |
9920 KB |
Output is correct |
51 |
Correct |
49 ms |
10184 KB |
Output is correct |
52 |
Correct |
58 ms |
9936 KB |
Output is correct |
53 |
Correct |
48 ms |
10152 KB |
Output is correct |
54 |
Correct |
47 ms |
10300 KB |
Output is correct |
55 |
Correct |
45 ms |
9892 KB |
Output is correct |
56 |
Correct |
10 ms |
5588 KB |
Output is correct |
57 |
Correct |
36 ms |
8252 KB |
Output is correct |
58 |
Correct |
33 ms |
7892 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
0 ms |
340 KB |
Output is correct |
61 |
Correct |
595 ms |
62244 KB |
Output is correct |
62 |
Correct |
626 ms |
62024 KB |
Output is correct |
63 |
Correct |
666 ms |
59240 KB |
Output is correct |
64 |
Correct |
551 ms |
58300 KB |
Output is correct |
65 |
Correct |
547 ms |
60688 KB |
Output is correct |
66 |
Correct |
489 ms |
58092 KB |
Output is correct |
67 |
Correct |
497 ms |
60280 KB |
Output is correct |
68 |
Correct |
516 ms |
58056 KB |
Output is correct |
69 |
Correct |
570 ms |
58196 KB |
Output is correct |
70 |
Correct |
509 ms |
57712 KB |
Output is correct |
71 |
Correct |
447 ms |
56796 KB |
Output is correct |
72 |
Correct |
490 ms |
57240 KB |
Output is correct |
73 |
Correct |
462 ms |
57448 KB |
Output is correct |
74 |
Correct |
485 ms |
58824 KB |
Output is correct |
75 |
Correct |
478 ms |
59052 KB |
Output is correct |
76 |
Correct |
20 ms |
9548 KB |
Output is correct |
77 |
Correct |
208 ms |
34164 KB |
Output is correct |
78 |
Correct |
291 ms |
42284 KB |
Output is correct |
79 |
Correct |
0 ms |
340 KB |
Output is correct |
80 |
Correct |
0 ms |
340 KB |
Output is correct |