#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define dl double
#define st first
#define nd second
#define II pair <int, int>
#include "bubblesort2.h"
using namespace std;
mt19937 rnd(chrono :: steady_clock :: now().time_since_epoch().count());
struct Treap {
Treap *left = nullptr, *right = nullptr;
int x, y, p, val, ma, siz = 1, lazy = 0;
Treap (int _x = 0, int _p = 0, int _val = 0) : x(_x), y(rnd()), p(_p), val(_val), ma(_val) {};
void push() {
if (left != nullptr)
left -> lazy += lazy;
if (right != nullptr)
right -> lazy += lazy;
val += lazy;
ma += lazy;
lazy = 0;
}
void update() {
push();
if (left != nullptr)
left -> push();
if (right != nullptr)
right -> push();
ma = max({val, (left != nullptr ? left -> ma : val), (right != nullptr ? right -> ma : val)});
siz = (left != nullptr ? left -> siz : 0) + (right != nullptr ? right -> siz : 0) + 1;
}
};
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 -> push();
t2 -> push();
if (t1 -> y > t2 -> y) {
t1 -> right = Merge(t1 -> right, t2);
t1 -> update();
return t1;
}
t2 -> left = Merge(t1, t2 -> left);
t2 -> update();
return t2;
}
pair <Treap*, Treap*> split(Treap *t, int x, int p) {
if (t == nullptr)
return {nullptr, nullptr};
t -> push();
if (t -> x > x || (t -> x == x && t -> p >= p)) {
auto a = split(t -> left, x, p);
t -> left = a.nd;
t -> update();
return {a.st, t};
}
auto a = split(t -> right, x, p);
t -> right = a.st;
t -> update();
return {t, a.nd};
}
void add(Treap *t, int val) {
if (t != nullptr)
t -> lazy += val;
}
Treap *root;
vector <int> countScans(vector <int> a, vector <int> x, vector <int> v){
int n = a.size(), q = x.size();
vector <II> b(n);
for (int i = 0; i < n; i ++)
b[i] = {a[i], i};
sort(b.begin(), b.end());
for (int i = 0; i < n; i ++)
root = Merge(root, new Treap(b[i].st, b[i].nd, b[i].nd - i));
vector <int> ans(q);
for (int t = 0; t < q; t ++) {
int pos = x[t], val = v[t];
if (val < a[pos]) {
auto za = split(root, val, pos), zb = split(za.nd, a[pos], pos), zc = split(zb.nd, a[pos], pos + 1);
add(zb.st, -1);
root = Merge(Merge(za.st, new Treap(val, pos, pos - siz(za.st))), Merge(zb.st, zc.nd));
}
else {
auto za = split(root, a[pos], pos), zc = split(za.nd, a[pos], pos + 1), zb = split(zc.nd, val, pos);
add(zb.st, 1);
root = Merge(Merge(za.st, zb.st), Merge(new Treap(val, pos, pos - siz(za.st) - siz(zb.st)), zb.nd));
}
a[pos] = val;
root -> push();
ans[t] = root -> ma;
}
return ans;
}
Compilation message
bubblesort2.cpp: In function 'std::vector<int> countScans(std::vector<int>, std::vector<int>, std::vector<int>)':
bubblesort2.cpp:88:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
88 | for (int i = 0; i < n; i ++)
| ^~~
bubblesort2.cpp:90:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
90 | vector <int> ans(q);
| ^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
604 KB |
Output is correct |
4 |
Correct |
4 ms |
752 KB |
Output is correct |
5 |
Correct |
4 ms |
604 KB |
Output is correct |
6 |
Correct |
4 ms |
604 KB |
Output is correct |
7 |
Correct |
4 ms |
600 KB |
Output is correct |
8 |
Correct |
4 ms |
604 KB |
Output is correct |
9 |
Correct |
5 ms |
604 KB |
Output is correct |
10 |
Correct |
5 ms |
856 KB |
Output is correct |
11 |
Correct |
4 ms |
600 KB |
Output is correct |
12 |
Correct |
4 ms |
604 KB |
Output is correct |
13 |
Correct |
4 ms |
600 KB |
Output is correct |
14 |
Correct |
4 ms |
600 KB |
Output is correct |
15 |
Correct |
4 ms |
604 KB |
Output is correct |
16 |
Correct |
4 ms |
604 KB |
Output is correct |
17 |
Correct |
5 ms |
604 KB |
Output is correct |
18 |
Correct |
4 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
604 KB |
Output is correct |
4 |
Correct |
4 ms |
752 KB |
Output is correct |
5 |
Correct |
4 ms |
604 KB |
Output is correct |
6 |
Correct |
4 ms |
604 KB |
Output is correct |
7 |
Correct |
4 ms |
600 KB |
Output is correct |
8 |
Correct |
4 ms |
604 KB |
Output is correct |
9 |
Correct |
5 ms |
604 KB |
Output is correct |
10 |
Correct |
5 ms |
856 KB |
Output is correct |
11 |
Correct |
4 ms |
600 KB |
Output is correct |
12 |
Correct |
4 ms |
604 KB |
Output is correct |
13 |
Correct |
4 ms |
600 KB |
Output is correct |
14 |
Correct |
4 ms |
600 KB |
Output is correct |
15 |
Correct |
4 ms |
604 KB |
Output is correct |
16 |
Correct |
4 ms |
604 KB |
Output is correct |
17 |
Correct |
5 ms |
604 KB |
Output is correct |
18 |
Correct |
4 ms |
604 KB |
Output is correct |
19 |
Correct |
17 ms |
1592 KB |
Output is correct |
20 |
Correct |
17 ms |
1884 KB |
Output is correct |
21 |
Correct |
17 ms |
1880 KB |
Output is correct |
22 |
Correct |
16 ms |
1788 KB |
Output is correct |
23 |
Correct |
16 ms |
1884 KB |
Output is correct |
24 |
Correct |
17 ms |
1672 KB |
Output is correct |
25 |
Correct |
16 ms |
1884 KB |
Output is correct |
26 |
Correct |
16 ms |
1884 KB |
Output is correct |
27 |
Correct |
16 ms |
2128 KB |
Output is correct |
28 |
Correct |
18 ms |
1888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
2908 KB |
Output is correct |
2 |
Correct |
66 ms |
5684 KB |
Output is correct |
3 |
Correct |
125 ms |
8820 KB |
Output is correct |
4 |
Correct |
127 ms |
8724 KB |
Output is correct |
5 |
Correct |
130 ms |
8788 KB |
Output is correct |
6 |
Correct |
124 ms |
8868 KB |
Output is correct |
7 |
Correct |
125 ms |
8788 KB |
Output is correct |
8 |
Correct |
120 ms |
8784 KB |
Output is correct |
9 |
Correct |
130 ms |
8888 KB |
Output is correct |
10 |
Correct |
94 ms |
8788 KB |
Output is correct |
11 |
Correct |
97 ms |
8896 KB |
Output is correct |
12 |
Correct |
86 ms |
8788 KB |
Output is correct |
13 |
Correct |
100 ms |
8788 KB |
Output is correct |
14 |
Correct |
96 ms |
8740 KB |
Output is correct |
15 |
Correct |
93 ms |
8784 KB |
Output is correct |
16 |
Correct |
100 ms |
8824 KB |
Output is correct |
17 |
Correct |
96 ms |
8944 KB |
Output is correct |
18 |
Correct |
81 ms |
8748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
604 KB |
Output is correct |
4 |
Correct |
4 ms |
752 KB |
Output is correct |
5 |
Correct |
4 ms |
604 KB |
Output is correct |
6 |
Correct |
4 ms |
604 KB |
Output is correct |
7 |
Correct |
4 ms |
600 KB |
Output is correct |
8 |
Correct |
4 ms |
604 KB |
Output is correct |
9 |
Correct |
5 ms |
604 KB |
Output is correct |
10 |
Correct |
5 ms |
856 KB |
Output is correct |
11 |
Correct |
4 ms |
600 KB |
Output is correct |
12 |
Correct |
4 ms |
604 KB |
Output is correct |
13 |
Correct |
4 ms |
600 KB |
Output is correct |
14 |
Correct |
4 ms |
600 KB |
Output is correct |
15 |
Correct |
4 ms |
604 KB |
Output is correct |
16 |
Correct |
4 ms |
604 KB |
Output is correct |
17 |
Correct |
5 ms |
604 KB |
Output is correct |
18 |
Correct |
4 ms |
604 KB |
Output is correct |
19 |
Correct |
17 ms |
1592 KB |
Output is correct |
20 |
Correct |
17 ms |
1884 KB |
Output is correct |
21 |
Correct |
17 ms |
1880 KB |
Output is correct |
22 |
Correct |
16 ms |
1788 KB |
Output is correct |
23 |
Correct |
16 ms |
1884 KB |
Output is correct |
24 |
Correct |
17 ms |
1672 KB |
Output is correct |
25 |
Correct |
16 ms |
1884 KB |
Output is correct |
26 |
Correct |
16 ms |
1884 KB |
Output is correct |
27 |
Correct |
16 ms |
2128 KB |
Output is correct |
28 |
Correct |
18 ms |
1888 KB |
Output is correct |
29 |
Correct |
15 ms |
2908 KB |
Output is correct |
30 |
Correct |
66 ms |
5684 KB |
Output is correct |
31 |
Correct |
125 ms |
8820 KB |
Output is correct |
32 |
Correct |
127 ms |
8724 KB |
Output is correct |
33 |
Correct |
130 ms |
8788 KB |
Output is correct |
34 |
Correct |
124 ms |
8868 KB |
Output is correct |
35 |
Correct |
125 ms |
8788 KB |
Output is correct |
36 |
Correct |
120 ms |
8784 KB |
Output is correct |
37 |
Correct |
130 ms |
8888 KB |
Output is correct |
38 |
Correct |
94 ms |
8788 KB |
Output is correct |
39 |
Correct |
97 ms |
8896 KB |
Output is correct |
40 |
Correct |
86 ms |
8788 KB |
Output is correct |
41 |
Correct |
100 ms |
8788 KB |
Output is correct |
42 |
Correct |
96 ms |
8740 KB |
Output is correct |
43 |
Correct |
93 ms |
8784 KB |
Output is correct |
44 |
Correct |
100 ms |
8824 KB |
Output is correct |
45 |
Correct |
96 ms |
8944 KB |
Output is correct |
46 |
Correct |
81 ms |
8748 KB |
Output is correct |
47 |
Correct |
554 ms |
26288 KB |
Output is correct |
48 |
Correct |
2509 ms |
79608 KB |
Output is correct |
49 |
Correct |
2550 ms |
87168 KB |
Output is correct |
50 |
Correct |
2757 ms |
87452 KB |
Output is correct |
51 |
Correct |
2677 ms |
87092 KB |
Output is correct |
52 |
Correct |
2719 ms |
87276 KB |
Output is correct |
53 |
Correct |
2640 ms |
87208 KB |
Output is correct |
54 |
Correct |
2539 ms |
87580 KB |
Output is correct |
55 |
Correct |
2593 ms |
87348 KB |
Output is correct |
56 |
Correct |
2537 ms |
87328 KB |
Output is correct |
57 |
Correct |
2665 ms |
87296 KB |
Output is correct |
58 |
Correct |
2593 ms |
87088 KB |
Output is correct |
59 |
Correct |
2240 ms |
86648 KB |
Output is correct |
60 |
Correct |
2279 ms |
86708 KB |
Output is correct |
61 |
Correct |
2302 ms |
86612 KB |
Output is correct |
62 |
Correct |
2134 ms |
86612 KB |
Output is correct |
63 |
Correct |
2111 ms |
86616 KB |
Output is correct |
64 |
Correct |
2112 ms |
86728 KB |
Output is correct |
65 |
Correct |
1914 ms |
86696 KB |
Output is correct |
66 |
Correct |
1956 ms |
86460 KB |
Output is correct |
67 |
Correct |
1884 ms |
86660 KB |
Output is correct |