#include <bits/stdc++.h>
#define se second
#define fs first
#define mp make_pair
#define pb push_back
#define ll long long
#define ii pair<ll,ll>
#define ld long double
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }
const int N = 2e5 + 7;
const int Mod = 1e9 + 7;
const int szBL = 240;
const int INF = 1e9 + 7;
const int BASE = 137;
struct fenwick_Tree {
int m;
int fen[N];
void init (int n) {
m = n;
}
void update (int pos, int val) {
for (;pos <= m; pos += pos & -pos) fen[pos] += val;
}
int get (int pos) {
int res = 0;
for (;pos > 0; pos -= pos & -pos) res += fen[pos];
return res;
}
}fen;
int n, Q;
int a[N];
pair<int,int> itv[N];
vector<pair<int,int>> qr[N];
int spt[N][25];
int Ans[(int)1e6 + 7];
void solution () {
cin >> n >> Q;
rep (i, 1, n) cin >> a[i];
fen.init(n);
rep (i, 1, n) spt[i][0] = a[i];
for (int j = 1; (1 << j) <= n; ++j)
for (int i = 1; i + (1 << j) - 1 <= n; i++)
spt[i][j] = max(spt[i][j - 1], spt[i + (1 << j - 1)][j - 1]);
auto get = [] (int L, int R) {
assert(L <= R);
int K = 31 - __builtin_clz(R - L + 1);
return max(spt[L][K], spt[R - (1 << K) + 1][K]);
};
auto get_bound = [&] (int pos, int R) {
int lf = pos, rt = R;
while (lf < rt) {
int mid = lf + rt + 1 >> 1;
if (get(pos, mid) == a[pos]) lf = mid;
else rt = mid - 1;
}
return lf;
};
rep (i, 1, n) {
int bound = get_bound(i, n);
itv[a[i]] = {i, bound};
fen.update (a[i], bound - i + 1);
i = bound;
}
rep (i, 1, Q) {
int T, p;
cin >> T >> p;
T = min(T, n);
qr[T].push_back({p, i});
}
auto get_val = [] (int p) {
int lf = 1, rt = n;
while (lf < rt) {
int mid = lf + rt >> 1;
if (fen.get(mid) >= p) rt = mid;
else lf = mid + 1;
}
return lf;
};
auto simulation = [&] (int p) {
int val = get_val(p);
int L = itv[val].fs, R = itv[val].se;
// cout << val <<"hihi\n";
if (p - fen.get(val - 1) == 1)
return;
int np = L + p - fen.get(val - 1) - 1;
// cout << np <<" aa\n";
fen.update(val, - (R - np + 1));
itv[val] = {L, np - 1};
rep (i, np, R) {
int bound = get_bound(i, R);
itv[a[i]] = {i, bound};
fen.update(a[i], bound - i + 1);
i = bound;
}
};
rep (i, 0, n) {
// cout << i <<":\n";
// rep (i, 1, n) cout << a[i] <<" "<<itv[a[i]].fs<<" "<<itv[a[i]].se <<"\n";
iter (&id, qr[i]) {
int val = get_val(id.fs);
// cout << id.se <<" "<<id.fs<<" "<<val<<" "<<itv[val].fs<<" "<<itv[val].se<<" "<<fen.get(4)<<"qr\n";
Ans[id.se] = a[itv[val].fs + id.fs - fen.get(val - 1) - 1];
}
simulation(n / 2 + 1);
}
rep (i, 1, Q) {
cout << Ans[i] <<"\n";
}
}
#define file(name) freopen(name".inp", "r", stdin); \
freopen(name".out", "w", stdout);
/*
*/
int main () {
// file("c");
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll num_Test = 1;
// cin >> num_Test;
while(num_Test--)
solution();
}
/*
no bug challenge +2
6
25 18 40 37 29 95 41 53 39 69 61 90
14 18 22 28 18 30 32 32 63 58 71 78
*/
Compilation message
Main.cpp: In function 'void solution()':
Main.cpp:59:60: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
59 | spt[i][j] = max(spt[i][j - 1], spt[i + (1 << j - 1)][j - 1]);
| ~~^~~
Main.cpp: In lambda function:
Main.cpp:70:31: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
70 | int mid = lf + rt + 1 >> 1;
| ~~~~~~~~^~~
Main.cpp: In lambda function:
Main.cpp:94:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
94 | int mid = lf + rt >> 1;
| ~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
183 ms |
27024 KB |
Output is correct |
2 |
Correct |
220 ms |
33252 KB |
Output is correct |
3 |
Correct |
201 ms |
32812 KB |
Output is correct |
4 |
Correct |
178 ms |
31548 KB |
Output is correct |
5 |
Correct |
176 ms |
34640 KB |
Output is correct |
6 |
Correct |
156 ms |
34520 KB |
Output is correct |
7 |
Correct |
168 ms |
35408 KB |
Output is correct |
8 |
Correct |
159 ms |
33328 KB |
Output is correct |
9 |
Correct |
175 ms |
32232 KB |
Output is correct |
10 |
Correct |
161 ms |
32016 KB |
Output is correct |
11 |
Correct |
167 ms |
32252 KB |
Output is correct |
12 |
Correct |
171 ms |
29892 KB |
Output is correct |
13 |
Correct |
177 ms |
31132 KB |
Output is correct |
14 |
Correct |
167 ms |
33680 KB |
Output is correct |
15 |
Correct |
174 ms |
31612 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
206 ms |
29948 KB |
Output is correct |
18 |
Correct |
147 ms |
29856 KB |
Output is correct |
19 |
Correct |
1 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
276 ms |
46304 KB |
Output is correct |
2 |
Correct |
294 ms |
59576 KB |
Output is correct |
3 |
Correct |
246 ms |
55044 KB |
Output is correct |
4 |
Correct |
248 ms |
54452 KB |
Output is correct |
5 |
Correct |
234 ms |
55760 KB |
Output is correct |
6 |
Correct |
228 ms |
54484 KB |
Output is correct |
7 |
Correct |
270 ms |
59580 KB |
Output is correct |
8 |
Correct |
289 ms |
58040 KB |
Output is correct |
9 |
Correct |
239 ms |
54748 KB |
Output is correct |
10 |
Correct |
286 ms |
57016 KB |
Output is correct |
11 |
Correct |
206 ms |
53172 KB |
Output is correct |
12 |
Correct |
225 ms |
53172 KB |
Output is correct |
13 |
Correct |
286 ms |
56252 KB |
Output is correct |
14 |
Correct |
217 ms |
53556 KB |
Output is correct |
15 |
Correct |
274 ms |
57904 KB |
Output is correct |
16 |
Correct |
41 ms |
30552 KB |
Output is correct |
17 |
Correct |
279 ms |
55308 KB |
Output is correct |
18 |
Correct |
230 ms |
51412 KB |
Output is correct |
19 |
Correct |
108 ms |
37316 KB |
Output is correct |
20 |
Correct |
93 ms |
38852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
25684 KB |
Output is correct |
2 |
Correct |
60 ms |
26960 KB |
Output is correct |
3 |
Correct |
66 ms |
26968 KB |
Output is correct |
4 |
Correct |
53 ms |
26684 KB |
Output is correct |
5 |
Correct |
53 ms |
27128 KB |
Output is correct |
6 |
Correct |
50 ms |
26732 KB |
Output is correct |
7 |
Correct |
50 ms |
27032 KB |
Output is correct |
8 |
Correct |
51 ms |
26764 KB |
Output is correct |
9 |
Correct |
54 ms |
26820 KB |
Output is correct |
10 |
Correct |
45 ms |
26448 KB |
Output is correct |
11 |
Correct |
42 ms |
26516 KB |
Output is correct |
12 |
Correct |
52 ms |
26452 KB |
Output is correct |
13 |
Correct |
45 ms |
26448 KB |
Output is correct |
14 |
Correct |
46 ms |
26600 KB |
Output is correct |
15 |
Correct |
58 ms |
26452 KB |
Output is correct |
16 |
Correct |
18 ms |
21336 KB |
Output is correct |
17 |
Correct |
58 ms |
25876 KB |
Output is correct |
18 |
Correct |
41 ms |
25808 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
183 ms |
27024 KB |
Output is correct |
2 |
Correct |
220 ms |
33252 KB |
Output is correct |
3 |
Correct |
201 ms |
32812 KB |
Output is correct |
4 |
Correct |
178 ms |
31548 KB |
Output is correct |
5 |
Correct |
176 ms |
34640 KB |
Output is correct |
6 |
Correct |
156 ms |
34520 KB |
Output is correct |
7 |
Correct |
168 ms |
35408 KB |
Output is correct |
8 |
Correct |
159 ms |
33328 KB |
Output is correct |
9 |
Correct |
175 ms |
32232 KB |
Output is correct |
10 |
Correct |
161 ms |
32016 KB |
Output is correct |
11 |
Correct |
167 ms |
32252 KB |
Output is correct |
12 |
Correct |
171 ms |
29892 KB |
Output is correct |
13 |
Correct |
177 ms |
31132 KB |
Output is correct |
14 |
Correct |
167 ms |
33680 KB |
Output is correct |
15 |
Correct |
174 ms |
31612 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
206 ms |
29948 KB |
Output is correct |
18 |
Correct |
147 ms |
29856 KB |
Output is correct |
19 |
Correct |
1 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
21 |
Correct |
276 ms |
46304 KB |
Output is correct |
22 |
Correct |
294 ms |
59576 KB |
Output is correct |
23 |
Correct |
246 ms |
55044 KB |
Output is correct |
24 |
Correct |
248 ms |
54452 KB |
Output is correct |
25 |
Correct |
234 ms |
55760 KB |
Output is correct |
26 |
Correct |
228 ms |
54484 KB |
Output is correct |
27 |
Correct |
270 ms |
59580 KB |
Output is correct |
28 |
Correct |
289 ms |
58040 KB |
Output is correct |
29 |
Correct |
239 ms |
54748 KB |
Output is correct |
30 |
Correct |
286 ms |
57016 KB |
Output is correct |
31 |
Correct |
206 ms |
53172 KB |
Output is correct |
32 |
Correct |
225 ms |
53172 KB |
Output is correct |
33 |
Correct |
286 ms |
56252 KB |
Output is correct |
34 |
Correct |
217 ms |
53556 KB |
Output is correct |
35 |
Correct |
274 ms |
57904 KB |
Output is correct |
36 |
Correct |
41 ms |
30552 KB |
Output is correct |
37 |
Correct |
279 ms |
55308 KB |
Output is correct |
38 |
Correct |
230 ms |
51412 KB |
Output is correct |
39 |
Correct |
108 ms |
37316 KB |
Output is correct |
40 |
Correct |
93 ms |
38852 KB |
Output is correct |
41 |
Correct |
53 ms |
25684 KB |
Output is correct |
42 |
Correct |
60 ms |
26960 KB |
Output is correct |
43 |
Correct |
66 ms |
26968 KB |
Output is correct |
44 |
Correct |
53 ms |
26684 KB |
Output is correct |
45 |
Correct |
53 ms |
27128 KB |
Output is correct |
46 |
Correct |
50 ms |
26732 KB |
Output is correct |
47 |
Correct |
50 ms |
27032 KB |
Output is correct |
48 |
Correct |
51 ms |
26764 KB |
Output is correct |
49 |
Correct |
54 ms |
26820 KB |
Output is correct |
50 |
Correct |
45 ms |
26448 KB |
Output is correct |
51 |
Correct |
42 ms |
26516 KB |
Output is correct |
52 |
Correct |
52 ms |
26452 KB |
Output is correct |
53 |
Correct |
45 ms |
26448 KB |
Output is correct |
54 |
Correct |
46 ms |
26600 KB |
Output is correct |
55 |
Correct |
58 ms |
26452 KB |
Output is correct |
56 |
Correct |
18 ms |
21336 KB |
Output is correct |
57 |
Correct |
58 ms |
25876 KB |
Output is correct |
58 |
Correct |
41 ms |
25808 KB |
Output is correct |
59 |
Correct |
2 ms |
10588 KB |
Output is correct |
60 |
Correct |
1 ms |
10588 KB |
Output is correct |
61 |
Correct |
468 ms |
67136 KB |
Output is correct |
62 |
Correct |
490 ms |
64940 KB |
Output is correct |
63 |
Correct |
467 ms |
64084 KB |
Output is correct |
64 |
Correct |
382 ms |
63512 KB |
Output is correct |
65 |
Correct |
400 ms |
65160 KB |
Output is correct |
66 |
Correct |
377 ms |
63636 KB |
Output is correct |
67 |
Correct |
318 ms |
62800 KB |
Output is correct |
68 |
Correct |
404 ms |
61520 KB |
Output is correct |
69 |
Correct |
359 ms |
63532 KB |
Output is correct |
70 |
Correct |
333 ms |
59924 KB |
Output is correct |
71 |
Correct |
297 ms |
61260 KB |
Output is correct |
72 |
Correct |
325 ms |
60240 KB |
Output is correct |
73 |
Correct |
365 ms |
60880 KB |
Output is correct |
74 |
Correct |
340 ms |
62548 KB |
Output is correct |
75 |
Correct |
328 ms |
61328 KB |
Output is correct |
76 |
Correct |
44 ms |
30772 KB |
Output is correct |
77 |
Correct |
426 ms |
54712 KB |
Output is correct |
78 |
Correct |
390 ms |
54380 KB |
Output is correct |
79 |
Correct |
1 ms |
10588 KB |
Output is correct |
80 |
Correct |
1 ms |
10588 KB |
Output is correct |