#include <bits/stdc++.h>
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;
const int N = 2e5 + 7, Q = 1e6 + 7;
int n, q;
int a[N], a_[N], res[Q], gr[N], fen[N];
pii g[N];
vector <pii> qu[Q];
void add(int x, int val) {for (; x < N; x += x & -x) fen[x] += val;}
int get(int x) {
int out = 0;
for (; x; x -= x & -x) out += fen[x];
return out;
}
int kth(int k) {
int sum = 0, x = 0;
for (int i = 17; i >= 0; i--) {
if (x+(1<<i) >= N) continue;
if (sum + fen[x+(1<<i)] <= k) {x += (1 << i); sum += fen[x];}
}
return x+1;
}
int main () {
FIO;
cin >> n >> q;
for (int i = 0; i < n; i++) cin >> a_[i];
for (int i = 0; i < q; i++) {
int t, x; cin >> t >> x; x--;
qu[min(n, t)].pb({x, i});
}
for (auto x : qu[0]) res[x.S] = a_[x.F];
for (int i = 0, n1 = 0, n2 = 0; i < n; i++) {
if (n2 == n/2 || (n1 < n/2 && a_[n1] < a_[n/2+n2])) a[i] = a_[n1++];
else a[i] = a_[n/2+n2++];
}
for (auto x : qu[1]) res[x.S] = a[x.F];
stack <int> st;
for (int i = 0; i < n; i++) {
while (!st.empty() && a[st.top()] < a[i]) {
gr[a[st.top()]] = i;
st.pop();
}
st.push(i);
}
while (!st.empty()) {
gr[a[st.top()]] = n;
st.pop();
}
for (int i = 0; i < n; i = gr[a[i]]) {
g[a[i]] = {i, gr[a[i]]-1};
add(a[i], gr[a[i]]-i);
}
for (int i = 2; i <= n; i++) {
int x = kth(n/2);
int y = get(x-1);
if (y < n/2) {
int l = g[x].F, r = g[x].S;
add(x, n/2-y-r+l-1);
l += n/2-y;
g[x].S = l-1;
for (int j = l; j <= r; j = gr[a[j]]) {
add(a[j], min(r+1, gr[a[j]])-j);
g[a[j]] = {j, min(r, gr[a[j]]-1)};
}
}
for (auto z : qu[i]) {
int h = kth(z.F);
res[z.S] = a[g[h].F+z.F-get(h-1)];
}
}
for (int i = 0; i < q; i++) cout << res[i] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
175 ms |
64716 KB |
Output is correct |
2 |
Correct |
172 ms |
62328 KB |
Output is correct |
3 |
Correct |
190 ms |
62032 KB |
Output is correct |
4 |
Correct |
154 ms |
59216 KB |
Output is correct |
5 |
Correct |
183 ms |
65208 KB |
Output is correct |
6 |
Correct |
159 ms |
62884 KB |
Output is correct |
7 |
Correct |
179 ms |
67512 KB |
Output is correct |
8 |
Correct |
158 ms |
61028 KB |
Output is correct |
9 |
Correct |
158 ms |
60168 KB |
Output is correct |
10 |
Correct |
161 ms |
60400 KB |
Output is correct |
11 |
Correct |
175 ms |
60212 KB |
Output is correct |
12 |
Correct |
137 ms |
58956 KB |
Output is correct |
13 |
Correct |
159 ms |
58760 KB |
Output is correct |
14 |
Correct |
161 ms |
62844 KB |
Output is correct |
15 |
Correct |
169 ms |
59848 KB |
Output is correct |
16 |
Correct |
4 ms |
31324 KB |
Output is correct |
17 |
Correct |
105 ms |
58240 KB |
Output is correct |
18 |
Correct |
114 ms |
58000 KB |
Output is correct |
19 |
Correct |
4 ms |
31320 KB |
Output is correct |
20 |
Correct |
4 ms |
31324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
207 ms |
71088 KB |
Output is correct |
2 |
Correct |
208 ms |
69808 KB |
Output is correct |
3 |
Correct |
168 ms |
64176 KB |
Output is correct |
4 |
Correct |
169 ms |
62384 KB |
Output is correct |
5 |
Correct |
163 ms |
63140 KB |
Output is correct |
6 |
Correct |
168 ms |
63664 KB |
Output is correct |
7 |
Correct |
204 ms |
69768 KB |
Output is correct |
8 |
Correct |
217 ms |
68376 KB |
Output is correct |
9 |
Correct |
176 ms |
64496 KB |
Output is correct |
10 |
Correct |
197 ms |
67504 KB |
Output is correct |
11 |
Correct |
147 ms |
62640 KB |
Output is correct |
12 |
Correct |
160 ms |
61356 KB |
Output is correct |
13 |
Correct |
188 ms |
66220 KB |
Output is correct |
14 |
Correct |
151 ms |
61796 KB |
Output is correct |
15 |
Correct |
195 ms |
67904 KB |
Output is correct |
16 |
Correct |
23 ms |
33112 KB |
Output is correct |
17 |
Correct |
136 ms |
64988 KB |
Output is correct |
18 |
Correct |
152 ms |
59820 KB |
Output is correct |
19 |
Correct |
52 ms |
41748 KB |
Output is correct |
20 |
Correct |
64 ms |
43708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
36692 KB |
Output is correct |
2 |
Correct |
42 ms |
36592 KB |
Output is correct |
3 |
Correct |
48 ms |
36400 KB |
Output is correct |
4 |
Correct |
36 ms |
36180 KB |
Output is correct |
5 |
Correct |
44 ms |
36508 KB |
Output is correct |
6 |
Correct |
39 ms |
36176 KB |
Output is correct |
7 |
Correct |
42 ms |
36500 KB |
Output is correct |
8 |
Correct |
40 ms |
36176 KB |
Output is correct |
9 |
Correct |
40 ms |
36180 KB |
Output is correct |
10 |
Correct |
32 ms |
35924 KB |
Output is correct |
11 |
Correct |
35 ms |
36436 KB |
Output is correct |
12 |
Correct |
35 ms |
36248 KB |
Output is correct |
13 |
Correct |
34 ms |
35876 KB |
Output is correct |
14 |
Correct |
35 ms |
36252 KB |
Output is correct |
15 |
Correct |
38 ms |
35848 KB |
Output is correct |
16 |
Correct |
16 ms |
32164 KB |
Output is correct |
17 |
Correct |
27 ms |
35028 KB |
Output is correct |
18 |
Correct |
25 ms |
35016 KB |
Output is correct |
19 |
Correct |
4 ms |
31324 KB |
Output is correct |
20 |
Correct |
4 ms |
31324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
175 ms |
64716 KB |
Output is correct |
2 |
Correct |
172 ms |
62328 KB |
Output is correct |
3 |
Correct |
190 ms |
62032 KB |
Output is correct |
4 |
Correct |
154 ms |
59216 KB |
Output is correct |
5 |
Correct |
183 ms |
65208 KB |
Output is correct |
6 |
Correct |
159 ms |
62884 KB |
Output is correct |
7 |
Correct |
179 ms |
67512 KB |
Output is correct |
8 |
Correct |
158 ms |
61028 KB |
Output is correct |
9 |
Correct |
158 ms |
60168 KB |
Output is correct |
10 |
Correct |
161 ms |
60400 KB |
Output is correct |
11 |
Correct |
175 ms |
60212 KB |
Output is correct |
12 |
Correct |
137 ms |
58956 KB |
Output is correct |
13 |
Correct |
159 ms |
58760 KB |
Output is correct |
14 |
Correct |
161 ms |
62844 KB |
Output is correct |
15 |
Correct |
169 ms |
59848 KB |
Output is correct |
16 |
Correct |
4 ms |
31324 KB |
Output is correct |
17 |
Correct |
105 ms |
58240 KB |
Output is correct |
18 |
Correct |
114 ms |
58000 KB |
Output is correct |
19 |
Correct |
4 ms |
31320 KB |
Output is correct |
20 |
Correct |
4 ms |
31324 KB |
Output is correct |
21 |
Correct |
207 ms |
71088 KB |
Output is correct |
22 |
Correct |
208 ms |
69808 KB |
Output is correct |
23 |
Correct |
168 ms |
64176 KB |
Output is correct |
24 |
Correct |
169 ms |
62384 KB |
Output is correct |
25 |
Correct |
163 ms |
63140 KB |
Output is correct |
26 |
Correct |
168 ms |
63664 KB |
Output is correct |
27 |
Correct |
204 ms |
69768 KB |
Output is correct |
28 |
Correct |
217 ms |
68376 KB |
Output is correct |
29 |
Correct |
176 ms |
64496 KB |
Output is correct |
30 |
Correct |
197 ms |
67504 KB |
Output is correct |
31 |
Correct |
147 ms |
62640 KB |
Output is correct |
32 |
Correct |
160 ms |
61356 KB |
Output is correct |
33 |
Correct |
188 ms |
66220 KB |
Output is correct |
34 |
Correct |
151 ms |
61796 KB |
Output is correct |
35 |
Correct |
195 ms |
67904 KB |
Output is correct |
36 |
Correct |
23 ms |
33112 KB |
Output is correct |
37 |
Correct |
136 ms |
64988 KB |
Output is correct |
38 |
Correct |
152 ms |
59820 KB |
Output is correct |
39 |
Correct |
52 ms |
41748 KB |
Output is correct |
40 |
Correct |
64 ms |
43708 KB |
Output is correct |
41 |
Correct |
47 ms |
36692 KB |
Output is correct |
42 |
Correct |
42 ms |
36592 KB |
Output is correct |
43 |
Correct |
48 ms |
36400 KB |
Output is correct |
44 |
Correct |
36 ms |
36180 KB |
Output is correct |
45 |
Correct |
44 ms |
36508 KB |
Output is correct |
46 |
Correct |
39 ms |
36176 KB |
Output is correct |
47 |
Correct |
42 ms |
36500 KB |
Output is correct |
48 |
Correct |
40 ms |
36176 KB |
Output is correct |
49 |
Correct |
40 ms |
36180 KB |
Output is correct |
50 |
Correct |
32 ms |
35924 KB |
Output is correct |
51 |
Correct |
35 ms |
36436 KB |
Output is correct |
52 |
Correct |
35 ms |
36248 KB |
Output is correct |
53 |
Correct |
34 ms |
35876 KB |
Output is correct |
54 |
Correct |
35 ms |
36252 KB |
Output is correct |
55 |
Correct |
38 ms |
35848 KB |
Output is correct |
56 |
Correct |
16 ms |
32164 KB |
Output is correct |
57 |
Correct |
27 ms |
35028 KB |
Output is correct |
58 |
Correct |
25 ms |
35016 KB |
Output is correct |
59 |
Correct |
4 ms |
31324 KB |
Output is correct |
60 |
Correct |
4 ms |
31324 KB |
Output is correct |
61 |
Correct |
393 ms |
80540 KB |
Output is correct |
62 |
Correct |
336 ms |
79824 KB |
Output is correct |
63 |
Correct |
351 ms |
78288 KB |
Output is correct |
64 |
Correct |
270 ms |
78032 KB |
Output is correct |
65 |
Correct |
276 ms |
80336 KB |
Output is correct |
66 |
Correct |
278 ms |
78000 KB |
Output is correct |
67 |
Correct |
247 ms |
76752 KB |
Output is correct |
68 |
Correct |
254 ms |
74956 KB |
Output is correct |
69 |
Correct |
267 ms |
78544 KB |
Output is correct |
70 |
Correct |
234 ms |
72260 KB |
Output is correct |
71 |
Correct |
210 ms |
74864 KB |
Output is correct |
72 |
Correct |
263 ms |
73164 KB |
Output is correct |
73 |
Correct |
218 ms |
74192 KB |
Output is correct |
74 |
Correct |
230 ms |
77760 KB |
Output is correct |
75 |
Correct |
247 ms |
73760 KB |
Output is correct |
76 |
Correct |
26 ms |
33184 KB |
Output is correct |
77 |
Correct |
147 ms |
65712 KB |
Output is correct |
78 |
Correct |
144 ms |
65428 KB |
Output is correct |
79 |
Correct |
4 ms |
31324 KB |
Output is correct |
80 |
Correct |
3 ms |
31324 KB |
Output is correct |