#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
using namespace std;
#ifdef LOCAL
#define eprintf(...) {fprintf(stderr, __VA_ARGS__);fflush(stderr);}
#else
#define eprintf(...) 42
#endif
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template<typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
return (ull)rng() % B;
}
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
clock_t startTime;
double getCurrentTime() {
return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
const int N = 200200;
const int Q = 1000100;
int n, q;
int a[N];
int pos[N];
vector<pair<int, int>> Qs[N];
int ANS[Q];
int r[N];
set<int> setik;
set<int> set_pos;
struct Node {
int l, r, sum;
Node() : sum(0) {}
Node(int _l, int _r, int _sum) : l(_l), r(_r), sum(_sum) {}
};
const int M = (1 << 22);
Node tree[4 * M + 5];
Node merge(Node L, Node R) {
return Node(L.l, R.r, L.sum + R.sum);
}
void build(int v, int l, int r) {
tree[v] = Node(l, r, 0);
if (l == r) return;
int mid = l + r >> 1;
build(v * 2, l, mid);
build(v * 2 + 1, mid + 1, r);
}
void update(int p, int i, int v) {
if (tree[p].l > i || tree[p].r < i) return;
if (tree[p].l == tree[p].r) {
tree[p].sum += v;
return;
}
update(p * 2, i, v);
update(p * 2 + 1, i, v);
tree[p].sum = tree[p * 2].sum + tree[p * 2 + 1].sum;
}
int getPos(int p, int v) {
// eprintf("segtree: %d %d %d\n", tree[p].l, tree[p].r, v);
if (tree[p].l == tree[p].r) return tree[p].l;
if (tree[p * 2].sum < v)
return getPos(p * 2 + 1, v - tree[p * 2].sum);
else
return getPos(p * 2, v);
}
int getSum(int p, int r) {
if (tree[p].r <= r) return tree[p].sum;
if (tree[p].l > r) return 0;
return getSum(p * 2, r) + getSum(p * 2 + 1, r);
}
void add(int x, int l) {
// eprintf("adding %d %d\n", x, l);
setik.insert(x);
set_pos.insert(pos[x]);
update(1, x, l);
}
void go(int i) {
i = pos[i];
// eprintf("OMFG GO FROM %d\n", i);
while(i <= n && setik.find(a[i]) == setik.end()) {
int nxt = r[i];
auto it = set_pos.lower_bound(i);
if (it != set_pos.end() && *it >= i)
nxt = min(nxt, *it);
add(a[i], nxt - i);
i = nxt;
}
}
int main()
{
startTime = clock();
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
pos[a[i]] = i;
}
vector<int> stk(1, n + 1);
for (int i = n; i >= 1; i--) {
while(stk.size() > 1 && a[stk.back()] <= a[i])
stk.pop_back();
r[i] = stk.back();
stk.push_back(i);
}
build(1, 0, M - 1);
go(a[1]);
for (int i = 0; i < q; i++) {
int t, x;
scanf("%d%d", &t, &x);
Qs[min(t, n)].push_back(mp(x, i));
}
for (int i = 0; i <= n; i++) {
for (auto& p : Qs[i]) {
int el = getPos(1, p.first);
// eprintf("p.first = %d el = %d\n", p.first, el);
// if (p.first == 5)
// eprintf("hIIIIIII sz = %d\n", setik.size());
ANS[p.second] = a[pos[el] + p.first - getSum(1, el - 1) - 1];
assert(ANS[p.second] <= el);
}
int id = getPos(1, n / 2 + 1);
// eprintf("ID = %d\n", id);
if (getSum(1, id - 1) == n / 2)
continue;
int el = a[pos[id] + n / 2 - getSum(1, id - 1)];
int new_v = n / 2 - getSum(1, id - 1) - (getSum(1, id) - getSum(1, id - 1));
// eprintf("WTF %d %d and %d\n", id, new_v, el);
update(1, id, new_v);
go(el);
}
for (int i = 0; i < q; i++)
printf("%d\n", ANS[i]);
return 0;
}
Compilation message
Main.cpp: In function 'void build(int, int, int)':
Main.cpp:80:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
80 | int mid = l + r >> 1;
| ~~^~~
Main.cpp: In function 'int main()':
Main.cpp:132:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
132 | scanf("%d%d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:134:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
134 | scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
Main.cpp:148:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
148 | scanf("%d%d", &t, &x);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
485 ms |
220600 KB |
Output is correct |
2 |
Correct |
491 ms |
219340 KB |
Output is correct |
3 |
Correct |
460 ms |
218848 KB |
Output is correct |
4 |
Correct |
391 ms |
217868 KB |
Output is correct |
5 |
Correct |
451 ms |
220872 KB |
Output is correct |
6 |
Correct |
377 ms |
221548 KB |
Output is correct |
7 |
Correct |
443 ms |
221844 KB |
Output is correct |
8 |
Correct |
399 ms |
219828 KB |
Output is correct |
9 |
Correct |
405 ms |
219496 KB |
Output is correct |
10 |
Correct |
410 ms |
218908 KB |
Output is correct |
11 |
Correct |
400 ms |
219192 KB |
Output is correct |
12 |
Correct |
423 ms |
217716 KB |
Output is correct |
13 |
Correct |
414 ms |
218380 KB |
Output is correct |
14 |
Correct |
430 ms |
220728 KB |
Output is correct |
15 |
Correct |
420 ms |
219016 KB |
Output is correct |
16 |
Correct |
109 ms |
201968 KB |
Output is correct |
17 |
Correct |
472 ms |
217812 KB |
Output is correct |
18 |
Correct |
375 ms |
217560 KB |
Output is correct |
19 |
Correct |
99 ms |
201892 KB |
Output is correct |
20 |
Correct |
118 ms |
202028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
780 ms |
242412 KB |
Output is correct |
2 |
Correct |
719 ms |
242380 KB |
Output is correct |
3 |
Correct |
641 ms |
237604 KB |
Output is correct |
4 |
Correct |
489 ms |
223676 KB |
Output is correct |
5 |
Correct |
545 ms |
224576 KB |
Output is correct |
6 |
Correct |
494 ms |
223496 KB |
Output is correct |
7 |
Correct |
526 ms |
225360 KB |
Output is correct |
8 |
Correct |
518 ms |
225796 KB |
Output is correct |
9 |
Correct |
512 ms |
223448 KB |
Output is correct |
10 |
Correct |
448 ms |
222768 KB |
Output is correct |
11 |
Correct |
401 ms |
219400 KB |
Output is correct |
12 |
Correct |
427 ms |
219736 KB |
Output is correct |
13 |
Correct |
502 ms |
222508 KB |
Output is correct |
14 |
Correct |
399 ms |
219800 KB |
Output is correct |
15 |
Correct |
441 ms |
222992 KB |
Output is correct |
16 |
Correct |
137 ms |
204828 KB |
Output is correct |
17 |
Correct |
665 ms |
241100 KB |
Output is correct |
18 |
Correct |
370 ms |
218880 KB |
Output is correct |
19 |
Correct |
196 ms |
208508 KB |
Output is correct |
20 |
Correct |
209 ms |
208604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
303 ms |
216428 KB |
Output is correct |
2 |
Correct |
267 ms |
215916 KB |
Output is correct |
3 |
Correct |
275 ms |
215480 KB |
Output is correct |
4 |
Correct |
172 ms |
206848 KB |
Output is correct |
5 |
Correct |
250 ms |
210496 KB |
Output is correct |
6 |
Correct |
189 ms |
207188 KB |
Output is correct |
7 |
Correct |
218 ms |
209100 KB |
Output is correct |
8 |
Correct |
192 ms |
208316 KB |
Output is correct |
9 |
Correct |
230 ms |
209296 KB |
Output is correct |
10 |
Correct |
152 ms |
205948 KB |
Output is correct |
11 |
Correct |
162 ms |
206252 KB |
Output is correct |
12 |
Correct |
151 ms |
205844 KB |
Output is correct |
13 |
Correct |
159 ms |
205964 KB |
Output is correct |
14 |
Correct |
158 ms |
205952 KB |
Output is correct |
15 |
Correct |
152 ms |
205796 KB |
Output is correct |
16 |
Correct |
125 ms |
203624 KB |
Output is correct |
17 |
Correct |
253 ms |
214856 KB |
Output is correct |
18 |
Correct |
144 ms |
205476 KB |
Output is correct |
19 |
Correct |
98 ms |
201932 KB |
Output is correct |
20 |
Correct |
98 ms |
201968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
485 ms |
220600 KB |
Output is correct |
2 |
Correct |
491 ms |
219340 KB |
Output is correct |
3 |
Correct |
460 ms |
218848 KB |
Output is correct |
4 |
Correct |
391 ms |
217868 KB |
Output is correct |
5 |
Correct |
451 ms |
220872 KB |
Output is correct |
6 |
Correct |
377 ms |
221548 KB |
Output is correct |
7 |
Correct |
443 ms |
221844 KB |
Output is correct |
8 |
Correct |
399 ms |
219828 KB |
Output is correct |
9 |
Correct |
405 ms |
219496 KB |
Output is correct |
10 |
Correct |
410 ms |
218908 KB |
Output is correct |
11 |
Correct |
400 ms |
219192 KB |
Output is correct |
12 |
Correct |
423 ms |
217716 KB |
Output is correct |
13 |
Correct |
414 ms |
218380 KB |
Output is correct |
14 |
Correct |
430 ms |
220728 KB |
Output is correct |
15 |
Correct |
420 ms |
219016 KB |
Output is correct |
16 |
Correct |
109 ms |
201968 KB |
Output is correct |
17 |
Correct |
472 ms |
217812 KB |
Output is correct |
18 |
Correct |
375 ms |
217560 KB |
Output is correct |
19 |
Correct |
99 ms |
201892 KB |
Output is correct |
20 |
Correct |
118 ms |
202028 KB |
Output is correct |
21 |
Correct |
780 ms |
242412 KB |
Output is correct |
22 |
Correct |
719 ms |
242380 KB |
Output is correct |
23 |
Correct |
641 ms |
237604 KB |
Output is correct |
24 |
Correct |
489 ms |
223676 KB |
Output is correct |
25 |
Correct |
545 ms |
224576 KB |
Output is correct |
26 |
Correct |
494 ms |
223496 KB |
Output is correct |
27 |
Correct |
526 ms |
225360 KB |
Output is correct |
28 |
Correct |
518 ms |
225796 KB |
Output is correct |
29 |
Correct |
512 ms |
223448 KB |
Output is correct |
30 |
Correct |
448 ms |
222768 KB |
Output is correct |
31 |
Correct |
401 ms |
219400 KB |
Output is correct |
32 |
Correct |
427 ms |
219736 KB |
Output is correct |
33 |
Correct |
502 ms |
222508 KB |
Output is correct |
34 |
Correct |
399 ms |
219800 KB |
Output is correct |
35 |
Correct |
441 ms |
222992 KB |
Output is correct |
36 |
Correct |
137 ms |
204828 KB |
Output is correct |
37 |
Correct |
665 ms |
241100 KB |
Output is correct |
38 |
Correct |
370 ms |
218880 KB |
Output is correct |
39 |
Correct |
196 ms |
208508 KB |
Output is correct |
40 |
Correct |
209 ms |
208604 KB |
Output is correct |
41 |
Correct |
303 ms |
216428 KB |
Output is correct |
42 |
Correct |
267 ms |
215916 KB |
Output is correct |
43 |
Correct |
275 ms |
215480 KB |
Output is correct |
44 |
Correct |
172 ms |
206848 KB |
Output is correct |
45 |
Correct |
250 ms |
210496 KB |
Output is correct |
46 |
Correct |
189 ms |
207188 KB |
Output is correct |
47 |
Correct |
218 ms |
209100 KB |
Output is correct |
48 |
Correct |
192 ms |
208316 KB |
Output is correct |
49 |
Correct |
230 ms |
209296 KB |
Output is correct |
50 |
Correct |
152 ms |
205948 KB |
Output is correct |
51 |
Correct |
162 ms |
206252 KB |
Output is correct |
52 |
Correct |
151 ms |
205844 KB |
Output is correct |
53 |
Correct |
159 ms |
205964 KB |
Output is correct |
54 |
Correct |
158 ms |
205952 KB |
Output is correct |
55 |
Correct |
152 ms |
205796 KB |
Output is correct |
56 |
Correct |
125 ms |
203624 KB |
Output is correct |
57 |
Correct |
253 ms |
214856 KB |
Output is correct |
58 |
Correct |
144 ms |
205476 KB |
Output is correct |
59 |
Correct |
98 ms |
201932 KB |
Output is correct |
60 |
Correct |
98 ms |
201968 KB |
Output is correct |
61 |
Correct |
1163 ms |
248112 KB |
Output is correct |
62 |
Correct |
1083 ms |
246752 KB |
Output is correct |
63 |
Correct |
1122 ms |
245660 KB |
Output is correct |
64 |
Correct |
750 ms |
231144 KB |
Output is correct |
65 |
Correct |
853 ms |
232864 KB |
Output is correct |
66 |
Correct |
746 ms |
231192 KB |
Output is correct |
67 |
Correct |
642 ms |
228280 KB |
Output is correct |
68 |
Correct |
678 ms |
228352 KB |
Output is correct |
69 |
Correct |
734 ms |
230548 KB |
Output is correct |
70 |
Correct |
589 ms |
224632 KB |
Output is correct |
71 |
Correct |
503 ms |
226824 KB |
Output is correct |
72 |
Correct |
560 ms |
224848 KB |
Output is correct |
73 |
Correct |
531 ms |
225740 KB |
Output is correct |
74 |
Correct |
545 ms |
227020 KB |
Output is correct |
75 |
Correct |
601 ms |
226060 KB |
Output is correct |
76 |
Correct |
138 ms |
204364 KB |
Output is correct |
77 |
Correct |
927 ms |
240676 KB |
Output is correct |
78 |
Correct |
438 ms |
221752 KB |
Output is correct |
79 |
Correct |
94 ms |
201932 KB |
Output is correct |
80 |
Correct |
94 ms |
201992 KB |
Output is correct |