#include "bubblesort2.h"
#include <bits/stdc++.h>
using namespace std;
#define MN 500071
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:86:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | 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:101: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]
101 | for(int i = 0; i < Ord.size(); ++i) MNorm[Ord[i]] = i + 1;
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
7 ms |
852 KB |
Output is correct |
4 |
Correct |
6 ms |
724 KB |
Output is correct |
5 |
Correct |
6 ms |
816 KB |
Output is correct |
6 |
Correct |
6 ms |
708 KB |
Output is correct |
7 |
Correct |
6 ms |
724 KB |
Output is correct |
8 |
Correct |
6 ms |
724 KB |
Output is correct |
9 |
Correct |
7 ms |
724 KB |
Output is correct |
10 |
Correct |
6 ms |
724 KB |
Output is correct |
11 |
Correct |
9 ms |
724 KB |
Output is correct |
12 |
Correct |
7 ms |
736 KB |
Output is correct |
13 |
Correct |
8 ms |
772 KB |
Output is correct |
14 |
Correct |
7 ms |
784 KB |
Output is correct |
15 |
Correct |
8 ms |
724 KB |
Output is correct |
16 |
Correct |
6 ms |
724 KB |
Output is correct |
17 |
Correct |
7 ms |
724 KB |
Output is correct |
18 |
Correct |
6 ms |
744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
7 ms |
852 KB |
Output is correct |
4 |
Correct |
6 ms |
724 KB |
Output is correct |
5 |
Correct |
6 ms |
816 KB |
Output is correct |
6 |
Correct |
6 ms |
708 KB |
Output is correct |
7 |
Correct |
6 ms |
724 KB |
Output is correct |
8 |
Correct |
6 ms |
724 KB |
Output is correct |
9 |
Correct |
7 ms |
724 KB |
Output is correct |
10 |
Correct |
6 ms |
724 KB |
Output is correct |
11 |
Correct |
9 ms |
724 KB |
Output is correct |
12 |
Correct |
7 ms |
736 KB |
Output is correct |
13 |
Correct |
8 ms |
772 KB |
Output is correct |
14 |
Correct |
7 ms |
784 KB |
Output is correct |
15 |
Correct |
8 ms |
724 KB |
Output is correct |
16 |
Correct |
6 ms |
724 KB |
Output is correct |
17 |
Correct |
7 ms |
724 KB |
Output is correct |
18 |
Correct |
6 ms |
744 KB |
Output is correct |
19 |
Correct |
32 ms |
2148 KB |
Output is correct |
20 |
Correct |
29 ms |
2348 KB |
Output is correct |
21 |
Correct |
28 ms |
2352 KB |
Output is correct |
22 |
Correct |
32 ms |
2380 KB |
Output is correct |
23 |
Correct |
28 ms |
2132 KB |
Output is correct |
24 |
Correct |
33 ms |
2216 KB |
Output is correct |
25 |
Correct |
25 ms |
2132 KB |
Output is correct |
26 |
Correct |
25 ms |
2116 KB |
Output is correct |
27 |
Correct |
24 ms |
2116 KB |
Output is correct |
28 |
Correct |
32 ms |
2004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
3768 KB |
Output is correct |
2 |
Correct |
123 ms |
8008 KB |
Output is correct |
3 |
Correct |
217 ms |
13636 KB |
Output is correct |
4 |
Correct |
243 ms |
13556 KB |
Output is correct |
5 |
Correct |
231 ms |
13664 KB |
Output is correct |
6 |
Correct |
205 ms |
13436 KB |
Output is correct |
7 |
Correct |
230 ms |
13492 KB |
Output is correct |
8 |
Correct |
244 ms |
13424 KB |
Output is correct |
9 |
Correct |
231 ms |
13412 KB |
Output is correct |
10 |
Correct |
176 ms |
10880 KB |
Output is correct |
11 |
Correct |
201 ms |
10796 KB |
Output is correct |
12 |
Correct |
180 ms |
10864 KB |
Output is correct |
13 |
Correct |
198 ms |
10812 KB |
Output is correct |
14 |
Correct |
235 ms |
10816 KB |
Output is correct |
15 |
Correct |
173 ms |
10828 KB |
Output is correct |
16 |
Correct |
171 ms |
10848 KB |
Output is correct |
17 |
Correct |
210 ms |
10816 KB |
Output is correct |
18 |
Correct |
210 ms |
10860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
7 ms |
852 KB |
Output is correct |
4 |
Correct |
6 ms |
724 KB |
Output is correct |
5 |
Correct |
6 ms |
816 KB |
Output is correct |
6 |
Correct |
6 ms |
708 KB |
Output is correct |
7 |
Correct |
6 ms |
724 KB |
Output is correct |
8 |
Correct |
6 ms |
724 KB |
Output is correct |
9 |
Correct |
7 ms |
724 KB |
Output is correct |
10 |
Correct |
6 ms |
724 KB |
Output is correct |
11 |
Correct |
9 ms |
724 KB |
Output is correct |
12 |
Correct |
7 ms |
736 KB |
Output is correct |
13 |
Correct |
8 ms |
772 KB |
Output is correct |
14 |
Correct |
7 ms |
784 KB |
Output is correct |
15 |
Correct |
8 ms |
724 KB |
Output is correct |
16 |
Correct |
6 ms |
724 KB |
Output is correct |
17 |
Correct |
7 ms |
724 KB |
Output is correct |
18 |
Correct |
6 ms |
744 KB |
Output is correct |
19 |
Correct |
32 ms |
2148 KB |
Output is correct |
20 |
Correct |
29 ms |
2348 KB |
Output is correct |
21 |
Correct |
28 ms |
2352 KB |
Output is correct |
22 |
Correct |
32 ms |
2380 KB |
Output is correct |
23 |
Correct |
28 ms |
2132 KB |
Output is correct |
24 |
Correct |
33 ms |
2216 KB |
Output is correct |
25 |
Correct |
25 ms |
2132 KB |
Output is correct |
26 |
Correct |
25 ms |
2116 KB |
Output is correct |
27 |
Correct |
24 ms |
2116 KB |
Output is correct |
28 |
Correct |
32 ms |
2004 KB |
Output is correct |
29 |
Correct |
45 ms |
3768 KB |
Output is correct |
30 |
Correct |
123 ms |
8008 KB |
Output is correct |
31 |
Correct |
217 ms |
13636 KB |
Output is correct |
32 |
Correct |
243 ms |
13556 KB |
Output is correct |
33 |
Correct |
231 ms |
13664 KB |
Output is correct |
34 |
Correct |
205 ms |
13436 KB |
Output is correct |
35 |
Correct |
230 ms |
13492 KB |
Output is correct |
36 |
Correct |
244 ms |
13424 KB |
Output is correct |
37 |
Correct |
231 ms |
13412 KB |
Output is correct |
38 |
Correct |
176 ms |
10880 KB |
Output is correct |
39 |
Correct |
201 ms |
10796 KB |
Output is correct |
40 |
Correct |
180 ms |
10864 KB |
Output is correct |
41 |
Correct |
198 ms |
10812 KB |
Output is correct |
42 |
Correct |
235 ms |
10816 KB |
Output is correct |
43 |
Correct |
173 ms |
10828 KB |
Output is correct |
44 |
Correct |
171 ms |
10848 KB |
Output is correct |
45 |
Correct |
210 ms |
10816 KB |
Output is correct |
46 |
Correct |
210 ms |
10860 KB |
Output is correct |
47 |
Correct |
945 ms |
46356 KB |
Output is correct |
48 |
Runtime error |
1704 ms |
225252 KB |
Execution killed with signal 11 |
49 |
Halted |
0 ms |
0 KB |
- |