#include "bits/stdc++.h"
#include "bubblesort2.h"
using namespace std;
template <typename T>
struct segment_tree {
vector<T> tree;
vector<T> lazy;
T neutral;
function<T(T, T)> join;
int size;
segment_tree() {}
segment_tree(int _size, T _neutral, function<T(T, T)> f) {
join = f;
size = _size;
neutral = _neutral;
tree = vector<T>(4 * size, neutral);
lazy = vector<T>(4 * size);
}
void prop(int l, int r, int p) {
if(lazy[p] == 0) return;
if(l < r) {
lazy[p << 1] += lazy[p];
lazy[p << 1 | 1] += lazy[p];
}
tree[p] += lazy[p];
lazy[p] = 0;
}
void point_update(int id, T val) {
point_update(1, size, id, val, 1);
}
void point_update(int l, int r, int x, T val, int p) {
prop(l, r, p);
if(l > x || r < x) {
return;
}
if(l == r) {
tree[p] = val;
return;
}
int middle = l + r >> 1;
point_update(l, middle, x, val, p << 1);
point_update(middle+1, r, x, val, p << 1 | 1);
tree[p] = join(tree[p << 1], tree[p << 1 | 1]);
}
void range_add(int L, int R, T val) {
if(L > R) {
return;
}
range_add(1, size, L, R, val, 1);
}
void range_add(int l, int r, int L, int R, T val, int p) {
prop(l, r, p);
if(L > r || l > R) return;
if(L <= l && R >= r) {
lazy[p] += val;
prop(l, r, p);
return;
}
int middle = l + r >> 1;
range_add(l, middle, L, R, val, p << 1);
range_add(middle+1, r, L, R, val, p << 1 | 1);
tree[p] = join(tree[p << 1], tree[p << 1 |1]);
}
T get(int id) {
return query(id, id);
}
T query(int L, int R) {
if(L > R) {
return neutral;
}
return query(1, size, L, R, 1);
}
T query(int l, int r, int L, int R, int p) {
prop(l, r, p);
if(L > r || l > R) return neutral;
if(L <= l && R >= r) return tree[p];
int middle = l + r >> 1;
T lft = query(l, middle, L, R, p << 1);
T rgh = query(middle+1, r, L, R, p << 1 | 1);
return join(lft, rgh);
}
};
vector<int> countScans(vector<int> A, vector<int> X, vector<int> V){
int Q = (int)X.size(), N = (int)A.size();
vector<int> answer(Q);
vector<array<int, 4>> coords;
for(int i = 0; i < N; ++i) {
coords.push_back({A[i], i, i, 0});
}
for(int i = 0; i < Q; ++i) {
coords.push_back({V[i], X[i], i, 1});
}
sort(coords.begin(), coords.end());
for(int i = 0; i < (int)coords.size(); ++i) {
if(coords[i][3] == 0) {
A[coords[i][2]] = i+1;
} else {
V[coords[i][2]] = i+1;
}
}
segment_tree<int> t(N + Q, -1e9, [](int x, int y) -> int {
return x > y ? x : y;
});
segment_tree<int> bruh(N + Q, 0, [](int x, int y) -> int {
return x+y;
});
vector<int> id(N + Q + 1);
for(int i = 0; i < N; ++i) {
id[A[i]] = i;
}
vector<int> save_A = A;
sort(A.begin(), A.end());
for(int i = 0; i < N; ++i) {
t.point_update(A[i], id[A[i]] - i);
bruh.point_update(A[i], 1);
}
A = save_A;
for(int i = 0; i < Q; ++i) {
int prv = A[X[i]];
int nxt = V[i];
A[X[i]] = V[i];
id[nxt] = id[prv];
bruh.point_update(prv, 0);
bruh.point_update(nxt, 1);
if(nxt > prv) {
t.range_add(prv, nxt, 1);
t.point_update(nxt, id[nxt] - bruh.query(1, nxt-1));
} else {
t.range_add(nxt, prv, -1);
t.point_update(nxt, id[nxt] - bruh.query(1, nxt-1));
}
t.point_update(prv, -1e9);
answer[i] = t.query(1, N + Q);
}
return answer;
}
Compilation message
bubblesort2.cpp: In instantiation of 'void segment_tree<T>::point_update(int, int, int, T, int) [with T = int]':
bubblesort2.cpp:31:3: required from 'void segment_tree<T>::point_update(int, T) [with T = int]'
bubblesort2.cpp:122:36: required from here
bubblesort2.cpp:43:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
43 | int middle = l + r >> 1;
| ~~^~~
bubblesort2.cpp: In instantiation of 'void segment_tree<T>::range_add(int, int, int, int, T, int) [with T = int]':
bubblesort2.cpp:53:3: required from 'void segment_tree<T>::range_add(int, int, T) [with T = int]'
bubblesort2.cpp:134:27: required from here
bubblesort2.cpp:64:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
64 | int middle = l + r >> 1;
| ~~^~~
bubblesort2.cpp: In instantiation of 'T segment_tree<T>::query(int, int, int, int, int) [with T = int]':
bubblesort2.cpp:78:32: required from 'T segment_tree<T>::query(int, int) [with T = int]'
bubblesort2.cpp:135:53: required from here
bubblesort2.cpp:85:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
85 | int middle = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
460 KB |
Output is correct |
3 |
Correct |
7 ms |
716 KB |
Output is correct |
4 |
Correct |
7 ms |
692 KB |
Output is correct |
5 |
Correct |
7 ms |
716 KB |
Output is correct |
6 |
Correct |
7 ms |
844 KB |
Output is correct |
7 |
Correct |
7 ms |
716 KB |
Output is correct |
8 |
Correct |
7 ms |
768 KB |
Output is correct |
9 |
Correct |
7 ms |
716 KB |
Output is correct |
10 |
Correct |
7 ms |
716 KB |
Output is correct |
11 |
Correct |
7 ms |
764 KB |
Output is correct |
12 |
Correct |
7 ms |
688 KB |
Output is correct |
13 |
Correct |
7 ms |
716 KB |
Output is correct |
14 |
Correct |
8 ms |
716 KB |
Output is correct |
15 |
Correct |
7 ms |
716 KB |
Output is correct |
16 |
Correct |
7 ms |
716 KB |
Output is correct |
17 |
Correct |
7 ms |
692 KB |
Output is correct |
18 |
Correct |
7 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
460 KB |
Output is correct |
3 |
Correct |
7 ms |
716 KB |
Output is correct |
4 |
Correct |
7 ms |
692 KB |
Output is correct |
5 |
Correct |
7 ms |
716 KB |
Output is correct |
6 |
Correct |
7 ms |
844 KB |
Output is correct |
7 |
Correct |
7 ms |
716 KB |
Output is correct |
8 |
Correct |
7 ms |
768 KB |
Output is correct |
9 |
Correct |
7 ms |
716 KB |
Output is correct |
10 |
Correct |
7 ms |
716 KB |
Output is correct |
11 |
Correct |
7 ms |
764 KB |
Output is correct |
12 |
Correct |
7 ms |
688 KB |
Output is correct |
13 |
Correct |
7 ms |
716 KB |
Output is correct |
14 |
Correct |
8 ms |
716 KB |
Output is correct |
15 |
Correct |
7 ms |
716 KB |
Output is correct |
16 |
Correct |
7 ms |
716 KB |
Output is correct |
17 |
Correct |
7 ms |
692 KB |
Output is correct |
18 |
Correct |
7 ms |
716 KB |
Output is correct |
19 |
Correct |
27 ms |
1900 KB |
Output is correct |
20 |
Correct |
31 ms |
1960 KB |
Output is correct |
21 |
Correct |
31 ms |
1992 KB |
Output is correct |
22 |
Correct |
31 ms |
1992 KB |
Output is correct |
23 |
Correct |
29 ms |
1992 KB |
Output is correct |
24 |
Correct |
30 ms |
2032 KB |
Output is correct |
25 |
Correct |
31 ms |
2020 KB |
Output is correct |
26 |
Correct |
30 ms |
1992 KB |
Output is correct |
27 |
Correct |
30 ms |
1992 KB |
Output is correct |
28 |
Correct |
31 ms |
2024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
3016 KB |
Output is correct |
2 |
Correct |
124 ms |
6668 KB |
Output is correct |
3 |
Correct |
236 ms |
10164 KB |
Output is correct |
4 |
Correct |
240 ms |
10272 KB |
Output is correct |
5 |
Correct |
232 ms |
10292 KB |
Output is correct |
6 |
Correct |
232 ms |
10144 KB |
Output is correct |
7 |
Correct |
240 ms |
10212 KB |
Output is correct |
8 |
Correct |
253 ms |
10212 KB |
Output is correct |
9 |
Correct |
235 ms |
10260 KB |
Output is correct |
10 |
Correct |
209 ms |
10164 KB |
Output is correct |
11 |
Correct |
209 ms |
10216 KB |
Output is correct |
12 |
Correct |
206 ms |
10088 KB |
Output is correct |
13 |
Correct |
205 ms |
10292 KB |
Output is correct |
14 |
Correct |
212 ms |
10088 KB |
Output is correct |
15 |
Correct |
210 ms |
10288 KB |
Output is correct |
16 |
Correct |
206 ms |
10200 KB |
Output is correct |
17 |
Correct |
205 ms |
10212 KB |
Output is correct |
18 |
Correct |
208 ms |
10212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
460 KB |
Output is correct |
3 |
Correct |
7 ms |
716 KB |
Output is correct |
4 |
Correct |
7 ms |
692 KB |
Output is correct |
5 |
Correct |
7 ms |
716 KB |
Output is correct |
6 |
Correct |
7 ms |
844 KB |
Output is correct |
7 |
Correct |
7 ms |
716 KB |
Output is correct |
8 |
Correct |
7 ms |
768 KB |
Output is correct |
9 |
Correct |
7 ms |
716 KB |
Output is correct |
10 |
Correct |
7 ms |
716 KB |
Output is correct |
11 |
Correct |
7 ms |
764 KB |
Output is correct |
12 |
Correct |
7 ms |
688 KB |
Output is correct |
13 |
Correct |
7 ms |
716 KB |
Output is correct |
14 |
Correct |
8 ms |
716 KB |
Output is correct |
15 |
Correct |
7 ms |
716 KB |
Output is correct |
16 |
Correct |
7 ms |
716 KB |
Output is correct |
17 |
Correct |
7 ms |
692 KB |
Output is correct |
18 |
Correct |
7 ms |
716 KB |
Output is correct |
19 |
Correct |
27 ms |
1900 KB |
Output is correct |
20 |
Correct |
31 ms |
1960 KB |
Output is correct |
21 |
Correct |
31 ms |
1992 KB |
Output is correct |
22 |
Correct |
31 ms |
1992 KB |
Output is correct |
23 |
Correct |
29 ms |
1992 KB |
Output is correct |
24 |
Correct |
30 ms |
2032 KB |
Output is correct |
25 |
Correct |
31 ms |
2020 KB |
Output is correct |
26 |
Correct |
30 ms |
1992 KB |
Output is correct |
27 |
Correct |
30 ms |
1992 KB |
Output is correct |
28 |
Correct |
31 ms |
2024 KB |
Output is correct |
29 |
Correct |
33 ms |
3016 KB |
Output is correct |
30 |
Correct |
124 ms |
6668 KB |
Output is correct |
31 |
Correct |
236 ms |
10164 KB |
Output is correct |
32 |
Correct |
240 ms |
10272 KB |
Output is correct |
33 |
Correct |
232 ms |
10292 KB |
Output is correct |
34 |
Correct |
232 ms |
10144 KB |
Output is correct |
35 |
Correct |
240 ms |
10212 KB |
Output is correct |
36 |
Correct |
253 ms |
10212 KB |
Output is correct |
37 |
Correct |
235 ms |
10260 KB |
Output is correct |
38 |
Correct |
209 ms |
10164 KB |
Output is correct |
39 |
Correct |
209 ms |
10216 KB |
Output is correct |
40 |
Correct |
206 ms |
10088 KB |
Output is correct |
41 |
Correct |
205 ms |
10292 KB |
Output is correct |
42 |
Correct |
212 ms |
10088 KB |
Output is correct |
43 |
Correct |
210 ms |
10288 KB |
Output is correct |
44 |
Correct |
206 ms |
10200 KB |
Output is correct |
45 |
Correct |
205 ms |
10212 KB |
Output is correct |
46 |
Correct |
208 ms |
10212 KB |
Output is correct |
47 |
Correct |
828 ms |
29456 KB |
Output is correct |
48 |
Correct |
3344 ms |
89860 KB |
Output is correct |
49 |
Correct |
3534 ms |
111056 KB |
Output is correct |
50 |
Correct |
3522 ms |
111064 KB |
Output is correct |
51 |
Correct |
3569 ms |
111116 KB |
Output is correct |
52 |
Correct |
3554 ms |
111160 KB |
Output is correct |
53 |
Correct |
3577 ms |
111064 KB |
Output is correct |
54 |
Correct |
3540 ms |
111208 KB |
Output is correct |
55 |
Correct |
3641 ms |
111248 KB |
Output is correct |
56 |
Correct |
3626 ms |
111184 KB |
Output is correct |
57 |
Correct |
3744 ms |
111224 KB |
Output is correct |
58 |
Correct |
3720 ms |
111200 KB |
Output is correct |
59 |
Correct |
3368 ms |
109884 KB |
Output is correct |
60 |
Correct |
3565 ms |
109880 KB |
Output is correct |
61 |
Correct |
3521 ms |
109876 KB |
Output is correct |
62 |
Correct |
3238 ms |
109740 KB |
Output is correct |
63 |
Correct |
3375 ms |
109744 KB |
Output is correct |
64 |
Correct |
3403 ms |
109740 KB |
Output is correct |
65 |
Correct |
3112 ms |
109528 KB |
Output is correct |
66 |
Correct |
3088 ms |
109592 KB |
Output is correct |
67 |
Correct |
3098 ms |
109588 KB |
Output is correct |