#include <bits/stdc++.h>
#include "minerals.h"
using namespace std;
const int N = 50000 + 5;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, ans[N], ptr, cnt;
set<int> cur;
int query(int x){
if(cur.find(x) != cur.end()) cur.erase(x);
else cur.insert(x);
return cnt = Query(x);
}
vector<int> treevec;
struct SegTree{
map<int, vector<int>> tree;
void clear(){
tree.clear();
}
void build(int v = 1, int l = 1, int r = n, bool flag = false){
if(l > r) return;
if(l == r) ans[l] = tree[v].front();
else{
int m = l + (r - l) * 0.373;
for(int i = l; i <= m; i++) query(treevec[i - 1]);
for(auto x : tree[v]){
if(tree[v * 2].size() == (m - l + 1)) tree[v * 2 + 1].push_back(x);
else if(tree[v * 2 + 1].size() == (r - (m + 1) + 1)) tree[v * 2].push_back(x);
else{
if(cur.find(x) == cur.end()){
int prevcnt = cnt;
query(x);
if(prevcnt + 1 == cnt) tree[v * 2 + flag].push_back(x);
else tree[v * 2 + !flag].push_back(x);
}
else{
int prevcnt = cnt;
query(x);
if(prevcnt == cnt) tree[v * 2 + !flag].push_back(x);
else tree[v * 2 + flag].push_back(x);
}
}
}
build(v * 2, l, m, !flag);
build(v * 2 + 1, m + 1, r, flag);
}
}
};
SegTree sgt;
void Solve(int N_) {
n = N_;
vector<int> tmp, a, b, c1, c2;
vector<int> v(n * 2);
iota(v.begin(), v.end(), 1);
for(int i = 0; i < n; i++){
int prvcnt = cnt;
query(v[i]);
if(cnt == prvcnt + 1) tmp.push_back(v[i]);
else a.push_back(v[i]);
}
while(!tmp.empty()){
int prvcnt = cnt;
query(tmp.back());
if(cnt == prvcnt - 1) c1.push_back(tmp.back());
else b.push_back(tmp.back());
tmp.pop_back();
}
treevec = b;
for(auto x : a) sgt.tree[1].push_back(x);
sgt.build(1, 1, a.size(), true);
for(int i = 1; i <= a.size(); i++) Answer(treevec[i - 1], ans[i]);
a.clear(), b.clear();
for(int i = n; i < n * 2; i++){
int prvcnt = cnt;
query(v[i]);
if(cnt == prvcnt + 1) tmp.push_back(v[i]);
else a.push_back(v[i]);
}
while(!tmp.empty()){
int prvcnt = cnt;
query(tmp.back());
if(cnt == prvcnt - 1) c2.push_back(tmp.back());
else b.push_back(tmp.back());
tmp.pop_back();
}
treevec = b;
sgt.clear();
for(auto x : a) sgt.tree[1].push_back(x);
sgt.build(1, 1, a.size(), true);
for(int i = 1; i <= a.size(); i++) Answer(treevec[i - 1], ans[i]);
treevec = c1;
sgt.clear();
for(auto x : c2) sgt.tree[1].push_back(x);
sgt.build(1, 1, c1.size(), true);
for(int i = 1; i <= c1.size(); i++) Answer(treevec[i - 1], ans[i]);
}
Compilation message
minerals.cpp: In member function 'void SegTree::build(int, int, int, bool)':
minerals.cpp:40:39: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
40 | if(tree[v * 2].size() == (m - l + 1)) tree[v * 2 + 1].push_back(x);
| ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
minerals.cpp:41:48: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
41 | else if(tree[v * 2 + 1].size() == (r - (m + 1) + 1)) tree[v * 2].push_back(x);
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
minerals.cpp: In function 'void Solve(int)':
minerals.cpp:105:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for(int i = 1; i <= a.size(); i++) Answer(treevec[i - 1], ans[i]);
| ~~^~~~~~~~~~~
minerals.cpp:137:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
137 | for(int i = 1; i <= a.size(); i++) Answer(treevec[i - 1], ans[i]);
| ~~^~~~~~~~~~~
minerals.cpp:147:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
147 | for(int i = 1; i <= c1.size(); i++) Answer(treevec[i - 1], ans[i]);
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
692 KB |
Output is correct |
2 |
Correct |
10 ms |
976 KB |
Output is correct |
3 |
Correct |
22 ms |
1760 KB |
Output is correct |
4 |
Correct |
52 ms |
3368 KB |
Output is correct |
5 |
Correct |
108 ms |
6164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
5 ms |
692 KB |
Output is correct |
6 |
Correct |
10 ms |
976 KB |
Output is correct |
7 |
Correct |
22 ms |
1760 KB |
Output is correct |
8 |
Correct |
52 ms |
3368 KB |
Output is correct |
9 |
Correct |
108 ms |
6164 KB |
Output is correct |
10 |
Correct |
4 ms |
464 KB |
Output is correct |
11 |
Correct |
59 ms |
2672 KB |
Output is correct |
12 |
Correct |
91 ms |
3836 KB |
Output is correct |
13 |
Correct |
77 ms |
4164 KB |
Output is correct |
14 |
Correct |
79 ms |
6016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
5 ms |
692 KB |
Output is correct |
6 |
Correct |
10 ms |
976 KB |
Output is correct |
7 |
Correct |
22 ms |
1760 KB |
Output is correct |
8 |
Correct |
52 ms |
3368 KB |
Output is correct |
9 |
Correct |
108 ms |
6164 KB |
Output is correct |
10 |
Correct |
4 ms |
464 KB |
Output is correct |
11 |
Correct |
59 ms |
2672 KB |
Output is correct |
12 |
Correct |
91 ms |
3836 KB |
Output is correct |
13 |
Correct |
77 ms |
4164 KB |
Output is correct |
14 |
Correct |
79 ms |
6016 KB |
Output is correct |
15 |
Correct |
283 ms |
9676 KB |
Output is correct |
16 |
Correct |
278 ms |
9800 KB |
Output is correct |
17 |
Correct |
210 ms |
10288 KB |
Output is correct |
18 |
Correct |
225 ms |
15392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
5 ms |
692 KB |
Output is correct |
6 |
Correct |
10 ms |
976 KB |
Output is correct |
7 |
Correct |
22 ms |
1760 KB |
Output is correct |
8 |
Correct |
52 ms |
3368 KB |
Output is correct |
9 |
Correct |
108 ms |
6164 KB |
Output is correct |
10 |
Correct |
4 ms |
464 KB |
Output is correct |
11 |
Correct |
59 ms |
2672 KB |
Output is correct |
12 |
Correct |
91 ms |
3836 KB |
Output is correct |
13 |
Correct |
77 ms |
4164 KB |
Output is correct |
14 |
Correct |
79 ms |
6016 KB |
Output is correct |
15 |
Correct |
283 ms |
9676 KB |
Output is correct |
16 |
Correct |
278 ms |
9800 KB |
Output is correct |
17 |
Correct |
210 ms |
10288 KB |
Output is correct |
18 |
Correct |
225 ms |
15392 KB |
Output is correct |
19 |
Correct |
291 ms |
9920 KB |
Output is correct |
20 |
Correct |
286 ms |
9920 KB |
Output is correct |
21 |
Correct |
217 ms |
10492 KB |
Output is correct |
22 |
Correct |
228 ms |
15728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
5 ms |
692 KB |
Output is correct |
6 |
Correct |
10 ms |
976 KB |
Output is correct |
7 |
Correct |
22 ms |
1760 KB |
Output is correct |
8 |
Correct |
52 ms |
3368 KB |
Output is correct |
9 |
Correct |
108 ms |
6164 KB |
Output is correct |
10 |
Correct |
4 ms |
464 KB |
Output is correct |
11 |
Correct |
59 ms |
2672 KB |
Output is correct |
12 |
Correct |
91 ms |
3836 KB |
Output is correct |
13 |
Correct |
77 ms |
4164 KB |
Output is correct |
14 |
Correct |
79 ms |
6016 KB |
Output is correct |
15 |
Correct |
283 ms |
9676 KB |
Output is correct |
16 |
Correct |
278 ms |
9800 KB |
Output is correct |
17 |
Correct |
210 ms |
10288 KB |
Output is correct |
18 |
Correct |
225 ms |
15392 KB |
Output is correct |
19 |
Correct |
291 ms |
9920 KB |
Output is correct |
20 |
Correct |
286 ms |
9920 KB |
Output is correct |
21 |
Correct |
217 ms |
10492 KB |
Output is correct |
22 |
Correct |
228 ms |
15728 KB |
Output is correct |
23 |
Correct |
299 ms |
10108 KB |
Output is correct |
24 |
Correct |
302 ms |
10240 KB |
Output is correct |
25 |
Correct |
209 ms |
10840 KB |
Output is correct |
26 |
Correct |
230 ms |
16028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
5 ms |
692 KB |
Output is correct |
6 |
Correct |
10 ms |
976 KB |
Output is correct |
7 |
Correct |
22 ms |
1760 KB |
Output is correct |
8 |
Correct |
52 ms |
3368 KB |
Output is correct |
9 |
Correct |
108 ms |
6164 KB |
Output is correct |
10 |
Correct |
4 ms |
464 KB |
Output is correct |
11 |
Correct |
59 ms |
2672 KB |
Output is correct |
12 |
Correct |
91 ms |
3836 KB |
Output is correct |
13 |
Correct |
77 ms |
4164 KB |
Output is correct |
14 |
Correct |
79 ms |
6016 KB |
Output is correct |
15 |
Correct |
283 ms |
9676 KB |
Output is correct |
16 |
Correct |
278 ms |
9800 KB |
Output is correct |
17 |
Correct |
210 ms |
10288 KB |
Output is correct |
18 |
Correct |
225 ms |
15392 KB |
Output is correct |
19 |
Correct |
291 ms |
9920 KB |
Output is correct |
20 |
Correct |
286 ms |
9920 KB |
Output is correct |
21 |
Correct |
217 ms |
10492 KB |
Output is correct |
22 |
Correct |
228 ms |
15728 KB |
Output is correct |
23 |
Correct |
299 ms |
10108 KB |
Output is correct |
24 |
Correct |
302 ms |
10240 KB |
Output is correct |
25 |
Correct |
209 ms |
10840 KB |
Output is correct |
26 |
Correct |
230 ms |
16028 KB |
Output is correct |
27 |
Correct |
310 ms |
10372 KB |
Output is correct |
28 |
Correct |
308 ms |
10360 KB |
Output is correct |
29 |
Correct |
220 ms |
11064 KB |
Output is correct |
30 |
Correct |
237 ms |
16472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
5 ms |
692 KB |
Output is correct |
6 |
Correct |
10 ms |
976 KB |
Output is correct |
7 |
Correct |
22 ms |
1760 KB |
Output is correct |
8 |
Correct |
52 ms |
3368 KB |
Output is correct |
9 |
Correct |
108 ms |
6164 KB |
Output is correct |
10 |
Correct |
4 ms |
464 KB |
Output is correct |
11 |
Correct |
59 ms |
2672 KB |
Output is correct |
12 |
Correct |
91 ms |
3836 KB |
Output is correct |
13 |
Correct |
77 ms |
4164 KB |
Output is correct |
14 |
Correct |
79 ms |
6016 KB |
Output is correct |
15 |
Correct |
283 ms |
9676 KB |
Output is correct |
16 |
Correct |
278 ms |
9800 KB |
Output is correct |
17 |
Correct |
210 ms |
10288 KB |
Output is correct |
18 |
Correct |
225 ms |
15392 KB |
Output is correct |
19 |
Correct |
291 ms |
9920 KB |
Output is correct |
20 |
Correct |
286 ms |
9920 KB |
Output is correct |
21 |
Correct |
217 ms |
10492 KB |
Output is correct |
22 |
Correct |
228 ms |
15728 KB |
Output is correct |
23 |
Correct |
299 ms |
10108 KB |
Output is correct |
24 |
Correct |
302 ms |
10240 KB |
Output is correct |
25 |
Correct |
209 ms |
10840 KB |
Output is correct |
26 |
Correct |
230 ms |
16028 KB |
Output is correct |
27 |
Correct |
310 ms |
10372 KB |
Output is correct |
28 |
Correct |
308 ms |
10360 KB |
Output is correct |
29 |
Correct |
220 ms |
11064 KB |
Output is correct |
30 |
Correct |
237 ms |
16472 KB |
Output is correct |
31 |
Correct |
323 ms |
10716 KB |
Output is correct |
32 |
Correct |
319 ms |
10564 KB |
Output is correct |
33 |
Correct |
227 ms |
11344 KB |
Output is correct |
34 |
Correct |
247 ms |
16828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
5 ms |
692 KB |
Output is correct |
6 |
Correct |
10 ms |
976 KB |
Output is correct |
7 |
Correct |
22 ms |
1760 KB |
Output is correct |
8 |
Correct |
52 ms |
3368 KB |
Output is correct |
9 |
Correct |
108 ms |
6164 KB |
Output is correct |
10 |
Correct |
4 ms |
464 KB |
Output is correct |
11 |
Correct |
59 ms |
2672 KB |
Output is correct |
12 |
Correct |
91 ms |
3836 KB |
Output is correct |
13 |
Correct |
77 ms |
4164 KB |
Output is correct |
14 |
Correct |
79 ms |
6016 KB |
Output is correct |
15 |
Correct |
283 ms |
9676 KB |
Output is correct |
16 |
Correct |
278 ms |
9800 KB |
Output is correct |
17 |
Correct |
210 ms |
10288 KB |
Output is correct |
18 |
Correct |
225 ms |
15392 KB |
Output is correct |
19 |
Correct |
291 ms |
9920 KB |
Output is correct |
20 |
Correct |
286 ms |
9920 KB |
Output is correct |
21 |
Correct |
217 ms |
10492 KB |
Output is correct |
22 |
Correct |
228 ms |
15728 KB |
Output is correct |
23 |
Correct |
299 ms |
10108 KB |
Output is correct |
24 |
Correct |
302 ms |
10240 KB |
Output is correct |
25 |
Correct |
209 ms |
10840 KB |
Output is correct |
26 |
Correct |
230 ms |
16028 KB |
Output is correct |
27 |
Correct |
310 ms |
10372 KB |
Output is correct |
28 |
Correct |
308 ms |
10360 KB |
Output is correct |
29 |
Correct |
220 ms |
11064 KB |
Output is correct |
30 |
Correct |
237 ms |
16472 KB |
Output is correct |
31 |
Correct |
323 ms |
10716 KB |
Output is correct |
32 |
Correct |
319 ms |
10564 KB |
Output is correct |
33 |
Correct |
227 ms |
11344 KB |
Output is correct |
34 |
Correct |
247 ms |
16828 KB |
Output is correct |
35 |
Correct |
332 ms |
10876 KB |
Output is correct |
36 |
Correct |
324 ms |
10884 KB |
Output is correct |
37 |
Correct |
228 ms |
11532 KB |
Output is correct |
38 |
Correct |
259 ms |
17136 KB |
Output is correct |