#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const int M = 1e6+2;
int n, m, bit, p[200002], dr[200002];
pair<int, int>interval[200002];
int aib[200002];
int ans[M];
struct queries{
int p, t, i;
bool operator < (const queries &other) {
return t < other.t;
}
} q[M];
void update (int x, int val) {
for (int i = x; i <= n; i += (i & -i)) {
aib[i] += val;
}
}
int query (int x) {
int s = 0;
for (int i = x; i; i -= (i & -i))
s += aib[i];
return s;
}
int binary (int x) {
int poz = 0;
int s = 0;
for (int i = (1 << bit); i; (i >>= 1)) {
if (poz + i <= n && s + aib[poz + i] < x) {
s += aib[poz + i];
poz += i;
}
}
return poz + 1;
}
int main ()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
for (int i = 1; i <= m; i++) {
cin >> q[i].t >> q[i].p;
q[i].i = i;
}
while ((1 << bit) <= n)
bit++;
bit--;
sort(q + 1,q + m + 1);
int ind = 1;
while (ind <= m && q[ind].t == 0) {
ans[q[ind].i] = p[q[ind].p];
ind++;
}
stack<int>st;
st.push(n + 1);
p[n + 1] = n + 1;
for (int i = n; i >= 1; i--) {
while (p[st.top()] < p[i])
st.pop();
dr[i] = st.top();
st.push(i);
}
int j = 1;
while (j <= n) {
if (j <= n / 2) {
int l = j, r = min(n / 2, dr[j] - 1);
update(p[j], r - l + 1);
interval[p[j]] = {l, r};
j = r + 1;
}
else {
update(p[j], dr[j] - j);
interval[p[j]] = {j, dr[j] - 1};
j = dr[j];
}
}
int time = 0;
while (1) {
time++;
int val = binary(n / 2);
int sum = query(val);
while (ind <= m && q[ind].t == time) {
int poz = binary(q[ind].p);
int sum2 = query(poz - 1);
ans[q[ind].i] = p[interval[poz].first + q[ind].p - sum2 - 1];
ind++;
}
if (sum == n / 2)
break;
int overflow = sum - n / 2;
update(val, -overflow);
interval[val].second -= overflow;
int j = interval[val].second + 1;
while (j <= interval[val].second + overflow) {
update(p[j], min(interval[val].second + overflow + 1, dr[j]) - j);
interval[p[j]] = {j, min(interval[val].second + overflow + 1, dr[j]) - 1};
j = min(interval[val].second + overflow + 1, dr[j]);
}
}
while (ind <= m) {
int poz = binary(q[ind].p);
int sum2 = query(poz - 1);
ans[q[ind].i] = p[interval[poz].first + q[ind].p - sum2 - 1];
ind++;
}
for (int i = 1; i <= m; i++)
cout << ans[i] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
278 ms |
19916 KB |
Output is correct |
2 |
Correct |
293 ms |
23500 KB |
Output is correct |
3 |
Correct |
282 ms |
22520 KB |
Output is correct |
4 |
Correct |
247 ms |
25036 KB |
Output is correct |
5 |
Correct |
288 ms |
25576 KB |
Output is correct |
6 |
Correct |
265 ms |
24496 KB |
Output is correct |
7 |
Correct |
282 ms |
24568 KB |
Output is correct |
8 |
Correct |
262 ms |
24000 KB |
Output is correct |
9 |
Correct |
256 ms |
24148 KB |
Output is correct |
10 |
Correct |
253 ms |
24056 KB |
Output is correct |
11 |
Correct |
262 ms |
23996 KB |
Output is correct |
12 |
Correct |
231 ms |
23440 KB |
Output is correct |
13 |
Correct |
256 ms |
23572 KB |
Output is correct |
14 |
Correct |
264 ms |
25824 KB |
Output is correct |
15 |
Correct |
256 ms |
26032 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
188 ms |
24504 KB |
Output is correct |
18 |
Correct |
207 ms |
24380 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
378 ms |
26256 KB |
Output is correct |
2 |
Correct |
370 ms |
39612 KB |
Output is correct |
3 |
Correct |
287 ms |
32204 KB |
Output is correct |
4 |
Correct |
288 ms |
29492 KB |
Output is correct |
5 |
Correct |
277 ms |
29848 KB |
Output is correct |
6 |
Correct |
270 ms |
30908 KB |
Output is correct |
7 |
Correct |
331 ms |
34252 KB |
Output is correct |
8 |
Correct |
336 ms |
35604 KB |
Output is correct |
9 |
Correct |
271 ms |
29492 KB |
Output is correct |
10 |
Correct |
322 ms |
33876 KB |
Output is correct |
11 |
Correct |
253 ms |
27596 KB |
Output is correct |
12 |
Correct |
264 ms |
27044 KB |
Output is correct |
13 |
Correct |
308 ms |
33784 KB |
Output is correct |
14 |
Correct |
275 ms |
31404 KB |
Output is correct |
15 |
Correct |
329 ms |
37164 KB |
Output is correct |
16 |
Correct |
20 ms |
5068 KB |
Output is correct |
17 |
Correct |
225 ms |
32760 KB |
Output is correct |
18 |
Correct |
246 ms |
23848 KB |
Output is correct |
19 |
Correct |
62 ms |
10080 KB |
Output is correct |
20 |
Correct |
92 ms |
11228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
4384 KB |
Output is correct |
2 |
Correct |
55 ms |
6032 KB |
Output is correct |
3 |
Correct |
55 ms |
5964 KB |
Output is correct |
4 |
Correct |
45 ms |
5580 KB |
Output is correct |
5 |
Correct |
50 ms |
5328 KB |
Output is correct |
6 |
Correct |
45 ms |
5636 KB |
Output is correct |
7 |
Correct |
51 ms |
5064 KB |
Output is correct |
8 |
Correct |
47 ms |
5368 KB |
Output is correct |
9 |
Correct |
49 ms |
5812 KB |
Output is correct |
10 |
Correct |
41 ms |
5676 KB |
Output is correct |
11 |
Correct |
43 ms |
5196 KB |
Output is correct |
12 |
Correct |
41 ms |
5636 KB |
Output is correct |
13 |
Correct |
43 ms |
5604 KB |
Output is correct |
14 |
Correct |
43 ms |
5924 KB |
Output is correct |
15 |
Correct |
42 ms |
5152 KB |
Output is correct |
16 |
Correct |
9 ms |
2504 KB |
Output is correct |
17 |
Correct |
30 ms |
5452 KB |
Output is correct |
18 |
Correct |
34 ms |
4172 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
278 ms |
19916 KB |
Output is correct |
2 |
Correct |
293 ms |
23500 KB |
Output is correct |
3 |
Correct |
282 ms |
22520 KB |
Output is correct |
4 |
Correct |
247 ms |
25036 KB |
Output is correct |
5 |
Correct |
288 ms |
25576 KB |
Output is correct |
6 |
Correct |
265 ms |
24496 KB |
Output is correct |
7 |
Correct |
282 ms |
24568 KB |
Output is correct |
8 |
Correct |
262 ms |
24000 KB |
Output is correct |
9 |
Correct |
256 ms |
24148 KB |
Output is correct |
10 |
Correct |
253 ms |
24056 KB |
Output is correct |
11 |
Correct |
262 ms |
23996 KB |
Output is correct |
12 |
Correct |
231 ms |
23440 KB |
Output is correct |
13 |
Correct |
256 ms |
23572 KB |
Output is correct |
14 |
Correct |
264 ms |
25824 KB |
Output is correct |
15 |
Correct |
256 ms |
26032 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
188 ms |
24504 KB |
Output is correct |
18 |
Correct |
207 ms |
24380 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
378 ms |
26256 KB |
Output is correct |
22 |
Correct |
370 ms |
39612 KB |
Output is correct |
23 |
Correct |
287 ms |
32204 KB |
Output is correct |
24 |
Correct |
288 ms |
29492 KB |
Output is correct |
25 |
Correct |
277 ms |
29848 KB |
Output is correct |
26 |
Correct |
270 ms |
30908 KB |
Output is correct |
27 |
Correct |
331 ms |
34252 KB |
Output is correct |
28 |
Correct |
336 ms |
35604 KB |
Output is correct |
29 |
Correct |
271 ms |
29492 KB |
Output is correct |
30 |
Correct |
322 ms |
33876 KB |
Output is correct |
31 |
Correct |
253 ms |
27596 KB |
Output is correct |
32 |
Correct |
264 ms |
27044 KB |
Output is correct |
33 |
Correct |
308 ms |
33784 KB |
Output is correct |
34 |
Correct |
275 ms |
31404 KB |
Output is correct |
35 |
Correct |
329 ms |
37164 KB |
Output is correct |
36 |
Correct |
20 ms |
5068 KB |
Output is correct |
37 |
Correct |
225 ms |
32760 KB |
Output is correct |
38 |
Correct |
246 ms |
23848 KB |
Output is correct |
39 |
Correct |
62 ms |
10080 KB |
Output is correct |
40 |
Correct |
92 ms |
11228 KB |
Output is correct |
41 |
Correct |
59 ms |
4384 KB |
Output is correct |
42 |
Correct |
55 ms |
6032 KB |
Output is correct |
43 |
Correct |
55 ms |
5964 KB |
Output is correct |
44 |
Correct |
45 ms |
5580 KB |
Output is correct |
45 |
Correct |
50 ms |
5328 KB |
Output is correct |
46 |
Correct |
45 ms |
5636 KB |
Output is correct |
47 |
Correct |
51 ms |
5064 KB |
Output is correct |
48 |
Correct |
47 ms |
5368 KB |
Output is correct |
49 |
Correct |
49 ms |
5812 KB |
Output is correct |
50 |
Correct |
41 ms |
5676 KB |
Output is correct |
51 |
Correct |
43 ms |
5196 KB |
Output is correct |
52 |
Correct |
41 ms |
5636 KB |
Output is correct |
53 |
Correct |
43 ms |
5604 KB |
Output is correct |
54 |
Correct |
43 ms |
5924 KB |
Output is correct |
55 |
Correct |
42 ms |
5152 KB |
Output is correct |
56 |
Correct |
9 ms |
2504 KB |
Output is correct |
57 |
Correct |
30 ms |
5452 KB |
Output is correct |
58 |
Correct |
34 ms |
4172 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
509 ms |
36440 KB |
Output is correct |
62 |
Correct |
503 ms |
37576 KB |
Output is correct |
63 |
Correct |
473 ms |
34252 KB |
Output is correct |
64 |
Correct |
416 ms |
33848 KB |
Output is correct |
65 |
Correct |
422 ms |
36812 KB |
Output is correct |
66 |
Correct |
411 ms |
34508 KB |
Output is correct |
67 |
Correct |
398 ms |
35488 KB |
Output is correct |
68 |
Correct |
392 ms |
35008 KB |
Output is correct |
69 |
Correct |
401 ms |
33812 KB |
Output is correct |
70 |
Correct |
405 ms |
34896 KB |
Output is correct |
71 |
Correct |
363 ms |
34156 KB |
Output is correct |
72 |
Correct |
386 ms |
32272 KB |
Output is correct |
73 |
Correct |
372 ms |
32952 KB |
Output is correct |
74 |
Correct |
383 ms |
34708 KB |
Output is correct |
75 |
Correct |
403 ms |
33332 KB |
Output is correct |
76 |
Correct |
20 ms |
4552 KB |
Output is correct |
77 |
Correct |
245 ms |
31584 KB |
Output is correct |
78 |
Correct |
294 ms |
30096 KB |
Output is correct |
79 |
Correct |
1 ms |
332 KB |
Output is correct |
80 |
Correct |
0 ms |
340 KB |
Output is correct |