#include "bubblesort2.h"
#include <bits/stdc++.h>
using namespace std;
#define MN 1000071
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
int n, q, ma_val;
struct nod {
int mi, ma, nr_activ;
int lazy;
} V[4 * MN];
void prop(int u, int s, int d) {
V[u].mi += V[u].lazy;
V[u].ma += V[u].lazy;
if(s != d) {
V[u << 1].lazy += V[u].lazy;
V[u << 1 | 1].lazy += V[u].lazy;
}
V[u].lazy = 0;
}
void update_range(int l, int r, int v, int u = 1, int s = 1, int d = ma_val) {
if(r < s || d < l) return;
prop(u, s, d);
if(l <= s && d <= r) {
V[u].lazy = v;
prop(u, s, d);
return;
}
update_range(l, r, v, u << 1, s, (s + d) >> 1);
update_range(l, r, v, u << 1 | 1, ((s + d) >> 1) + 1, d);
V[u].mi = min(V[u << 1].mi, V[u << 1 | 1].mi);
V[u].ma = max(V[u << 1].ma, V[u << 1 | 1].ma);
}
void init(int u = 1, int s = 1, int d = ma_val) {
if(s != d) {
init(u << 1, s, (s + d) >> 1);
init(u << 1 | 1, ((s + d) >> 1) + 1, d);
}
V[u].ma = V[u].nr_activ = V[u].lazy = 0;
V[u].mi = INT_MAX;
}
void update_active(int p, int v, int u = 1, int s = 1, int d = ma_val) {
prop(u, s, d);
if(d < p || p < s) return;
if(s == d) {
V[u].nr_activ = v;
if(!V[u].nr_activ) V[u].ma = V[u].lazy = 0, V[u].mi = INT_MAX;
return;
}
update_active(p, v, u << 1, s, (s + d) >> 1);
update_active(p, v, u << 1 | 1, ((s + d) >> 1) + 1, d);
V[u].nr_activ = V[u << 1].nr_activ + V[u << 1 | 1].nr_activ;
V[u].mi = min(V[u << 1].mi, V[u << 1 | 1].mi);
V[u].ma = max(V[u << 1].ma, V[u << 1 | 1].ma);
if(!V[u].nr_activ) V[u].ma = V[u].lazy = 0, V[u].mi = INT_MAX;
}
int query_smaller(int p, int u = 1, int s = 1, int d = ma_val) {
prop(u, s, d);
if(p <= s) return 0;
if(d < p) return V[u].nr_activ;
return query_smaller(p, u << 1, s, (s + d) >> 1) + query_smaller(p, u << 1 | 1, ((s + d) >> 1) + 1, d);
}
void update_val(int p, int v, int u = 1, int s = 1, int d = ma_val) {
prop(u, s, d);
if(p < s || d < p) return;
if(s == d) {
V[u].ma = V[u].mi = v;
return;
}
update_val(p, v, u << 1, s, (s + d) >> 1);
update_val(p, v, u << 1 | 1, ((s + d) >> 1) + 1, d);
V[u].mi = min(V[u << 1].mi, V[u << 1 | 1].mi);
V[u].ma = max(V[u << 1].ma, V[u << 1 | 1].ma);
}
int brute(vector<int> A) {
int re = 0, ok;
do {
ok = 0;
for(int i = 0; i < A.size() - 1; ++i)
if(A[i] > A[i + 1]) swap(A[i], A[i + 1]), ok = 1;
re += ok;
} while (ok);
return re;
}
vector<int> countScans(vector<int> A,vector<int> X,vector<int> VQ){
n = A.size(), q = X.size();
vector<pair<int, int> > Ord;
map<pair<int, int>, int> MNorm;
for(int i = 0; i < n; ++i) Ord.push_back({A[i], i});
for(int i = 0; i < q; ++i)
Ord.push_back({VQ[i], X[i]});
sort(Ord.begin(), Ord.end());
for(int i = 0; i < Ord.size(); ++i) MNorm[Ord[i]] = i + 1;
for(int i = 0; i < n; ++i) A[i] = MNorm[{A[i], i}];
for(int i = 0; i < q; ++i) VQ[i] = MNorm[{VQ[i], X[i]}];
ma_val = Ord.size();
init();
for(int i = 0; i < n; ++i)
update_active(A[i], 1);
for(int i = 0; i < n; ++i)
update_val(A[i], i - query_smaller(A[i]));
vector<int> answer;
for(int i = 0; i < q; ++i) {
int poz = X[i], val = VQ[i];
update_active(A[poz], 0);
update_range(A[poz] + 1, ma_val, 1);
A[poz] = val;
update_active(A[poz], 1);
update_val(A[poz], poz - query_smaller(A[poz]));
update_range(A[poz] + 1, ma_val, -1);
answer.push_back(V[1].ma);
}
return answer;
}
Compilation message
bubblesort2.cpp: In function 'int brute(std::vector<int>)':
bubblesort2.cpp:87:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for(int i = 0; i < A.size() - 1; ++i)
| ~~^~~~~~~~~~~~~~
bubblesort2.cpp: In function 'std::vector<int> countScans(std::vector<int>, std::vector<int>, std::vector<int>)':
bubblesort2.cpp:102:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int i = 0; i < Ord.size(); ++i) MNorm[Ord[i]] = i + 1;
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
8 ms |
768 KB |
Output is correct |
4 |
Correct |
7 ms |
672 KB |
Output is correct |
5 |
Correct |
7 ms |
724 KB |
Output is correct |
6 |
Correct |
6 ms |
724 KB |
Output is correct |
7 |
Correct |
6 ms |
760 KB |
Output is correct |
8 |
Correct |
6 ms |
724 KB |
Output is correct |
9 |
Correct |
5 ms |
724 KB |
Output is correct |
10 |
Correct |
5 ms |
724 KB |
Output is correct |
11 |
Correct |
5 ms |
724 KB |
Output is correct |
12 |
Correct |
6 ms |
748 KB |
Output is correct |
13 |
Correct |
6 ms |
724 KB |
Output is correct |
14 |
Correct |
5 ms |
732 KB |
Output is correct |
15 |
Correct |
8 ms |
724 KB |
Output is correct |
16 |
Correct |
5 ms |
724 KB |
Output is correct |
17 |
Correct |
6 ms |
724 KB |
Output is correct |
18 |
Correct |
6 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
8 ms |
768 KB |
Output is correct |
4 |
Correct |
7 ms |
672 KB |
Output is correct |
5 |
Correct |
7 ms |
724 KB |
Output is correct |
6 |
Correct |
6 ms |
724 KB |
Output is correct |
7 |
Correct |
6 ms |
760 KB |
Output is correct |
8 |
Correct |
6 ms |
724 KB |
Output is correct |
9 |
Correct |
5 ms |
724 KB |
Output is correct |
10 |
Correct |
5 ms |
724 KB |
Output is correct |
11 |
Correct |
5 ms |
724 KB |
Output is correct |
12 |
Correct |
6 ms |
748 KB |
Output is correct |
13 |
Correct |
6 ms |
724 KB |
Output is correct |
14 |
Correct |
5 ms |
732 KB |
Output is correct |
15 |
Correct |
8 ms |
724 KB |
Output is correct |
16 |
Correct |
5 ms |
724 KB |
Output is correct |
17 |
Correct |
6 ms |
724 KB |
Output is correct |
18 |
Correct |
6 ms |
724 KB |
Output is correct |
19 |
Correct |
24 ms |
2004 KB |
Output is correct |
20 |
Correct |
27 ms |
2132 KB |
Output is correct |
21 |
Correct |
25 ms |
2152 KB |
Output is correct |
22 |
Correct |
25 ms |
2152 KB |
Output is correct |
23 |
Correct |
24 ms |
2056 KB |
Output is correct |
24 |
Correct |
23 ms |
2004 KB |
Output is correct |
25 |
Correct |
27 ms |
1956 KB |
Output is correct |
26 |
Correct |
22 ms |
2004 KB |
Output is correct |
27 |
Correct |
23 ms |
1876 KB |
Output is correct |
28 |
Correct |
23 ms |
1900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
3672 KB |
Output is correct |
2 |
Correct |
110 ms |
7692 KB |
Output is correct |
3 |
Correct |
205 ms |
13068 KB |
Output is correct |
4 |
Correct |
230 ms |
13104 KB |
Output is correct |
5 |
Correct |
185 ms |
12864 KB |
Output is correct |
6 |
Correct |
197 ms |
12840 KB |
Output is correct |
7 |
Correct |
203 ms |
12868 KB |
Output is correct |
8 |
Correct |
189 ms |
12844 KB |
Output is correct |
9 |
Correct |
211 ms |
12832 KB |
Output is correct |
10 |
Correct |
199 ms |
10136 KB |
Output is correct |
11 |
Correct |
165 ms |
10176 KB |
Output is correct |
12 |
Correct |
167 ms |
10116 KB |
Output is correct |
13 |
Correct |
160 ms |
10296 KB |
Output is correct |
14 |
Correct |
159 ms |
10208 KB |
Output is correct |
15 |
Correct |
163 ms |
10136 KB |
Output is correct |
16 |
Correct |
154 ms |
10180 KB |
Output is correct |
17 |
Correct |
151 ms |
10236 KB |
Output is correct |
18 |
Correct |
156 ms |
10152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
8 ms |
768 KB |
Output is correct |
4 |
Correct |
7 ms |
672 KB |
Output is correct |
5 |
Correct |
7 ms |
724 KB |
Output is correct |
6 |
Correct |
6 ms |
724 KB |
Output is correct |
7 |
Correct |
6 ms |
760 KB |
Output is correct |
8 |
Correct |
6 ms |
724 KB |
Output is correct |
9 |
Correct |
5 ms |
724 KB |
Output is correct |
10 |
Correct |
5 ms |
724 KB |
Output is correct |
11 |
Correct |
5 ms |
724 KB |
Output is correct |
12 |
Correct |
6 ms |
748 KB |
Output is correct |
13 |
Correct |
6 ms |
724 KB |
Output is correct |
14 |
Correct |
5 ms |
732 KB |
Output is correct |
15 |
Correct |
8 ms |
724 KB |
Output is correct |
16 |
Correct |
5 ms |
724 KB |
Output is correct |
17 |
Correct |
6 ms |
724 KB |
Output is correct |
18 |
Correct |
6 ms |
724 KB |
Output is correct |
19 |
Correct |
24 ms |
2004 KB |
Output is correct |
20 |
Correct |
27 ms |
2132 KB |
Output is correct |
21 |
Correct |
25 ms |
2152 KB |
Output is correct |
22 |
Correct |
25 ms |
2152 KB |
Output is correct |
23 |
Correct |
24 ms |
2056 KB |
Output is correct |
24 |
Correct |
23 ms |
2004 KB |
Output is correct |
25 |
Correct |
27 ms |
1956 KB |
Output is correct |
26 |
Correct |
22 ms |
2004 KB |
Output is correct |
27 |
Correct |
23 ms |
1876 KB |
Output is correct |
28 |
Correct |
23 ms |
1900 KB |
Output is correct |
29 |
Correct |
42 ms |
3672 KB |
Output is correct |
30 |
Correct |
110 ms |
7692 KB |
Output is correct |
31 |
Correct |
205 ms |
13068 KB |
Output is correct |
32 |
Correct |
230 ms |
13104 KB |
Output is correct |
33 |
Correct |
185 ms |
12864 KB |
Output is correct |
34 |
Correct |
197 ms |
12840 KB |
Output is correct |
35 |
Correct |
203 ms |
12868 KB |
Output is correct |
36 |
Correct |
189 ms |
12844 KB |
Output is correct |
37 |
Correct |
211 ms |
12832 KB |
Output is correct |
38 |
Correct |
199 ms |
10136 KB |
Output is correct |
39 |
Correct |
165 ms |
10176 KB |
Output is correct |
40 |
Correct |
167 ms |
10116 KB |
Output is correct |
41 |
Correct |
160 ms |
10296 KB |
Output is correct |
42 |
Correct |
159 ms |
10208 KB |
Output is correct |
43 |
Correct |
163 ms |
10136 KB |
Output is correct |
44 |
Correct |
154 ms |
10180 KB |
Output is correct |
45 |
Correct |
151 ms |
10236 KB |
Output is correct |
46 |
Correct |
156 ms |
10152 KB |
Output is correct |
47 |
Correct |
852 ms |
42700 KB |
Output is correct |
48 |
Correct |
3315 ms |
112188 KB |
Output is correct |
49 |
Correct |
3679 ms |
132196 KB |
Output is correct |
50 |
Correct |
3675 ms |
132152 KB |
Output is correct |
51 |
Correct |
3694 ms |
132140 KB |
Output is correct |
52 |
Correct |
3938 ms |
132204 KB |
Output is correct |
53 |
Correct |
3970 ms |
132200 KB |
Output is correct |
54 |
Correct |
3453 ms |
132372 KB |
Output is correct |
55 |
Correct |
3841 ms |
132308 KB |
Output is correct |
56 |
Correct |
3454 ms |
132208 KB |
Output is correct |
57 |
Correct |
3301 ms |
132320 KB |
Output is correct |
58 |
Correct |
3086 ms |
132284 KB |
Output is correct |
59 |
Correct |
2893 ms |
123300 KB |
Output is correct |
60 |
Correct |
2854 ms |
123144 KB |
Output is correct |
61 |
Correct |
2852 ms |
123128 KB |
Output is correct |
62 |
Correct |
2772 ms |
119180 KB |
Output is correct |
63 |
Correct |
2795 ms |
119172 KB |
Output is correct |
64 |
Correct |
2926 ms |
119276 KB |
Output is correct |
65 |
Correct |
2720 ms |
115088 KB |
Output is correct |
66 |
Correct |
2733 ms |
115136 KB |
Output is correct |
67 |
Correct |
2723 ms |
115116 KB |
Output is correct |