#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int B = 350;
#define all(x) x.begin(),x.end()
#define sz(x) x.size()
#define X first
#define Y second
typedef pair<int,int> ii;
typedef vector<int> vi;
struct Edge {
int x, y, i;
bool operator < (const Edge &a) const {
return x < a.x;
}
};
struct Ques {
int day, typ;
int idx, ver;
bool operator < (const Ques &a) const {
return day == a.day ? typ : day < a.day;
}
};
bool On[N], uV[N], uE[N];
namespace DSU {
int p[N];
int lead(int x) {
return p[x] == x ? x : p[x] = lead(p[x]);
}
int join(int x,int y) {
x = lead(x);
y = lead(y);
if (x == y) return 0;
if (uV[y]) swap(x,y);
p[y] = x;
return 1;
}
};
vector<Edge> E, se1, se2;
vector<int> ans;
int n, m;
void calc(vector<Ques> qr) {
fill(uV,uV + N,0);
fill(uE,uE + N,0);
map<int,vector<ii>> mp;
vector<Ques> Q;
vector<Edge> ed;
vector<int> vx;
for(Ques q : qr) {
if (q.typ) uE[q.idx] = uV[E[q.idx].x] = uV[E[q.idx].y] = 1;
else Q.push_back(q);
}
for(int i = 0 ; i < n ; ++i) if (uV[i]) vx.push_back(i);
for(int i = 0 ; i < m ; ++i) if (uE[i]) ed.push_back(E[i]);
sort(all(Q),[](Ques &a,Ques &b) {
return a.ver < b.ver;
});
for(int t = 0 ; t < 2 ; ++t) {
iota(DSU::p,DSU::p + n,0);
for(int i = 0, j = 0, r = 0 ; i < sz(Q) ; ++i) {
while (j < se1.size()) {
if (t == 0 && se1[j].x > Q[i].ver) break;
if (t == 1 && se1[j].x <= Q[i].ver) break;
if (!uE[se1[j].i] && On[se1[j].i])
r += DSU::join(se1[j].x,se1[j].y);
j++;
}
ans[Q[i].idx] -= r;
for(int x : vx) {
if (t == 0 && x > Q[i].ver) continue;
if (t == 1 && x <= Q[i].ver) continue;
mp[Q[i].idx].push_back({x,DSU::lead(x)});
}
}
reverse(Q.begin(),Q.end());
swap(se1,se2);
}
for(Ques q : qr) {
if (q.typ) On[q.idx] ^= 1;
else {
for(ii t : mp[q.idx]) DSU::p[t.X] = t.Y;
for(Edge e : ed)
if (On[e.i] && (e.x <= q.ver || e.y > q.ver))
ans[q.idx] -= DSU::join(e.x,e.y);
}
}
}
vi simulateCollapse(int _,vi T,vi X,vi Y,vi W,vi P) {
n = _;
m = T.size();
vector<Ques> v;
map<ii,int> mp;
for(int i = 0 ; i < m ; i++) {
if (X[i] < Y[i]) swap(X[i], Y[i]);
int num;
if(!mp.count({X[i],Y[i]})) {
mp[{X[i],Y[i]}] = num = sz(E);
se1.push_back({X[i],Y[i],num});
se2.push_back({Y[i],X[i],num});
E.push_back({X[i],Y[i],num});
}
else num = mp[{X[i],Y[i]}];
v.push_back({i,1,num,0});
}
sort(all(se1));
sort(all(se2)); reverse(all(se2));
ans.assign(W.size(),n);
for(int i = 0 ; i < W.size() ; ++i)
v.push_back({W[i],0,i,P[i]});
sort(all(v));
for(int i = 0 ; i < v.size() ; i += B)
calc(vector<Ques>(v.begin() + i,v.begin() + min((int)v.size(),i + B)));
return ans;
}
Compilation message
collapse.cpp: In function 'void calc(std::vector<Ques>)':
collapse.cpp:75:41: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0, j = 0, r = 0 ; i < sz(Q) ; ++i) {
^
collapse.cpp:76:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (j < se1.size()) {
~~^~~~~~~~~~~~
collapse.cpp: In function 'vi simulateCollapse(int, vi, vi, vi, vi, vi)':
collapse.cpp:129:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0 ; i < W.size() ; ++i)
~~^~~~~~~~~~
collapse.cpp:129:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
for(int i = 0 ; i < W.size() ; ++i)
^~~
collapse.cpp:132:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
sort(all(v));
^~~~
collapse.cpp:134:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0 ; i < v.size() ; i += B)
~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1016 KB |
Output is correct |
2 |
Correct |
7 ms |
892 KB |
Output is correct |
3 |
Correct |
10 ms |
888 KB |
Output is correct |
4 |
Correct |
10 ms |
892 KB |
Output is correct |
5 |
Correct |
17 ms |
1036 KB |
Output is correct |
6 |
Correct |
42 ms |
1776 KB |
Output is correct |
7 |
Correct |
7 ms |
888 KB |
Output is correct |
8 |
Correct |
8 ms |
888 KB |
Output is correct |
9 |
Correct |
51 ms |
1756 KB |
Output is correct |
10 |
Correct |
59 ms |
1860 KB |
Output is correct |
11 |
Correct |
88 ms |
2436 KB |
Output is correct |
12 |
Correct |
66 ms |
2048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
4972 KB |
Output is correct |
2 |
Correct |
87 ms |
4892 KB |
Output is correct |
3 |
Correct |
338 ms |
10088 KB |
Output is correct |
4 |
Correct |
91 ms |
4972 KB |
Output is correct |
5 |
Correct |
722 ms |
10968 KB |
Output is correct |
6 |
Correct |
294 ms |
6200 KB |
Output is correct |
7 |
Correct |
1623 ms |
20812 KB |
Output is correct |
8 |
Correct |
1271 ms |
16280 KB |
Output is correct |
9 |
Correct |
189 ms |
5736 KB |
Output is correct |
10 |
Correct |
196 ms |
5692 KB |
Output is correct |
11 |
Correct |
304 ms |
6080 KB |
Output is correct |
12 |
Correct |
1494 ms |
16816 KB |
Output is correct |
13 |
Correct |
1579 ms |
18480 KB |
Output is correct |
14 |
Correct |
2075 ms |
21680 KB |
Output is correct |
15 |
Correct |
1698 ms |
21892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
4920 KB |
Output is correct |
2 |
Correct |
87 ms |
4848 KB |
Output is correct |
3 |
Correct |
93 ms |
5096 KB |
Output is correct |
4 |
Correct |
95 ms |
4972 KB |
Output is correct |
5 |
Correct |
147 ms |
5228 KB |
Output is correct |
6 |
Correct |
328 ms |
6164 KB |
Output is correct |
7 |
Correct |
1983 ms |
17240 KB |
Output is correct |
8 |
Correct |
3141 ms |
21328 KB |
Output is correct |
9 |
Correct |
198 ms |
5688 KB |
Output is correct |
10 |
Correct |
296 ms |
6028 KB |
Output is correct |
11 |
Correct |
3395 ms |
21852 KB |
Output is correct |
12 |
Correct |
4601 ms |
21796 KB |
Output is correct |
13 |
Correct |
3613 ms |
21672 KB |
Output is correct |
14 |
Correct |
4586 ms |
22008 KB |
Output is correct |
15 |
Correct |
3368 ms |
21448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1016 KB |
Output is correct |
2 |
Correct |
7 ms |
892 KB |
Output is correct |
3 |
Correct |
10 ms |
888 KB |
Output is correct |
4 |
Correct |
10 ms |
892 KB |
Output is correct |
5 |
Correct |
17 ms |
1036 KB |
Output is correct |
6 |
Correct |
42 ms |
1776 KB |
Output is correct |
7 |
Correct |
7 ms |
888 KB |
Output is correct |
8 |
Correct |
8 ms |
888 KB |
Output is correct |
9 |
Correct |
51 ms |
1756 KB |
Output is correct |
10 |
Correct |
59 ms |
1860 KB |
Output is correct |
11 |
Correct |
88 ms |
2436 KB |
Output is correct |
12 |
Correct |
66 ms |
2048 KB |
Output is correct |
13 |
Correct |
79 ms |
4972 KB |
Output is correct |
14 |
Correct |
87 ms |
4892 KB |
Output is correct |
15 |
Correct |
338 ms |
10088 KB |
Output is correct |
16 |
Correct |
91 ms |
4972 KB |
Output is correct |
17 |
Correct |
722 ms |
10968 KB |
Output is correct |
18 |
Correct |
294 ms |
6200 KB |
Output is correct |
19 |
Correct |
1623 ms |
20812 KB |
Output is correct |
20 |
Correct |
1271 ms |
16280 KB |
Output is correct |
21 |
Correct |
189 ms |
5736 KB |
Output is correct |
22 |
Correct |
196 ms |
5692 KB |
Output is correct |
23 |
Correct |
304 ms |
6080 KB |
Output is correct |
24 |
Correct |
1494 ms |
16816 KB |
Output is correct |
25 |
Correct |
1579 ms |
18480 KB |
Output is correct |
26 |
Correct |
2075 ms |
21680 KB |
Output is correct |
27 |
Correct |
1698 ms |
21892 KB |
Output is correct |
28 |
Correct |
79 ms |
4920 KB |
Output is correct |
29 |
Correct |
87 ms |
4848 KB |
Output is correct |
30 |
Correct |
93 ms |
5096 KB |
Output is correct |
31 |
Correct |
95 ms |
4972 KB |
Output is correct |
32 |
Correct |
147 ms |
5228 KB |
Output is correct |
33 |
Correct |
328 ms |
6164 KB |
Output is correct |
34 |
Correct |
1983 ms |
17240 KB |
Output is correct |
35 |
Correct |
3141 ms |
21328 KB |
Output is correct |
36 |
Correct |
198 ms |
5688 KB |
Output is correct |
37 |
Correct |
296 ms |
6028 KB |
Output is correct |
38 |
Correct |
3395 ms |
21852 KB |
Output is correct |
39 |
Correct |
4601 ms |
21796 KB |
Output is correct |
40 |
Correct |
3613 ms |
21672 KB |
Output is correct |
41 |
Correct |
4586 ms |
22008 KB |
Output is correct |
42 |
Correct |
3368 ms |
21448 KB |
Output is correct |
43 |
Correct |
1239 ms |
15760 KB |
Output is correct |
44 |
Correct |
2601 ms |
20880 KB |
Output is correct |
45 |
Correct |
1457 ms |
16204 KB |
Output is correct |
46 |
Correct |
3132 ms |
21184 KB |
Output is correct |
47 |
Correct |
201 ms |
5608 KB |
Output is correct |
48 |
Correct |
211 ms |
5608 KB |
Output is correct |
49 |
Correct |
314 ms |
6132 KB |
Output is correct |
50 |
Correct |
702 ms |
8012 KB |
Output is correct |
51 |
Correct |
1465 ms |
16408 KB |
Output is correct |
52 |
Correct |
2231 ms |
18004 KB |
Output is correct |
53 |
Correct |
1826 ms |
16716 KB |
Output is correct |
54 |
Correct |
2681 ms |
18988 KB |
Output is correct |
55 |
Correct |
2304 ms |
18260 KB |
Output is correct |
56 |
Correct |
2722 ms |
19408 KB |
Output is correct |
57 |
Correct |
3264 ms |
20424 KB |
Output is correct |
58 |
Correct |
3945 ms |
20912 KB |
Output is correct |
59 |
Correct |
3368 ms |
21384 KB |
Output is correct |
60 |
Correct |
4649 ms |
21972 KB |
Output is correct |