#include<bits/stdc++.h>
#include "bubblesort2.h"
using namespace std;
#define endl "\n"
typedef long long int ll;
const int MOD = 1e9 + 7;
struct segTree{
vector<int> v;
vector<int> upd;
int siz;
void init(int n){
siz = 1;
while(siz < n) siz *= 2;
v.assign(2 * siz, 0);
upd.assign(2 * siz, 0);
}
void set(int l, int r, int u, int x, int lx, int rx){
if(lx >= l && rx <= r){
upd[x] += u;
v[x] += u;
return;
}
if(lx >= r || rx <= l) return;
int m = (lx + rx) / 2;
set(l, r, u, 2 * x + 1, lx, m);
set(l, r, u, 2 * x + 2, m, rx);
v[x] = max(v[2 * x + 1], v[2 * x + 2]) + upd[x];
}
void set(int l, int r, int u){
set(l, r, u, 0, 0, siz);
}
int get_ans(){
return v[0];
}
};
vector<int> countScans(vector<int> A,vector<int> V1,vector<int> V2){
int n = (int)A.size();
int q = (int)V1.size();
vector<int> all(n + q);
for(int i = 0; i < n; i++) all[i] = A[i];
for(int i = 0; i < q; i++) all[i + n] = V2[i];
sort(all.begin(), all.end());
unordered_map<int, int> m;
int curr = 0;
for(int i = 0; i < n + q; i++){
if(i == 0) {m[all[i]] = curr; curr++;}
else{
if(all[i] == all[i-1]) continue;
m[all[i]] = curr;
curr++;
}
}
segTree st; st.init(curr);
vector<set<int>> pos(curr);
for(int i = 0; i < n; i++){
pos[m[A[i]]].insert(i);
st.set(m[A[i]], curr, -1);
}
for(int i = 0; i < curr; i++){
if((int)pos[i].size() > 0){
st.set(i, i + 1, *pos[i].rbegin()+1);
}
}
vector<int> ans(q);
for(int qq = 0; qq < q; qq++){
int i = V1[qq];
int lst = A[i];
int nw = V2[qq];
A[i] = nw;
lst = m[lst];
nw = m[nw];
int lst_pos = *pos[lst].rbegin();
pos[lst].erase(i);
if((int)pos[lst].size() > 0) st.set(lst, lst + 1, (*pos[lst].rbegin() - lst_pos));
else st.set(lst, lst + 1, -lst_pos - 1);
st.set(lst, curr, 1);
if((int)pos[nw].size() == 0){
pos[nw].insert(i);
st.set(nw, nw + 1, i+1);
}else{
lst_pos = *pos[nw].rbegin();
pos[nw].insert(i);
st.set(nw, nw + 1, (*pos[nw].rbegin() - lst_pos));
}
st.set(nw, curr, -1);
ans[qq] = st.get_ans();
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
4 ms |
852 KB |
Output is correct |
4 |
Correct |
4 ms |
852 KB |
Output is correct |
5 |
Correct |
4 ms |
852 KB |
Output is correct |
6 |
Correct |
4 ms |
852 KB |
Output is correct |
7 |
Correct |
4 ms |
852 KB |
Output is correct |
8 |
Correct |
4 ms |
852 KB |
Output is correct |
9 |
Correct |
4 ms |
852 KB |
Output is correct |
10 |
Correct |
4 ms |
852 KB |
Output is correct |
11 |
Correct |
5 ms |
852 KB |
Output is correct |
12 |
Correct |
5 ms |
724 KB |
Output is correct |
13 |
Correct |
5 ms |
724 KB |
Output is correct |
14 |
Correct |
5 ms |
724 KB |
Output is correct |
15 |
Correct |
5 ms |
724 KB |
Output is correct |
16 |
Correct |
7 ms |
776 KB |
Output is correct |
17 |
Correct |
5 ms |
724 KB |
Output is correct |
18 |
Correct |
5 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
4 ms |
852 KB |
Output is correct |
4 |
Correct |
4 ms |
852 KB |
Output is correct |
5 |
Correct |
4 ms |
852 KB |
Output is correct |
6 |
Correct |
4 ms |
852 KB |
Output is correct |
7 |
Correct |
4 ms |
852 KB |
Output is correct |
8 |
Correct |
4 ms |
852 KB |
Output is correct |
9 |
Correct |
4 ms |
852 KB |
Output is correct |
10 |
Correct |
4 ms |
852 KB |
Output is correct |
11 |
Correct |
5 ms |
852 KB |
Output is correct |
12 |
Correct |
5 ms |
724 KB |
Output is correct |
13 |
Correct |
5 ms |
724 KB |
Output is correct |
14 |
Correct |
5 ms |
724 KB |
Output is correct |
15 |
Correct |
5 ms |
724 KB |
Output is correct |
16 |
Correct |
7 ms |
776 KB |
Output is correct |
17 |
Correct |
5 ms |
724 KB |
Output is correct |
18 |
Correct |
5 ms |
724 KB |
Output is correct |
19 |
Correct |
19 ms |
2396 KB |
Output is correct |
20 |
Correct |
18 ms |
2644 KB |
Output is correct |
21 |
Correct |
19 ms |
2644 KB |
Output is correct |
22 |
Correct |
18 ms |
2564 KB |
Output is correct |
23 |
Correct |
19 ms |
2500 KB |
Output is correct |
24 |
Correct |
23 ms |
2472 KB |
Output is correct |
25 |
Correct |
22 ms |
2388 KB |
Output is correct |
26 |
Correct |
18 ms |
2388 KB |
Output is correct |
27 |
Correct |
18 ms |
2260 KB |
Output is correct |
28 |
Correct |
17 ms |
2324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
1876 KB |
Output is correct |
2 |
Correct |
43 ms |
3168 KB |
Output is correct |
3 |
Correct |
74 ms |
4416 KB |
Output is correct |
4 |
Correct |
71 ms |
4304 KB |
Output is correct |
5 |
Correct |
80 ms |
4436 KB |
Output is correct |
6 |
Correct |
75 ms |
4300 KB |
Output is correct |
7 |
Correct |
117 ms |
4396 KB |
Output is correct |
8 |
Correct |
67 ms |
4300 KB |
Output is correct |
9 |
Correct |
70 ms |
4528 KB |
Output is correct |
10 |
Correct |
54 ms |
4356 KB |
Output is correct |
11 |
Correct |
55 ms |
4304 KB |
Output is correct |
12 |
Correct |
58 ms |
4416 KB |
Output is correct |
13 |
Correct |
67 ms |
4412 KB |
Output is correct |
14 |
Correct |
58 ms |
4300 KB |
Output is correct |
15 |
Correct |
58 ms |
4292 KB |
Output is correct |
16 |
Correct |
49 ms |
4352 KB |
Output is correct |
17 |
Correct |
47 ms |
4356 KB |
Output is correct |
18 |
Correct |
50 ms |
4428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
4 ms |
852 KB |
Output is correct |
4 |
Correct |
4 ms |
852 KB |
Output is correct |
5 |
Correct |
4 ms |
852 KB |
Output is correct |
6 |
Correct |
4 ms |
852 KB |
Output is correct |
7 |
Correct |
4 ms |
852 KB |
Output is correct |
8 |
Correct |
4 ms |
852 KB |
Output is correct |
9 |
Correct |
4 ms |
852 KB |
Output is correct |
10 |
Correct |
4 ms |
852 KB |
Output is correct |
11 |
Correct |
5 ms |
852 KB |
Output is correct |
12 |
Correct |
5 ms |
724 KB |
Output is correct |
13 |
Correct |
5 ms |
724 KB |
Output is correct |
14 |
Correct |
5 ms |
724 KB |
Output is correct |
15 |
Correct |
5 ms |
724 KB |
Output is correct |
16 |
Correct |
7 ms |
776 KB |
Output is correct |
17 |
Correct |
5 ms |
724 KB |
Output is correct |
18 |
Correct |
5 ms |
724 KB |
Output is correct |
19 |
Correct |
19 ms |
2396 KB |
Output is correct |
20 |
Correct |
18 ms |
2644 KB |
Output is correct |
21 |
Correct |
19 ms |
2644 KB |
Output is correct |
22 |
Correct |
18 ms |
2564 KB |
Output is correct |
23 |
Correct |
19 ms |
2500 KB |
Output is correct |
24 |
Correct |
23 ms |
2472 KB |
Output is correct |
25 |
Correct |
22 ms |
2388 KB |
Output is correct |
26 |
Correct |
18 ms |
2388 KB |
Output is correct |
27 |
Correct |
18 ms |
2260 KB |
Output is correct |
28 |
Correct |
17 ms |
2324 KB |
Output is correct |
29 |
Correct |
13 ms |
1876 KB |
Output is correct |
30 |
Correct |
43 ms |
3168 KB |
Output is correct |
31 |
Correct |
74 ms |
4416 KB |
Output is correct |
32 |
Correct |
71 ms |
4304 KB |
Output is correct |
33 |
Correct |
80 ms |
4436 KB |
Output is correct |
34 |
Correct |
75 ms |
4300 KB |
Output is correct |
35 |
Correct |
117 ms |
4396 KB |
Output is correct |
36 |
Correct |
67 ms |
4300 KB |
Output is correct |
37 |
Correct |
70 ms |
4528 KB |
Output is correct |
38 |
Correct |
54 ms |
4356 KB |
Output is correct |
39 |
Correct |
55 ms |
4304 KB |
Output is correct |
40 |
Correct |
58 ms |
4416 KB |
Output is correct |
41 |
Correct |
67 ms |
4412 KB |
Output is correct |
42 |
Correct |
58 ms |
4300 KB |
Output is correct |
43 |
Correct |
58 ms |
4292 KB |
Output is correct |
44 |
Correct |
49 ms |
4352 KB |
Output is correct |
45 |
Correct |
47 ms |
4356 KB |
Output is correct |
46 |
Correct |
50 ms |
4428 KB |
Output is correct |
47 |
Correct |
662 ms |
47168 KB |
Output is correct |
48 |
Correct |
2624 ms |
135972 KB |
Output is correct |
49 |
Correct |
2862 ms |
147304 KB |
Output is correct |
50 |
Correct |
2899 ms |
147360 KB |
Output is correct |
51 |
Correct |
2805 ms |
147368 KB |
Output is correct |
52 |
Correct |
2752 ms |
147420 KB |
Output is correct |
53 |
Correct |
2822 ms |
147356 KB |
Output is correct |
54 |
Correct |
2534 ms |
147324 KB |
Output is correct |
55 |
Correct |
2725 ms |
147284 KB |
Output is correct |
56 |
Correct |
2594 ms |
147336 KB |
Output is correct |
57 |
Correct |
2670 ms |
147304 KB |
Output is correct |
58 |
Correct |
2577 ms |
147324 KB |
Output is correct |
59 |
Correct |
2125 ms |
137640 KB |
Output is correct |
60 |
Correct |
2217 ms |
137668 KB |
Output is correct |
61 |
Correct |
2156 ms |
137592 KB |
Output is correct |
62 |
Correct |
2040 ms |
132724 KB |
Output is correct |
63 |
Correct |
2033 ms |
132820 KB |
Output is correct |
64 |
Correct |
2050 ms |
132716 KB |
Output is correct |
65 |
Correct |
1898 ms |
127852 KB |
Output is correct |
66 |
Correct |
1911 ms |
127964 KB |
Output is correct |
67 |
Correct |
1877 ms |
127856 KB |
Output is correct |