#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using ii = pair<int, int>;
struct nod {
int sz, pmax, vmax, val;
nod *l, *r;
} *nil = new nod{0, -1, -1, -1, 0, 0};
using ns = nod*;
vector<ns> ToDel;
ns nod_nou(int v) {
ns re = new nod{1, 0, v, v, nil, nil};
ToDel.push_back(re);
return re;
}
ns mod_fiu(ns a, int side, ns f) {
(side ? a->r : a->l) = f;
a->sz = a->l->sz + a->r->sz + 1;
int vl = a->l->vmax, eu = a->val, vr = a->r->vmax;
if(vl > eu && vl > vr) {
a->pmax = a->l->pmax;
a->vmax = vl;
}
if(eu > vl && eu > vr) {
a->pmax = a->l->sz;
a->vmax = eu;
}
if(vr > vl && vr > eu) {
a->pmax = a->r->pmax + a->l->sz + 1;
a->vmax = vr;
}
return a;
}
mt19937 rng(1);
ns join(ns a, ns b) {
return a == nil ? b :
b == nil ? a :
uniform_int_distribution<int>(1, a->sz + b->sz)(rng) <= a->sz ?
mod_fiu(a, 1, join(a->r, b)) /// a este tata
: mod_fiu(b, 0, join(a, b->l));
}
struct pns {
ns l, r;
};
pns split(ns a, int cnt) {
if(cnt > a->sz) {
cout << "Split prost\n";
exit(0);
}
if(a == nil)
assert(!a->sz);
pns tmp;
return !cnt ? pns{nil, a} :
cnt == a->sz ? pns{a, nil} :
a->l->sz >= cnt ?
(tmp = split(a->l, cnt), tmp.r = mod_fiu(a, 0, tmp.r), tmp)
: (tmp = split(a->r, cnt - a->l->sz - 1), tmp.l = mod_fiu(a, 1, tmp.l), tmp);
}
void afis(ns a) {
if(a == nil) return;
afis(a->l);
cout << a->val + 1 << " ";
afis(a->r);
}
vector<ns> Sparge(ns a) {
vector<ns> Re;
while(a->pmax) {
pns doi = split(a, a->pmax);
Re.push_back(doi.r);
a = doi.l;
}
Re.push_back(a);
return Re;
}
struct AIB {
int n;
vi V;
AIB(int N) : n(N + 1), V(N + 1, 0) {}
void update(int p, int v) {
++p;
while(p < n) {
V[p] += v;
p += p & -p;
}
}
int cauta(int v) {
///vreau prima pozitie pt care query(p) >= v
int p = 0;
for(int step = 20; step >= 0; --step)
if(p + (1 << step) < n && v >= V[p + (1 << step)]) {
p += (1 << step);
v -= V[p];
}
return p;
}
int query(int p) {
++p;
int re = 0;
while(p) {
re += V[p];
p -= p & -p;
}
return re;
}
};
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int n, q;
cin >> n >> q;
vi V0(n, 0);
ns A = nil, B = nil;
for(int i = 0; i < n; ++i) {
cin >> V0[i];
--V0[i];
if(i < n / 2) A = join(A, nod_nou(V0[i]));
else B = join(B, nod_nou(V0[i]));
}
AIB Sume(n);
auto SA = Sparge(A), SB = Sparge(B);
vector<ns> V(n, nil);
vi Sz(n, 0);
auto update = [&](ns nod, int sgn) {
if(nod == nil) return;
Sz[nod->vmax] += sgn * nod->sz;
V[nod->vmax] = nod;
Sume.update(nod->vmax, sgn * nod->sz);
};
vector<vector<ii> > Q(n + 2);
vi Re(q, 0);
for(int i = 0; i < q; ++i) {
int t, p;
cin >> t >> p;
Q[min(t, n)].push_back({i, p});
}
for(auto [id, p] : Q[0]) Re[id] = V0[p - 1];
for(auto it : SA)
update(it, 1);
for(auto it : SB)
update(it, 1);
for(int t = 1; t <= n; ++t) {
for(auto [id, p] : Q[t]) {
int p1 = Sume.cauta(p - 1);
ns eu = V[p1];
int nr = p - Sume.query(p1 - 1) - 1;
pns a = split(eu, nr);
pns b = split(a.r, 1);
Re[id] = b.l->val;
eu = join(a.l, join(b.l, b.r));
V[p1] = eu;
}
int p = Sume.cauta(n / 2);
if(Sume.query(p) == n / 2) { /// nu trebuie facut nimic
continue;
}
update(V[p], -1);
pns nou = split(V[p], n / 2 - Sume.query(p - 1));
update(nou.l, 1);
auto sir = Sparge(nou.r);
for(auto it : sir)
update(it, 1);
}
for(auto it : Re)
cout << it + 1 << "\n";
for(auto it : ToDel)
delete it;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
530 ms |
18508 KB |
Output is correct |
2 |
Correct |
286 ms |
17368 KB |
Output is correct |
3 |
Correct |
335 ms |
16892 KB |
Output is correct |
4 |
Correct |
783 ms |
16704 KB |
Output is correct |
5 |
Correct |
726 ms |
19028 KB |
Output is correct |
6 |
Correct |
763 ms |
19448 KB |
Output is correct |
7 |
Correct |
701 ms |
20052 KB |
Output is correct |
8 |
Correct |
666 ms |
24440 KB |
Output is correct |
9 |
Correct |
669 ms |
24112 KB |
Output is correct |
10 |
Correct |
649 ms |
23120 KB |
Output is correct |
11 |
Correct |
679 ms |
23640 KB |
Output is correct |
12 |
Correct |
558 ms |
21604 KB |
Output is correct |
13 |
Correct |
673 ms |
22476 KB |
Output is correct |
14 |
Correct |
615 ms |
25136 KB |
Output is correct |
15 |
Correct |
625 ms |
23324 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
140 ms |
21244 KB |
Output is correct |
18 |
Correct |
430 ms |
21040 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
576 ms |
38392 KB |
Output is correct |
2 |
Correct |
430 ms |
41736 KB |
Output is correct |
3 |
Correct |
485 ms |
37216 KB |
Output is correct |
4 |
Correct |
1032 ms |
35644 KB |
Output is correct |
5 |
Correct |
981 ms |
35232 KB |
Output is correct |
6 |
Correct |
1118 ms |
35380 KB |
Output is correct |
7 |
Correct |
1321 ms |
38140 KB |
Output is correct |
8 |
Correct |
1080 ms |
49892 KB |
Output is correct |
9 |
Correct |
888 ms |
46788 KB |
Output is correct |
10 |
Correct |
1114 ms |
48040 KB |
Output is correct |
11 |
Correct |
942 ms |
43960 KB |
Output is correct |
12 |
Correct |
978 ms |
44072 KB |
Output is correct |
13 |
Correct |
1103 ms |
47820 KB |
Output is correct |
14 |
Correct |
1043 ms |
44320 KB |
Output is correct |
15 |
Correct |
1162 ms |
49416 KB |
Output is correct |
16 |
Correct |
96 ms |
20876 KB |
Output is correct |
17 |
Correct |
248 ms |
49284 KB |
Output is correct |
18 |
Correct |
1047 ms |
41392 KB |
Output is correct |
19 |
Correct |
122 ms |
26028 KB |
Output is correct |
20 |
Correct |
293 ms |
27008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
166 ms |
13248 KB |
Output is correct |
2 |
Correct |
101 ms |
13176 KB |
Output is correct |
3 |
Correct |
121 ms |
12816 KB |
Output is correct |
4 |
Correct |
208 ms |
12284 KB |
Output is correct |
5 |
Correct |
203 ms |
12840 KB |
Output is correct |
6 |
Correct |
191 ms |
12148 KB |
Output is correct |
7 |
Correct |
204 ms |
12936 KB |
Output is correct |
8 |
Correct |
183 ms |
13764 KB |
Output is correct |
9 |
Correct |
178 ms |
14280 KB |
Output is correct |
10 |
Correct |
186 ms |
13416 KB |
Output is correct |
11 |
Correct |
166 ms |
14020 KB |
Output is correct |
12 |
Correct |
168 ms |
13368 KB |
Output is correct |
13 |
Correct |
181 ms |
13432 KB |
Output is correct |
14 |
Correct |
179 ms |
14100 KB |
Output is correct |
15 |
Correct |
190 ms |
13588 KB |
Output is correct |
16 |
Correct |
46 ms |
10808 KB |
Output is correct |
17 |
Correct |
64 ms |
14604 KB |
Output is correct |
18 |
Correct |
119 ms |
13352 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
530 ms |
18508 KB |
Output is correct |
2 |
Correct |
286 ms |
17368 KB |
Output is correct |
3 |
Correct |
335 ms |
16892 KB |
Output is correct |
4 |
Correct |
783 ms |
16704 KB |
Output is correct |
5 |
Correct |
726 ms |
19028 KB |
Output is correct |
6 |
Correct |
763 ms |
19448 KB |
Output is correct |
7 |
Correct |
701 ms |
20052 KB |
Output is correct |
8 |
Correct |
666 ms |
24440 KB |
Output is correct |
9 |
Correct |
669 ms |
24112 KB |
Output is correct |
10 |
Correct |
649 ms |
23120 KB |
Output is correct |
11 |
Correct |
679 ms |
23640 KB |
Output is correct |
12 |
Correct |
558 ms |
21604 KB |
Output is correct |
13 |
Correct |
673 ms |
22476 KB |
Output is correct |
14 |
Correct |
615 ms |
25136 KB |
Output is correct |
15 |
Correct |
625 ms |
23324 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
140 ms |
21244 KB |
Output is correct |
18 |
Correct |
430 ms |
21040 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
576 ms |
38392 KB |
Output is correct |
22 |
Correct |
430 ms |
41736 KB |
Output is correct |
23 |
Correct |
485 ms |
37216 KB |
Output is correct |
24 |
Correct |
1032 ms |
35644 KB |
Output is correct |
25 |
Correct |
981 ms |
35232 KB |
Output is correct |
26 |
Correct |
1118 ms |
35380 KB |
Output is correct |
27 |
Correct |
1321 ms |
38140 KB |
Output is correct |
28 |
Correct |
1080 ms |
49892 KB |
Output is correct |
29 |
Correct |
888 ms |
46788 KB |
Output is correct |
30 |
Correct |
1114 ms |
48040 KB |
Output is correct |
31 |
Correct |
942 ms |
43960 KB |
Output is correct |
32 |
Correct |
978 ms |
44072 KB |
Output is correct |
33 |
Correct |
1103 ms |
47820 KB |
Output is correct |
34 |
Correct |
1043 ms |
44320 KB |
Output is correct |
35 |
Correct |
1162 ms |
49416 KB |
Output is correct |
36 |
Correct |
96 ms |
20876 KB |
Output is correct |
37 |
Correct |
248 ms |
49284 KB |
Output is correct |
38 |
Correct |
1047 ms |
41392 KB |
Output is correct |
39 |
Correct |
122 ms |
26028 KB |
Output is correct |
40 |
Correct |
293 ms |
27008 KB |
Output is correct |
41 |
Correct |
166 ms |
13248 KB |
Output is correct |
42 |
Correct |
101 ms |
13176 KB |
Output is correct |
43 |
Correct |
121 ms |
12816 KB |
Output is correct |
44 |
Correct |
208 ms |
12284 KB |
Output is correct |
45 |
Correct |
203 ms |
12840 KB |
Output is correct |
46 |
Correct |
191 ms |
12148 KB |
Output is correct |
47 |
Correct |
204 ms |
12936 KB |
Output is correct |
48 |
Correct |
183 ms |
13764 KB |
Output is correct |
49 |
Correct |
178 ms |
14280 KB |
Output is correct |
50 |
Correct |
186 ms |
13416 KB |
Output is correct |
51 |
Correct |
166 ms |
14020 KB |
Output is correct |
52 |
Correct |
168 ms |
13368 KB |
Output is correct |
53 |
Correct |
181 ms |
13432 KB |
Output is correct |
54 |
Correct |
179 ms |
14100 KB |
Output is correct |
55 |
Correct |
190 ms |
13588 KB |
Output is correct |
56 |
Correct |
46 ms |
10808 KB |
Output is correct |
57 |
Correct |
64 ms |
14604 KB |
Output is correct |
58 |
Correct |
119 ms |
13352 KB |
Output is correct |
59 |
Correct |
0 ms |
344 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
1347 ms |
59152 KB |
Output is correct |
62 |
Correct |
691 ms |
58384 KB |
Output is correct |
63 |
Correct |
871 ms |
56088 KB |
Output is correct |
64 |
Correct |
1757 ms |
55268 KB |
Output is correct |
65 |
Correct |
1925 ms |
57388 KB |
Output is correct |
66 |
Correct |
1758 ms |
54968 KB |
Output is correct |
67 |
Correct |
1935 ms |
55260 KB |
Output is correct |
68 |
Correct |
1706 ms |
52760 KB |
Output is correct |
69 |
Correct |
1555 ms |
55592 KB |
Output is correct |
70 |
Correct |
1561 ms |
51336 KB |
Output is correct |
71 |
Correct |
1597 ms |
53156 KB |
Output is correct |
72 |
Correct |
1584 ms |
51508 KB |
Output is correct |
73 |
Correct |
1516 ms |
51992 KB |
Output is correct |
74 |
Correct |
1639 ms |
54464 KB |
Output is correct |
75 |
Correct |
1569 ms |
53184 KB |
Output is correct |
76 |
Correct |
92 ms |
20964 KB |
Output is correct |
77 |
Correct |
271 ms |
49588 KB |
Output is correct |
78 |
Correct |
920 ms |
49528 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |