#include "bubblesort2.h"
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct treap {
treap *l = nullptr, *r = nullptr;
int x, y, p, siz = 1;
int c;
int lazy = 0;
int val;
void pushdown() {
if (l != nullptr) l -> lazy += lazy;
if (r != nullptr) r -> lazy += lazy;
val += lazy;
c += lazy;
lazy = 0;
}
void upd() {
pushdown();
if (l != nullptr) l -> pushdown();
if (r != nullptr) r -> pushdown();
siz = ((l == nullptr) ? 0 : l -> siz) + ((r == nullptr) ? 0 : r -> siz) + 1;
val = max(c, max((l == nullptr) ? c : l -> val, (r == nullptr) ? c : r -> val));
}
treap(int a, int pp, int cc) : x(a), y(rng()), p(pp), c(cc) {val = c;}
};
int siz(treap *t) { return (t == nullptr) ? 0 : t -> siz; }
treap* merge(treap *t1, treap *t2) {
if (t1 == nullptr)
return t2;
if (t2 == nullptr)
return t1;
t1 -> pushdown();
t2 -> pushdown();
if (t1 -> y > t2 -> y) {
t1 -> r = merge(t1 -> r, t2);
t1 -> upd();
return t1;
} else {
t2 -> l = merge(t1, t2 -> l);
t2 -> upd();
return t2;
}
}
pair<treap*, treap*> split(treap *t, int x, int pos) {
if (t == nullptr)
return {nullptr, nullptr};
t -> pushdown();
if (t -> x > x || (t -> x == x && t -> p >= pos)) {
auto tmp = split(t -> l, x, pos);
t -> l = tmp.second;
t -> upd();
return {tmp.first, t};
} else {
auto tmp = split(t -> r, x, pos);
t -> r = tmp.first;
t -> upd();
return {t, tmp.second};
}
}
inline void addAll(treap *t, int a) { if (t != nullptr) t -> lazy += a; }
treap *root;
vector<int> countScans(vector<int> A, vector<int> X, vector<int> V) {
int n = A.size();
pair<int, int> B[n];
for (int i = 0; i < n; i++)
B[i] = {A[i], i};
sort(B, B + n);
for (int i = 0; i < n; i++)
root = merge(root, new treap(B[i].fs, B[i].sc, B[i].sc - i));
vector<int> answer(X.size());
for (unsigned int query = 0; query < X.size(); query++) {
int pos = X[query], val = V[query];
if (A[pos] > val) {
//a.fs -end position of element- b.fs -this element- th.sc
auto a = split(root, val, pos);
auto b = split(a.sc, A[pos], pos);
auto th = split(b.sc, A[pos], pos + 1);
addAll(b.fs, -1);
treap *d = new treap(val, pos, pos - siz(a.fs));
root = merge(merge(a.fs, d), merge(b.fs, th.sc));
} else {
//a.fs -this element- b.fs -end position of element- b.sc
auto a = split(root, A[pos], pos);
auto th = split(a.sc, A[pos], pos + 1);
auto b = split(th.sc, val, pos);
addAll(b.fs, 1);
treap *d = new treap(val, pos, pos - siz(a.fs) - siz(b.fs));
root = merge(merge(a.fs, b.fs), merge(d, b.sc));
}
A[pos] = val;
root -> pushdown();
answer[query] = root -> val;
}
return answer;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
9 ms |
640 KB |
Output is correct |
4 |
Correct |
7 ms |
640 KB |
Output is correct |
5 |
Correct |
9 ms |
640 KB |
Output is correct |
6 |
Correct |
8 ms |
640 KB |
Output is correct |
7 |
Correct |
8 ms |
640 KB |
Output is correct |
8 |
Correct |
8 ms |
640 KB |
Output is correct |
9 |
Correct |
8 ms |
640 KB |
Output is correct |
10 |
Correct |
8 ms |
640 KB |
Output is correct |
11 |
Correct |
8 ms |
640 KB |
Output is correct |
12 |
Correct |
9 ms |
640 KB |
Output is correct |
13 |
Correct |
7 ms |
640 KB |
Output is correct |
14 |
Correct |
7 ms |
640 KB |
Output is correct |
15 |
Correct |
10 ms |
640 KB |
Output is correct |
16 |
Correct |
7 ms |
640 KB |
Output is correct |
17 |
Correct |
7 ms |
612 KB |
Output is correct |
18 |
Correct |
8 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
9 ms |
640 KB |
Output is correct |
4 |
Correct |
7 ms |
640 KB |
Output is correct |
5 |
Correct |
9 ms |
640 KB |
Output is correct |
6 |
Correct |
8 ms |
640 KB |
Output is correct |
7 |
Correct |
8 ms |
640 KB |
Output is correct |
8 |
Correct |
8 ms |
640 KB |
Output is correct |
9 |
Correct |
8 ms |
640 KB |
Output is correct |
10 |
Correct |
8 ms |
640 KB |
Output is correct |
11 |
Correct |
8 ms |
640 KB |
Output is correct |
12 |
Correct |
9 ms |
640 KB |
Output is correct |
13 |
Correct |
7 ms |
640 KB |
Output is correct |
14 |
Correct |
7 ms |
640 KB |
Output is correct |
15 |
Correct |
10 ms |
640 KB |
Output is correct |
16 |
Correct |
7 ms |
640 KB |
Output is correct |
17 |
Correct |
7 ms |
612 KB |
Output is correct |
18 |
Correct |
8 ms |
640 KB |
Output is correct |
19 |
Correct |
24 ms |
1560 KB |
Output is correct |
20 |
Correct |
25 ms |
1792 KB |
Output is correct |
21 |
Correct |
27 ms |
1784 KB |
Output is correct |
22 |
Correct |
26 ms |
1788 KB |
Output is correct |
23 |
Correct |
37 ms |
1756 KB |
Output is correct |
24 |
Correct |
26 ms |
1792 KB |
Output is correct |
25 |
Correct |
25 ms |
1812 KB |
Output is correct |
26 |
Correct |
23 ms |
1792 KB |
Output is correct |
27 |
Correct |
24 ms |
1784 KB |
Output is correct |
28 |
Correct |
34 ms |
1788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
2680 KB |
Output is correct |
2 |
Correct |
132 ms |
5852 KB |
Output is correct |
3 |
Correct |
273 ms |
8796 KB |
Output is correct |
4 |
Correct |
236 ms |
8824 KB |
Output is correct |
5 |
Correct |
237 ms |
8952 KB |
Output is correct |
6 |
Correct |
256 ms |
8896 KB |
Output is correct |
7 |
Correct |
245 ms |
8884 KB |
Output is correct |
8 |
Correct |
271 ms |
8952 KB |
Output is correct |
9 |
Correct |
234 ms |
8952 KB |
Output is correct |
10 |
Correct |
139 ms |
8952 KB |
Output is correct |
11 |
Correct |
173 ms |
8924 KB |
Output is correct |
12 |
Correct |
179 ms |
8952 KB |
Output is correct |
13 |
Correct |
161 ms |
8924 KB |
Output is correct |
14 |
Correct |
152 ms |
8832 KB |
Output is correct |
15 |
Correct |
212 ms |
8824 KB |
Output is correct |
16 |
Correct |
140 ms |
8848 KB |
Output is correct |
17 |
Correct |
168 ms |
8796 KB |
Output is correct |
18 |
Correct |
125 ms |
8824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
9 ms |
640 KB |
Output is correct |
4 |
Correct |
7 ms |
640 KB |
Output is correct |
5 |
Correct |
9 ms |
640 KB |
Output is correct |
6 |
Correct |
8 ms |
640 KB |
Output is correct |
7 |
Correct |
8 ms |
640 KB |
Output is correct |
8 |
Correct |
8 ms |
640 KB |
Output is correct |
9 |
Correct |
8 ms |
640 KB |
Output is correct |
10 |
Correct |
8 ms |
640 KB |
Output is correct |
11 |
Correct |
8 ms |
640 KB |
Output is correct |
12 |
Correct |
9 ms |
640 KB |
Output is correct |
13 |
Correct |
7 ms |
640 KB |
Output is correct |
14 |
Correct |
7 ms |
640 KB |
Output is correct |
15 |
Correct |
10 ms |
640 KB |
Output is correct |
16 |
Correct |
7 ms |
640 KB |
Output is correct |
17 |
Correct |
7 ms |
612 KB |
Output is correct |
18 |
Correct |
8 ms |
640 KB |
Output is correct |
19 |
Correct |
24 ms |
1560 KB |
Output is correct |
20 |
Correct |
25 ms |
1792 KB |
Output is correct |
21 |
Correct |
27 ms |
1784 KB |
Output is correct |
22 |
Correct |
26 ms |
1788 KB |
Output is correct |
23 |
Correct |
37 ms |
1756 KB |
Output is correct |
24 |
Correct |
26 ms |
1792 KB |
Output is correct |
25 |
Correct |
25 ms |
1812 KB |
Output is correct |
26 |
Correct |
23 ms |
1792 KB |
Output is correct |
27 |
Correct |
24 ms |
1784 KB |
Output is correct |
28 |
Correct |
34 ms |
1788 KB |
Output is correct |
29 |
Correct |
24 ms |
2680 KB |
Output is correct |
30 |
Correct |
132 ms |
5852 KB |
Output is correct |
31 |
Correct |
273 ms |
8796 KB |
Output is correct |
32 |
Correct |
236 ms |
8824 KB |
Output is correct |
33 |
Correct |
237 ms |
8952 KB |
Output is correct |
34 |
Correct |
256 ms |
8896 KB |
Output is correct |
35 |
Correct |
245 ms |
8884 KB |
Output is correct |
36 |
Correct |
271 ms |
8952 KB |
Output is correct |
37 |
Correct |
234 ms |
8952 KB |
Output is correct |
38 |
Correct |
139 ms |
8952 KB |
Output is correct |
39 |
Correct |
173 ms |
8924 KB |
Output is correct |
40 |
Correct |
179 ms |
8952 KB |
Output is correct |
41 |
Correct |
161 ms |
8924 KB |
Output is correct |
42 |
Correct |
152 ms |
8832 KB |
Output is correct |
43 |
Correct |
212 ms |
8824 KB |
Output is correct |
44 |
Correct |
140 ms |
8848 KB |
Output is correct |
45 |
Correct |
168 ms |
8796 KB |
Output is correct |
46 |
Correct |
125 ms |
8824 KB |
Output is correct |
47 |
Correct |
852 ms |
24804 KB |
Output is correct |
48 |
Correct |
3799 ms |
74256 KB |
Output is correct |
49 |
Correct |
4027 ms |
81336 KB |
Output is correct |
50 |
Correct |
4546 ms |
81376 KB |
Output is correct |
51 |
Correct |
3920 ms |
81272 KB |
Output is correct |
52 |
Correct |
3927 ms |
81204 KB |
Output is correct |
53 |
Correct |
4286 ms |
81352 KB |
Output is correct |
54 |
Correct |
4213 ms |
81056 KB |
Output is correct |
55 |
Correct |
4661 ms |
81276 KB |
Output is correct |
56 |
Correct |
3898 ms |
81028 KB |
Output is correct |
57 |
Correct |
3942 ms |
81116 KB |
Output is correct |
58 |
Correct |
4112 ms |
81092 KB |
Output is correct |
59 |
Correct |
3643 ms |
81208 KB |
Output is correct |
60 |
Correct |
3847 ms |
81140 KB |
Output is correct |
61 |
Correct |
3709 ms |
80868 KB |
Output is correct |
62 |
Correct |
3249 ms |
80868 KB |
Output is correct |
63 |
Correct |
3235 ms |
80872 KB |
Output is correct |
64 |
Correct |
3372 ms |
81032 KB |
Output is correct |
65 |
Correct |
2968 ms |
81040 KB |
Output is correct |
66 |
Correct |
2983 ms |
80844 KB |
Output is correct |
67 |
Correct |
2984 ms |
80756 KB |
Output is correct |