/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
int n, q, ans[N], T[N], to[N], posarray[N];
array<int, 2> pos[N];
vector<array<int, 3>> Q;
vector<int> a;
vector<bool> active;
void build(int l, int r, int k){
if(l == r){
if(active[l])
T[k] = pos[l][1] - pos[l][0] + 1;
else
T[k] = 0;
return;
}
int m = l+r>>1;
build(l, m, k<<1);
build(m+1, r, k<<1|1);
T[k] = T[k<<1] + T[k<<1|1];
}
array<int, 4> get(int l, int r, int p, int k){
if(l == r){
return {l, a[pos[l][0] + p - 1], pos[l][1] - pos[l][0] + 1, pos[l][1] - pos[l][0] + 1 - p + 1};
}
int m = l+r>>1;
if(T[k<<1] >= p){
return get(l, m, p, k<<1);
}
return get(m+1, r, p - T[k<<1], k<<1|1);
}
void update(int l, int r, int p, int k, int val){
if(l == r){
T[k] = val;
pos[l] = {posarray[l], posarray[l] + val - 1};
return;
}
int m = l+r>>1;
if(p <= m)
update(l, m, p, k<<1, val);
else
update(m+1, r, p, k<<1|1, val);
T[k] = T[k<<1] + T[k<<1|1];
}
void solve(){
cin >> n >> q;
a.resize(n);
active.resize(n+1);
for(int i = 0; i < n; ++i) cin >> a[i];
for(int i = 0; i < q; ++i){
int t, x; cin >> t >> x;
Q.pb({t, x, i});
}
for(int i = 0; i < n; ++i) posarray[a[i]] = i;
to[n - 1] = n - 1;
deque<int> qq;
qq.pb(n - 1);
for(int i = n - 2; i >= 0; --i){
while(!qq.empty() && a[qq.back()] < a[i]) qq.pop_back();
if(qq.empty()) to[i] = n-1;
else to[i] = qq.back()-1;
qq.pb(i);
}
sort(all(Q));
bool ok = 1;
int t = 0;
int qp = 0;
while(qp < Q.size() && Q[qp][0] == t){
ans[Q[qp][2]] = a[Q[qp][1] - 1];
++qp;
}
int last = a[0], lastpos = 0;
for(int i = 0; i < n/2; ++i){
if(last < a[i]){
pos[a[lastpos]] = {lastpos, i - 1};
active[a[lastpos]] = 1;
last = a[i], lastpos = i;
}
}
active[a[lastpos]] = 1;
pos[a[lastpos]] = {lastpos, n/2-1};
last = a[n/2], lastpos = n/2;
for(int i = n/2; i < n; ++i){
if(last < a[i]){
pos[a[lastpos]] = {lastpos, i - 1};
active[a[lastpos]] = 1;
last = a[i], lastpos = i;
}
}
active[a[lastpos]] = 1;
pos[a[lastpos]] = {lastpos, n-1};
build(1, n, 1);
while(ok){
++t;
while(qp < Q.size() && (Q[qp][0] == t || !ok)){
ans[Q[qp][2]] = get(1, n, Q[qp][1], 1)[1];
++qp;
}
ok = 0;
auto x = get(1, n, n/2+1, 1);
int lenn = x[2];
while(x[0] > x[1]){
// cout << x[0] << ' ' << x[1] << ' ' << x[2] << ' ' << x[3] << '\n';
ok = 1;
int len = min(to[posarray[x[1]]] - posarray[x[1]] + 1, x[3]);
lenn -= len;
update(1, n, x[0], 1, lenn);
update(1, n, x[1], 1, len);
if(x[3] - len > 0)
x = {x[0], a[posarray[x[1]] + len], lenn, x[3] - len};
else
break;
}
}
while(qp < Q.size()){
ans[Q[qp][2]] = get(1, n, Q[qp][1], 1)[1];
++qp;
}
for(int i = 0; i < q; ++i) cout << ans[i] << '\n';
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message
Main.cpp: In function 'void build(int, int, int)':
Main.cpp:26:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
26 | int m = l+r>>1;
| ~^~
Main.cpp: In function 'std::array<int, 4> get(int, int, int, int)':
Main.cpp:35:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
35 | int m = l+r>>1;
| ~^~
Main.cpp: In function 'void update(int, int, int, int, int)':
Main.cpp:48:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
48 | int m = l+r>>1;
| ~^~
Main.cpp: In function 'void solve()':
Main.cpp:81:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | while(qp < Q.size() && Q[qp][0] == t){
| ~~~^~~~~~~~~~
Main.cpp:110:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | while(qp < Q.size() && (Q[qp][0] == t || !ok)){
| ~~~^~~~~~~~~~
Main.cpp:130:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
130 | while(qp < Q.size()){
| ~~~^~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:141:15: warning: unused variable 'aa' [-Wunused-variable]
141 | int tt = 1, aa;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
302 ms |
27820 KB |
Output is correct |
2 |
Correct |
312 ms |
31324 KB |
Output is correct |
3 |
Correct |
294 ms |
30532 KB |
Output is correct |
4 |
Correct |
286 ms |
29452 KB |
Output is correct |
5 |
Correct |
297 ms |
30996 KB |
Output is correct |
6 |
Correct |
292 ms |
30032 KB |
Output is correct |
7 |
Correct |
296 ms |
31368 KB |
Output is correct |
8 |
Correct |
285 ms |
30548 KB |
Output is correct |
9 |
Correct |
306 ms |
30896 KB |
Output is correct |
10 |
Correct |
290 ms |
30464 KB |
Output is correct |
11 |
Correct |
291 ms |
30332 KB |
Output is correct |
12 |
Correct |
285 ms |
30124 KB |
Output is correct |
13 |
Correct |
295 ms |
30116 KB |
Output is correct |
14 |
Correct |
288 ms |
30960 KB |
Output is correct |
15 |
Correct |
302 ms |
30756 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
263 ms |
29776 KB |
Output is correct |
18 |
Correct |
280 ms |
30248 KB |
Output is correct |
19 |
Correct |
1 ms |
8540 KB |
Output is correct |
20 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
330 ms |
39360 KB |
Output is correct |
2 |
Correct |
303 ms |
52912 KB |
Output is correct |
3 |
Correct |
285 ms |
46916 KB |
Output is correct |
4 |
Correct |
324 ms |
45496 KB |
Output is correct |
5 |
Correct |
292 ms |
46336 KB |
Output is correct |
6 |
Correct |
277 ms |
46388 KB |
Output is correct |
7 |
Correct |
304 ms |
52148 KB |
Output is correct |
8 |
Correct |
292 ms |
50696 KB |
Output is correct |
9 |
Correct |
288 ms |
46628 KB |
Output is correct |
10 |
Correct |
282 ms |
49520 KB |
Output is correct |
11 |
Correct |
266 ms |
44784 KB |
Output is correct |
12 |
Correct |
261 ms |
45236 KB |
Output is correct |
13 |
Correct |
276 ms |
49912 KB |
Output is correct |
14 |
Correct |
276 ms |
46256 KB |
Output is correct |
15 |
Correct |
286 ms |
51116 KB |
Output is correct |
16 |
Correct |
19 ms |
19024 KB |
Output is correct |
17 |
Correct |
239 ms |
48736 KB |
Output is correct |
18 |
Correct |
247 ms |
43040 KB |
Output is correct |
19 |
Correct |
58 ms |
24256 KB |
Output is correct |
20 |
Correct |
71 ms |
25752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
15044 KB |
Output is correct |
2 |
Correct |
51 ms |
16928 KB |
Output is correct |
3 |
Correct |
51 ms |
16832 KB |
Output is correct |
4 |
Correct |
41 ms |
16492 KB |
Output is correct |
5 |
Correct |
50 ms |
16864 KB |
Output is correct |
6 |
Correct |
42 ms |
16492 KB |
Output is correct |
7 |
Correct |
48 ms |
16804 KB |
Output is correct |
8 |
Correct |
44 ms |
16512 KB |
Output is correct |
9 |
Correct |
47 ms |
16740 KB |
Output is correct |
10 |
Correct |
39 ms |
16380 KB |
Output is correct |
11 |
Correct |
45 ms |
16732 KB |
Output is correct |
12 |
Correct |
38 ms |
16580 KB |
Output is correct |
13 |
Correct |
40 ms |
16580 KB |
Output is correct |
14 |
Correct |
39 ms |
16588 KB |
Output is correct |
15 |
Correct |
38 ms |
16476 KB |
Output is correct |
16 |
Correct |
9 ms |
13916 KB |
Output is correct |
17 |
Correct |
35 ms |
16836 KB |
Output is correct |
18 |
Correct |
36 ms |
16068 KB |
Output is correct |
19 |
Correct |
1 ms |
8540 KB |
Output is correct |
20 |
Correct |
1 ms |
8660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
302 ms |
27820 KB |
Output is correct |
2 |
Correct |
312 ms |
31324 KB |
Output is correct |
3 |
Correct |
294 ms |
30532 KB |
Output is correct |
4 |
Correct |
286 ms |
29452 KB |
Output is correct |
5 |
Correct |
297 ms |
30996 KB |
Output is correct |
6 |
Correct |
292 ms |
30032 KB |
Output is correct |
7 |
Correct |
296 ms |
31368 KB |
Output is correct |
8 |
Correct |
285 ms |
30548 KB |
Output is correct |
9 |
Correct |
306 ms |
30896 KB |
Output is correct |
10 |
Correct |
290 ms |
30464 KB |
Output is correct |
11 |
Correct |
291 ms |
30332 KB |
Output is correct |
12 |
Correct |
285 ms |
30124 KB |
Output is correct |
13 |
Correct |
295 ms |
30116 KB |
Output is correct |
14 |
Correct |
288 ms |
30960 KB |
Output is correct |
15 |
Correct |
302 ms |
30756 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
263 ms |
29776 KB |
Output is correct |
18 |
Correct |
280 ms |
30248 KB |
Output is correct |
19 |
Correct |
1 ms |
8540 KB |
Output is correct |
20 |
Correct |
1 ms |
8540 KB |
Output is correct |
21 |
Correct |
330 ms |
39360 KB |
Output is correct |
22 |
Correct |
303 ms |
52912 KB |
Output is correct |
23 |
Correct |
285 ms |
46916 KB |
Output is correct |
24 |
Correct |
324 ms |
45496 KB |
Output is correct |
25 |
Correct |
292 ms |
46336 KB |
Output is correct |
26 |
Correct |
277 ms |
46388 KB |
Output is correct |
27 |
Correct |
304 ms |
52148 KB |
Output is correct |
28 |
Correct |
292 ms |
50696 KB |
Output is correct |
29 |
Correct |
288 ms |
46628 KB |
Output is correct |
30 |
Correct |
282 ms |
49520 KB |
Output is correct |
31 |
Correct |
266 ms |
44784 KB |
Output is correct |
32 |
Correct |
261 ms |
45236 KB |
Output is correct |
33 |
Correct |
276 ms |
49912 KB |
Output is correct |
34 |
Correct |
276 ms |
46256 KB |
Output is correct |
35 |
Correct |
286 ms |
51116 KB |
Output is correct |
36 |
Correct |
19 ms |
19024 KB |
Output is correct |
37 |
Correct |
239 ms |
48736 KB |
Output is correct |
38 |
Correct |
247 ms |
43040 KB |
Output is correct |
39 |
Correct |
58 ms |
24256 KB |
Output is correct |
40 |
Correct |
71 ms |
25752 KB |
Output is correct |
41 |
Correct |
59 ms |
15044 KB |
Output is correct |
42 |
Correct |
51 ms |
16928 KB |
Output is correct |
43 |
Correct |
51 ms |
16832 KB |
Output is correct |
44 |
Correct |
41 ms |
16492 KB |
Output is correct |
45 |
Correct |
50 ms |
16864 KB |
Output is correct |
46 |
Correct |
42 ms |
16492 KB |
Output is correct |
47 |
Correct |
48 ms |
16804 KB |
Output is correct |
48 |
Correct |
44 ms |
16512 KB |
Output is correct |
49 |
Correct |
47 ms |
16740 KB |
Output is correct |
50 |
Correct |
39 ms |
16380 KB |
Output is correct |
51 |
Correct |
45 ms |
16732 KB |
Output is correct |
52 |
Correct |
38 ms |
16580 KB |
Output is correct |
53 |
Correct |
40 ms |
16580 KB |
Output is correct |
54 |
Correct |
39 ms |
16588 KB |
Output is correct |
55 |
Correct |
38 ms |
16476 KB |
Output is correct |
56 |
Correct |
9 ms |
13916 KB |
Output is correct |
57 |
Correct |
35 ms |
16836 KB |
Output is correct |
58 |
Correct |
36 ms |
16068 KB |
Output is correct |
59 |
Correct |
1 ms |
8540 KB |
Output is correct |
60 |
Correct |
1 ms |
8660 KB |
Output is correct |
61 |
Correct |
421 ms |
52640 KB |
Output is correct |
62 |
Correct |
422 ms |
52664 KB |
Output is correct |
63 |
Correct |
406 ms |
51120 KB |
Output is correct |
64 |
Correct |
360 ms |
50608 KB |
Output is correct |
65 |
Correct |
375 ms |
51892 KB |
Output is correct |
66 |
Correct |
359 ms |
50536 KB |
Output is correct |
67 |
Correct |
375 ms |
52012 KB |
Output is correct |
68 |
Correct |
372 ms |
50388 KB |
Output is correct |
69 |
Correct |
364 ms |
50604 KB |
Output is correct |
70 |
Correct |
389 ms |
50000 KB |
Output is correct |
71 |
Correct |
336 ms |
49072 KB |
Output is correct |
72 |
Correct |
345 ms |
49560 KB |
Output is correct |
73 |
Correct |
342 ms |
45744 KB |
Output is correct |
74 |
Correct |
351 ms |
50548 KB |
Output is correct |
75 |
Correct |
361 ms |
50864 KB |
Output is correct |
76 |
Correct |
18 ms |
19028 KB |
Output is correct |
77 |
Correct |
304 ms |
48048 KB |
Output is correct |
78 |
Correct |
308 ms |
47280 KB |
Output is correct |
79 |
Correct |
1 ms |
8540 KB |
Output is correct |
80 |
Correct |
1 ms |
8540 KB |
Output is correct |