#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define bg begin
#define ed end
#define ft front
#define bk back
#define ins insert
#define ers erase
#define sz(x) (int)(x).size()
#define all(x) (x).bg(), (x).ed()
#define mtp make_tuple
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define str string
#define pi pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vpi vector<pi>
#define vpll vector<pll>
#define FOR(i, l, r) for (int i = (l); i <= (r); ++i)
#define FOS(i, r, l) for (int i = (r); i >= (l); --i)
#define FRN(i, n) for (int i = 0; i < (n); ++i)
#define FSN(i, n) for (int i = (n) - 1; i >= 0; --i)
#define EACH(i, x) for (auto &i : (x))
#define WHILE while
template<typename T> T gcd(T a, T b) { WHILE(b) { a %= b; swap(a, b); } return b; }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define file "TEST"
mt19937 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 oo = 1e9 + 5;
const int N = 2e5 + 5;
const int Q = 1e6 + 5;
int n, q, a[N], f[N], opos[N], maxx[N][18], LOG[N], ans[Q];
vpi d[N];
void upd(int pos, int v) { for (int i = pos; i <= n; i += (i & -i)) f[i] += v; }
int get(int pos) { int rs = 0; for (int i = pos; i > 0; i -= (i & -i)) rs += f[i]; return rs; }
void build() {
FOR(i, 1, n) {
maxx[i][0] = a[i];
LOG[i] = log2(i);
}
FOR(j, 1, 17)
FOR(i, 1, n) {
if (i + (1 << j) - 1 > n) continue;
maxx[i][j] = max(maxx[i][j - 1], maxx[i + (1 << (j - 1))][j - 1]);
}
}
int getm(int l, int r) {
int len = LOG[r - l + 1];
return max(maxx[l][len], maxx[r - (1 << len) + 1][len]);
}
int lbound(int x) {
int pos = 0, num = 0, ans = oo;
FSN(i, 18) {
if (pos + (1 << i) > n) continue;
if (num + f[pos + (1 << i)] >= x) ans = min(ans, pos + (1 << i));
else {
pos |= 1 << i;
num += f[pos];
}
}
return ans;
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
cin >> n >> q;
FOR(i, 1, n) {
cin >> a[i];
opos[a[i]] = i;
}
FOR(j, 1, q) {
int t, i;
cin >> t >> i;
t = min(t, n);
d[t].pb({j, i});
}
FOR(i, 1, n) {
int j = i;
WHILE(j <= n && a[j] <= a[i]) ++j;
--j;
upd(a[i], j - i + 1);
i = j;
}
build();
FOR(i, 0, n) {
EACH(j, d[i]) {
int id = j.st, z = j.nd, pos = lbound(z), op = opos[pos] + z - get(pos - 1) - 1;
ans[id] = a[op];
}
int pos = lbound(n / 2), num = get(pos) - get(pos - 1), sum = get(pos);
if (sum > n / 2) {
upd(pos, n / 2 - sum);
int cur = opos[pos] + n / 2 - get(pos - 1);
WHILE(cur <= opos[pos] + num - 1) {
int l = cur, r = opos[pos] + num - 1, nxt = -1;
WHILE(l <= r) {
int mid = l + (r - l) / 2;
if (getm(cur, mid) <= a[cur]) {
nxt = mid;
l = mid + 1;
}
else r = mid - 1;
}
upd(a[cur], nxt - cur + 1);
cur = nxt + 1;
}
}
}
FOR(i, 1, q) cout << ans[i] << '\n';
// cerr << "\nTime: " << setprecision(5) << fixed << (ldb)clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
251 ms |
30880 KB |
Output is correct |
2 |
Correct |
267 ms |
29388 KB |
Output is correct |
3 |
Correct |
260 ms |
28808 KB |
Output is correct |
4 |
Correct |
204 ms |
27548 KB |
Output is correct |
5 |
Correct |
228 ms |
30732 KB |
Output is correct |
6 |
Correct |
251 ms |
30448 KB |
Output is correct |
7 |
Correct |
225 ms |
31624 KB |
Output is correct |
8 |
Correct |
214 ms |
29220 KB |
Output is correct |
9 |
Correct |
206 ms |
28008 KB |
Output is correct |
10 |
Correct |
223 ms |
27960 KB |
Output is correct |
11 |
Correct |
206 ms |
28360 KB |
Output is correct |
12 |
Correct |
206 ms |
25944 KB |
Output is correct |
13 |
Correct |
217 ms |
27120 KB |
Output is correct |
14 |
Correct |
215 ms |
29776 KB |
Output is correct |
15 |
Correct |
255 ms |
27760 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
235 ms |
26032 KB |
Output is correct |
18 |
Correct |
208 ms |
25840 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
361 ms |
53624 KB |
Output is correct |
2 |
Correct |
339 ms |
53924 KB |
Output is correct |
3 |
Correct |
288 ms |
46700 KB |
Output is correct |
4 |
Correct |
279 ms |
46880 KB |
Output is correct |
5 |
Correct |
267 ms |
47848 KB |
Output is correct |
6 |
Correct |
275 ms |
46268 KB |
Output is correct |
7 |
Correct |
334 ms |
53664 KB |
Output is correct |
8 |
Correct |
313 ms |
51620 KB |
Output is correct |
9 |
Correct |
274 ms |
47676 KB |
Output is correct |
10 |
Correct |
311 ms |
50508 KB |
Output is correct |
11 |
Correct |
263 ms |
45896 KB |
Output is correct |
12 |
Correct |
275 ms |
45452 KB |
Output is correct |
13 |
Correct |
339 ms |
49936 KB |
Output is correct |
14 |
Correct |
256 ms |
46684 KB |
Output is correct |
15 |
Correct |
311 ms |
51816 KB |
Output is correct |
16 |
Correct |
60 ms |
23376 KB |
Output is correct |
17 |
Correct |
296 ms |
48396 KB |
Output is correct |
18 |
Correct |
225 ms |
42680 KB |
Output is correct |
19 |
Correct |
107 ms |
28432 KB |
Output is correct |
20 |
Correct |
119 ms |
29504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
18508 KB |
Output is correct |
2 |
Correct |
68 ms |
17812 KB |
Output is correct |
3 |
Correct |
73 ms |
17608 KB |
Output is correct |
4 |
Correct |
52 ms |
17356 KB |
Output is correct |
5 |
Correct |
82 ms |
17972 KB |
Output is correct |
6 |
Correct |
63 ms |
17256 KB |
Output is correct |
7 |
Correct |
55 ms |
17868 KB |
Output is correct |
8 |
Correct |
54 ms |
17176 KB |
Output is correct |
9 |
Correct |
59 ms |
17688 KB |
Output is correct |
10 |
Correct |
48 ms |
16892 KB |
Output is correct |
11 |
Correct |
58 ms |
17360 KB |
Output is correct |
12 |
Correct |
48 ms |
16880 KB |
Output is correct |
13 |
Correct |
47 ms |
16880 KB |
Output is correct |
14 |
Correct |
47 ms |
17316 KB |
Output is correct |
15 |
Correct |
46 ms |
17008 KB |
Output is correct |
16 |
Correct |
25 ms |
14096 KB |
Output is correct |
17 |
Correct |
66 ms |
16368 KB |
Output is correct |
18 |
Correct |
46 ms |
16072 KB |
Output is correct |
19 |
Correct |
3 ms |
5036 KB |
Output is correct |
20 |
Correct |
3 ms |
5032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
251 ms |
30880 KB |
Output is correct |
2 |
Correct |
267 ms |
29388 KB |
Output is correct |
3 |
Correct |
260 ms |
28808 KB |
Output is correct |
4 |
Correct |
204 ms |
27548 KB |
Output is correct |
5 |
Correct |
228 ms |
30732 KB |
Output is correct |
6 |
Correct |
251 ms |
30448 KB |
Output is correct |
7 |
Correct |
225 ms |
31624 KB |
Output is correct |
8 |
Correct |
214 ms |
29220 KB |
Output is correct |
9 |
Correct |
206 ms |
28008 KB |
Output is correct |
10 |
Correct |
223 ms |
27960 KB |
Output is correct |
11 |
Correct |
206 ms |
28360 KB |
Output is correct |
12 |
Correct |
206 ms |
25944 KB |
Output is correct |
13 |
Correct |
217 ms |
27120 KB |
Output is correct |
14 |
Correct |
215 ms |
29776 KB |
Output is correct |
15 |
Correct |
255 ms |
27760 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
235 ms |
26032 KB |
Output is correct |
18 |
Correct |
208 ms |
25840 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
361 ms |
53624 KB |
Output is correct |
22 |
Correct |
339 ms |
53924 KB |
Output is correct |
23 |
Correct |
288 ms |
46700 KB |
Output is correct |
24 |
Correct |
279 ms |
46880 KB |
Output is correct |
25 |
Correct |
267 ms |
47848 KB |
Output is correct |
26 |
Correct |
275 ms |
46268 KB |
Output is correct |
27 |
Correct |
334 ms |
53664 KB |
Output is correct |
28 |
Correct |
313 ms |
51620 KB |
Output is correct |
29 |
Correct |
274 ms |
47676 KB |
Output is correct |
30 |
Correct |
311 ms |
50508 KB |
Output is correct |
31 |
Correct |
263 ms |
45896 KB |
Output is correct |
32 |
Correct |
275 ms |
45452 KB |
Output is correct |
33 |
Correct |
339 ms |
49936 KB |
Output is correct |
34 |
Correct |
256 ms |
46684 KB |
Output is correct |
35 |
Correct |
311 ms |
51816 KB |
Output is correct |
36 |
Correct |
60 ms |
23376 KB |
Output is correct |
37 |
Correct |
296 ms |
48396 KB |
Output is correct |
38 |
Correct |
225 ms |
42680 KB |
Output is correct |
39 |
Correct |
107 ms |
28432 KB |
Output is correct |
40 |
Correct |
119 ms |
29504 KB |
Output is correct |
41 |
Correct |
76 ms |
18508 KB |
Output is correct |
42 |
Correct |
68 ms |
17812 KB |
Output is correct |
43 |
Correct |
73 ms |
17608 KB |
Output is correct |
44 |
Correct |
52 ms |
17356 KB |
Output is correct |
45 |
Correct |
82 ms |
17972 KB |
Output is correct |
46 |
Correct |
63 ms |
17256 KB |
Output is correct |
47 |
Correct |
55 ms |
17868 KB |
Output is correct |
48 |
Correct |
54 ms |
17176 KB |
Output is correct |
49 |
Correct |
59 ms |
17688 KB |
Output is correct |
50 |
Correct |
48 ms |
16892 KB |
Output is correct |
51 |
Correct |
58 ms |
17360 KB |
Output is correct |
52 |
Correct |
48 ms |
16880 KB |
Output is correct |
53 |
Correct |
47 ms |
16880 KB |
Output is correct |
54 |
Correct |
47 ms |
17316 KB |
Output is correct |
55 |
Correct |
46 ms |
17008 KB |
Output is correct |
56 |
Correct |
25 ms |
14096 KB |
Output is correct |
57 |
Correct |
66 ms |
16368 KB |
Output is correct |
58 |
Correct |
46 ms |
16072 KB |
Output is correct |
59 |
Correct |
3 ms |
5036 KB |
Output is correct |
60 |
Correct |
3 ms |
5032 KB |
Output is correct |
61 |
Correct |
646 ms |
61304 KB |
Output is correct |
62 |
Correct |
560 ms |
59320 KB |
Output is correct |
63 |
Correct |
568 ms |
57868 KB |
Output is correct |
64 |
Correct |
438 ms |
57456 KB |
Output is correct |
65 |
Correct |
450 ms |
59652 KB |
Output is correct |
66 |
Correct |
471 ms |
57164 KB |
Output is correct |
67 |
Correct |
429 ms |
57320 KB |
Output is correct |
68 |
Correct |
393 ms |
55084 KB |
Output is correct |
69 |
Correct |
420 ms |
57744 KB |
Output is correct |
70 |
Correct |
385 ms |
53472 KB |
Output is correct |
71 |
Correct |
419 ms |
55236 KB |
Output is correct |
72 |
Correct |
381 ms |
54036 KB |
Output is correct |
73 |
Correct |
410 ms |
54580 KB |
Output is correct |
74 |
Correct |
432 ms |
56740 KB |
Output is correct |
75 |
Correct |
427 ms |
55432 KB |
Output is correct |
76 |
Correct |
57 ms |
23336 KB |
Output is correct |
77 |
Correct |
428 ms |
48488 KB |
Output is correct |
78 |
Correct |
294 ms |
47656 KB |
Output is correct |
79 |
Correct |
3 ms |
4948 KB |
Output is correct |
80 |
Correct |
3 ms |
4948 KB |
Output is correct |