// Arayi
#include <bits/stdc++.h>
#define fr first
#define sc second
#define MP make_pair
#define ad push_back
#define PB push_back
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define lli long long int
#define y1 arayikhalatyan
#define j1 jigglypuff
#define ld long double
#define itn int
#define pir pair<int, int>
#define all(x) (x).begin(), (x).end()
#define str string
#define enl endl
#define en endl
#define cd complex<long double>
#define vcd vector<cd>
#define vii vector<int>
#define vlli vector<lli>
using namespace std;
lli gcd(lli a, lli b) { return (b == 0LL ? a : gcd(b, a % b)); }
ld dist(ld x, ld y1, ld x2, ld y2)
{
return sqrt((x - x2) * (x - x2) + (y1 - y2) * (y1 - y2));
}
lli S(lli a)
{
return (a * (a + 1LL)) / 2;
}
mt19937 rnd(363542);
char vow[] = {'a', 'e', 'i', 'o', 'u'};
int dx[] = {0, -1, 0, 1, -1, -1, 1, 1, 0};
int dy[] = {-1, 0, 1, 0, -1, 1, -1, 1, 0};
char dc[] = {'R', 'D', 'L', 'U'};
const int N = 1e6 + 20;
const lli mod = 1e9 + 7;
const ld pi = acos(-1);
const ld e = 1e-13;
const int T = 200;
lli bp(lli a, lli b = mod - 2LL)
{
lli ret = 1;
while (b)
{
if (b & 1)
ret *= a, ret %= mod;
a *= a;
a %= mod;
b >>= 1;
}
return ret;
}
ostream &operator<<(ostream &c, pir a)
{
c << a.fr << " " << a.sc;
return c;
}
template <class T>
void maxi(T &a, T b)
{
a = max(a, b);
}
template <class T>
void mini(T &a, T b)
{
a = min(a, b);
}
int n, q;
int a[N], ind[N], hj[N], pat[N];
pair<pir, int> b[N];
int t[4 * N];
void upd(int q, int v, int l = 0, int r = n, int nd = 1)
{
if (q > r || q < l)
return;
if (l == r)
{
t[nd] += v;
return;
}
int md = (l + r) / 2;
upd(q, v, l, md, nd * 2);
upd(q, v, md + 1, r, nd * 2 + 1);
t[nd] = t[nd * 2] + t[nd * 2 + 1];
}
int qry(int l, int r, int nl = 0, int nr = n, int nd = 1)
{
if (l > nr || r < nl)
return 0;
if (nl == l && r == nr)
return t[nd];
int md = (nl + nr) / 2;
return qry(l, min(md, r), nl, md, nd * 2) + qry(max(md + 1, l), r, md + 1, nr, nd * 2 + 1);
}
int qr(int v, int nl = 0, int nr = n, int nd = 1)
{
if (nl == nr)
return nl;
int md = (nl + nr) / 2;
if (t[nd * 2] > v)
return qr(v, nl, md, nd * 2);
else
return qr(v - t[nd * 2], md + 1, nr, nd * 2 + 1);
}
int main()
{
fastio;
cin >> n >> q;
for (int i = 0; i < n; i++)
{
cin >> a[i];
ind[a[i]] = i;
}
for (int i = 0; i < q; i++)
{
cin >> b[i].fr.fr >> b[i].fr.sc;
b[i].fr.sc--;
b[i].sc = i;
}
sort(b, b + q);
stack<pair<int, int>> st;
st.push(MP(mod, n));
for (int i = n - 1; i >= 0; i--)
{
while (a[i] > st.top().fr)
st.pop();
hj[i] = st.top().sc;
st.push(MP(a[i], i));
}
int i1 = 0;
while (i1 < n)
{
if (i1 < n / 2 && hj[i1] >= n / 2)
{
upd(a[i1], n / 2 - i1);
// cout << a[i1] << " " << n / 2 - i1 << endl;
i1 = n / 2;
}
else
{
upd(a[i1], hj[i1] - i1);
// cout << a[i1] << " " << hj[i1] - i1 << endl;
i1 = hj[i1];
}
}
i1 = 0;
int t1 = 0;
while (i1 < q && b[i1].fr.fr == t1)
{
pat[b[i1].sc] = a[b[i1].fr.sc];
i1++;
}
t1++;
while (i1 < q)
{
while (i1 < q && b[i1].fr.fr == t1)
{
int ans = qr(b[i1].fr.sc);
// cout << b[i1].fr.sc << " " << ans << endl;
int sm = b[i1].fr.sc - qry(0, ans - 1);
pat[b[i1].sc] = a[ind[ans] + sm];
i1++;
}
int ans = qr(n / 2);
if (qry(0, ans - 1) == n / 2)
{
while (i1 < q)
{
int ans = qr(b[i1].fr.sc);
// cout << b[i1].fr.sc << " " << ans << endl;
int sm = b[i1].fr.sc - qry(0, ans - 1);
pat[b[i1].sc] = a[ind[ans] + sm];
i1++;
}
}
else
{
int sm = n / 2 - qry(0, ans - 1);
int qn = qry(ans, ans);
upd(ans, sm - qn);
int i1 = ind[ans] + sm;
while (i1 < ind[ans] + qn)
{
upd(a[i1], min(hj[i1], ind[ans] + qn) - i1);
i1 = hj[i1];
}
}
t1++;
}
for (int i = 0; i < q; i++)
cout << pat[i] << "\n";
return 0;
}
/*
__
*(><)*
\/ /
||/
--||
||
/\
/ \
/ \
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
444 ms |
20432 KB |
Output is correct |
2 |
Correct |
397 ms |
22600 KB |
Output is correct |
3 |
Correct |
379 ms |
22548 KB |
Output is correct |
4 |
Correct |
386 ms |
25096 KB |
Output is correct |
5 |
Correct |
393 ms |
25604 KB |
Output is correct |
6 |
Correct |
377 ms |
24304 KB |
Output is correct |
7 |
Correct |
389 ms |
24236 KB |
Output is correct |
8 |
Correct |
381 ms |
23604 KB |
Output is correct |
9 |
Correct |
389 ms |
24000 KB |
Output is correct |
10 |
Correct |
401 ms |
23600 KB |
Output is correct |
11 |
Correct |
385 ms |
23600 KB |
Output is correct |
12 |
Correct |
367 ms |
22608 KB |
Output is correct |
13 |
Correct |
399 ms |
23220 KB |
Output is correct |
14 |
Correct |
392 ms |
25776 KB |
Output is correct |
15 |
Correct |
401 ms |
25944 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
327 ms |
24668 KB |
Output is correct |
18 |
Correct |
357 ms |
24416 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
530 ms |
40208 KB |
Output is correct |
2 |
Correct |
496 ms |
40652 KB |
Output is correct |
3 |
Correct |
424 ms |
32552 KB |
Output is correct |
4 |
Correct |
472 ms |
29576 KB |
Output is correct |
5 |
Correct |
442 ms |
29400 KB |
Output is correct |
6 |
Correct |
395 ms |
31368 KB |
Output is correct |
7 |
Correct |
456 ms |
33532 KB |
Output is correct |
8 |
Correct |
474 ms |
35996 KB |
Output is correct |
9 |
Correct |
423 ms |
29496 KB |
Output is correct |
10 |
Correct |
419 ms |
33828 KB |
Output is correct |
11 |
Correct |
366 ms |
27476 KB |
Output is correct |
12 |
Correct |
364 ms |
26244 KB |
Output is correct |
13 |
Correct |
417 ms |
33856 KB |
Output is correct |
14 |
Correct |
383 ms |
31900 KB |
Output is correct |
15 |
Correct |
423 ms |
37512 KB |
Output is correct |
16 |
Correct |
21 ms |
5068 KB |
Output is correct |
17 |
Correct |
311 ms |
33576 KB |
Output is correct |
18 |
Correct |
338 ms |
24004 KB |
Output is correct |
19 |
Correct |
67 ms |
9004 KB |
Output is correct |
20 |
Correct |
131 ms |
11464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
6392 KB |
Output is correct |
2 |
Correct |
85 ms |
6380 KB |
Output is correct |
3 |
Correct |
92 ms |
6336 KB |
Output is correct |
4 |
Correct |
61 ms |
5872 KB |
Output is correct |
5 |
Correct |
88 ms |
5032 KB |
Output is correct |
6 |
Correct |
68 ms |
6020 KB |
Output is correct |
7 |
Correct |
74 ms |
5120 KB |
Output is correct |
8 |
Correct |
64 ms |
5480 KB |
Output is correct |
9 |
Correct |
75 ms |
5956 KB |
Output is correct |
10 |
Correct |
61 ms |
5892 KB |
Output is correct |
11 |
Correct |
61 ms |
5096 KB |
Output is correct |
12 |
Correct |
53 ms |
5784 KB |
Output is correct |
13 |
Correct |
56 ms |
5852 KB |
Output is correct |
14 |
Correct |
56 ms |
6008 KB |
Output is correct |
15 |
Correct |
55 ms |
5224 KB |
Output is correct |
16 |
Correct |
12 ms |
2628 KB |
Output is correct |
17 |
Correct |
51 ms |
6236 KB |
Output is correct |
18 |
Correct |
44 ms |
4428 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
444 ms |
20432 KB |
Output is correct |
2 |
Correct |
397 ms |
22600 KB |
Output is correct |
3 |
Correct |
379 ms |
22548 KB |
Output is correct |
4 |
Correct |
386 ms |
25096 KB |
Output is correct |
5 |
Correct |
393 ms |
25604 KB |
Output is correct |
6 |
Correct |
377 ms |
24304 KB |
Output is correct |
7 |
Correct |
389 ms |
24236 KB |
Output is correct |
8 |
Correct |
381 ms |
23604 KB |
Output is correct |
9 |
Correct |
389 ms |
24000 KB |
Output is correct |
10 |
Correct |
401 ms |
23600 KB |
Output is correct |
11 |
Correct |
385 ms |
23600 KB |
Output is correct |
12 |
Correct |
367 ms |
22608 KB |
Output is correct |
13 |
Correct |
399 ms |
23220 KB |
Output is correct |
14 |
Correct |
392 ms |
25776 KB |
Output is correct |
15 |
Correct |
401 ms |
25944 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
327 ms |
24668 KB |
Output is correct |
18 |
Correct |
357 ms |
24416 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
328 KB |
Output is correct |
21 |
Correct |
530 ms |
40208 KB |
Output is correct |
22 |
Correct |
496 ms |
40652 KB |
Output is correct |
23 |
Correct |
424 ms |
32552 KB |
Output is correct |
24 |
Correct |
472 ms |
29576 KB |
Output is correct |
25 |
Correct |
442 ms |
29400 KB |
Output is correct |
26 |
Correct |
395 ms |
31368 KB |
Output is correct |
27 |
Correct |
456 ms |
33532 KB |
Output is correct |
28 |
Correct |
474 ms |
35996 KB |
Output is correct |
29 |
Correct |
423 ms |
29496 KB |
Output is correct |
30 |
Correct |
419 ms |
33828 KB |
Output is correct |
31 |
Correct |
366 ms |
27476 KB |
Output is correct |
32 |
Correct |
364 ms |
26244 KB |
Output is correct |
33 |
Correct |
417 ms |
33856 KB |
Output is correct |
34 |
Correct |
383 ms |
31900 KB |
Output is correct |
35 |
Correct |
423 ms |
37512 KB |
Output is correct |
36 |
Correct |
21 ms |
5068 KB |
Output is correct |
37 |
Correct |
311 ms |
33576 KB |
Output is correct |
38 |
Correct |
338 ms |
24004 KB |
Output is correct |
39 |
Correct |
67 ms |
9004 KB |
Output is correct |
40 |
Correct |
131 ms |
11464 KB |
Output is correct |
41 |
Correct |
102 ms |
6392 KB |
Output is correct |
42 |
Correct |
85 ms |
6380 KB |
Output is correct |
43 |
Correct |
92 ms |
6336 KB |
Output is correct |
44 |
Correct |
61 ms |
5872 KB |
Output is correct |
45 |
Correct |
88 ms |
5032 KB |
Output is correct |
46 |
Correct |
68 ms |
6020 KB |
Output is correct |
47 |
Correct |
74 ms |
5120 KB |
Output is correct |
48 |
Correct |
64 ms |
5480 KB |
Output is correct |
49 |
Correct |
75 ms |
5956 KB |
Output is correct |
50 |
Correct |
61 ms |
5892 KB |
Output is correct |
51 |
Correct |
61 ms |
5096 KB |
Output is correct |
52 |
Correct |
53 ms |
5784 KB |
Output is correct |
53 |
Correct |
56 ms |
5852 KB |
Output is correct |
54 |
Correct |
56 ms |
6008 KB |
Output is correct |
55 |
Correct |
55 ms |
5224 KB |
Output is correct |
56 |
Correct |
12 ms |
2628 KB |
Output is correct |
57 |
Correct |
51 ms |
6236 KB |
Output is correct |
58 |
Correct |
44 ms |
4428 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
0 ms |
340 KB |
Output is correct |
61 |
Correct |
664 ms |
35948 KB |
Output is correct |
62 |
Correct |
646 ms |
37748 KB |
Output is correct |
63 |
Correct |
650 ms |
33356 KB |
Output is correct |
64 |
Correct |
556 ms |
32864 KB |
Output is correct |
65 |
Correct |
576 ms |
35948 KB |
Output is correct |
66 |
Correct |
554 ms |
33664 KB |
Output is correct |
67 |
Correct |
539 ms |
35192 KB |
Output is correct |
68 |
Correct |
551 ms |
35036 KB |
Output is correct |
69 |
Correct |
542 ms |
33188 KB |
Output is correct |
70 |
Correct |
545 ms |
34648 KB |
Output is correct |
71 |
Correct |
531 ms |
34068 KB |
Output is correct |
72 |
Correct |
543 ms |
31740 KB |
Output is correct |
73 |
Correct |
526 ms |
32828 KB |
Output is correct |
74 |
Correct |
606 ms |
34148 KB |
Output is correct |
75 |
Correct |
554 ms |
32740 KB |
Output is correct |
76 |
Correct |
23 ms |
4200 KB |
Output is correct |
77 |
Correct |
400 ms |
31872 KB |
Output is correct |
78 |
Correct |
436 ms |
30092 KB |
Output is correct |
79 |
Correct |
1 ms |
340 KB |
Output is correct |
80 |
Correct |
1 ms |
340 KB |
Output is correct |