#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=1e5+7;
set<pair<ll,ll>>A[LIM];
set<ll>B[LIM];
ll F[LIM], ile[LIM], ans;
void kraw(ll a, ll b);
ll fnd(ll x) {
if(F[x]==x) return x;
return F[x]=fnd(F[x]);
}
void uni(ll a, ll b) {
a=fnd(a); b=fnd(b);
if(ile[a]>ile[b]) swap(a, b);
rep(xd, 2) {
while(true) {
auto it=A[a].lower_bound({b, -1});
if(it==A[a].end()) break;
pair<ll,ll>p=*it;
if(p.st>b) break;
A[a].erase(p);
B[p.st].erase(p.nd);
ans-=ile[b];
}
swap(a, b);
}
ans+=ile[a]*ile[b]*2;
vector<pair<int,int>>P;
for(auto i : A[a]) P.pb({i.nd, i.st});
for(auto i : A[a]) {
B[i.st].erase(i.nd);
ans-=ile[i.st];
}
ans-=ile[a]*(ll)B[a].size();
ans+=ile[a]*(ll)B[b].size();
for(auto i : B[a]) A[fnd(i)].erase({a, i});
for(auto i : B[a]) P.pb({i, a});
ile[b]+=ile[a];
F[a]=b;
A[a].clear();
B[a].clear();
for(auto i : P) kraw(i.st, i.nd);
}
void kraw(ll a, ll b) {
if(fnd(a)==fnd(b)) return;
b=fnd(b);
auto it=A[b].lower_bound({fnd(a), -1});
if(it!=A[b].end()) {
pair<ll,ll>p=*it;
if(p.st==fnd(a)) {
uni(a, b);
return;
}
}
if(B[b].find(a)!=B[b].end()) return;
B[b].insert(a);
A[fnd(a)].insert({b, a});
ans+=ile[b];
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n, m;
cin >> n >> m;
rep(i, n) {
F[i]=i;
ile[i]=1;
}
while(m--) {
ll a, b;
cin >> a >> b; --a; --b;
kraw(a, b);
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11100 KB |
Output is correct |
2 |
Correct |
2 ms |
11100 KB |
Output is correct |
3 |
Correct |
3 ms |
11100 KB |
Output is correct |
4 |
Correct |
2 ms |
11096 KB |
Output is correct |
5 |
Correct |
3 ms |
11100 KB |
Output is correct |
6 |
Correct |
2 ms |
11212 KB |
Output is correct |
7 |
Correct |
3 ms |
11100 KB |
Output is correct |
8 |
Correct |
3 ms |
11352 KB |
Output is correct |
9 |
Correct |
4 ms |
11292 KB |
Output is correct |
10 |
Correct |
2 ms |
11100 KB |
Output is correct |
11 |
Correct |
4 ms |
11224 KB |
Output is correct |
12 |
Correct |
3 ms |
11100 KB |
Output is correct |
13 |
Correct |
3 ms |
11100 KB |
Output is correct |
14 |
Correct |
2 ms |
11256 KB |
Output is correct |
15 |
Correct |
2 ms |
11096 KB |
Output is correct |
16 |
Correct |
3 ms |
11100 KB |
Output is correct |
17 |
Correct |
3 ms |
11100 KB |
Output is correct |
18 |
Correct |
3 ms |
11216 KB |
Output is correct |
19 |
Correct |
3 ms |
11352 KB |
Output is correct |
20 |
Correct |
3 ms |
11096 KB |
Output is correct |
21 |
Correct |
3 ms |
11252 KB |
Output is correct |
22 |
Correct |
3 ms |
11100 KB |
Output is correct |
23 |
Correct |
3 ms |
11188 KB |
Output is correct |
24 |
Correct |
3 ms |
11100 KB |
Output is correct |
25 |
Correct |
3 ms |
11100 KB |
Output is correct |
26 |
Correct |
3 ms |
11100 KB |
Output is correct |
27 |
Correct |
3 ms |
11096 KB |
Output is correct |
28 |
Correct |
2 ms |
11352 KB |
Output is correct |
29 |
Correct |
2 ms |
11100 KB |
Output is correct |
30 |
Correct |
2 ms |
11100 KB |
Output is correct |
31 |
Correct |
3 ms |
11100 KB |
Output is correct |
32 |
Correct |
4 ms |
11100 KB |
Output is correct |
33 |
Correct |
3 ms |
11100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11100 KB |
Output is correct |
2 |
Correct |
2 ms |
11100 KB |
Output is correct |
3 |
Correct |
3 ms |
11100 KB |
Output is correct |
4 |
Correct |
2 ms |
11096 KB |
Output is correct |
5 |
Correct |
3 ms |
11100 KB |
Output is correct |
6 |
Correct |
2 ms |
11212 KB |
Output is correct |
7 |
Correct |
3 ms |
11100 KB |
Output is correct |
8 |
Correct |
3 ms |
11352 KB |
Output is correct |
9 |
Correct |
4 ms |
11292 KB |
Output is correct |
10 |
Correct |
2 ms |
11100 KB |
Output is correct |
11 |
Correct |
4 ms |
11224 KB |
Output is correct |
12 |
Correct |
3 ms |
11100 KB |
Output is correct |
13 |
Correct |
3 ms |
11100 KB |
Output is correct |
14 |
Correct |
2 ms |
11256 KB |
Output is correct |
15 |
Correct |
2 ms |
11096 KB |
Output is correct |
16 |
Correct |
3 ms |
11100 KB |
Output is correct |
17 |
Correct |
3 ms |
11100 KB |
Output is correct |
18 |
Correct |
3 ms |
11216 KB |
Output is correct |
19 |
Correct |
3 ms |
11352 KB |
Output is correct |
20 |
Correct |
3 ms |
11096 KB |
Output is correct |
21 |
Correct |
3 ms |
11252 KB |
Output is correct |
22 |
Correct |
3 ms |
11100 KB |
Output is correct |
23 |
Correct |
3 ms |
11188 KB |
Output is correct |
24 |
Correct |
3 ms |
11100 KB |
Output is correct |
25 |
Correct |
3 ms |
11100 KB |
Output is correct |
26 |
Correct |
3 ms |
11100 KB |
Output is correct |
27 |
Correct |
3 ms |
11096 KB |
Output is correct |
28 |
Correct |
2 ms |
11352 KB |
Output is correct |
29 |
Correct |
2 ms |
11100 KB |
Output is correct |
30 |
Correct |
2 ms |
11100 KB |
Output is correct |
31 |
Correct |
3 ms |
11100 KB |
Output is correct |
32 |
Correct |
4 ms |
11100 KB |
Output is correct |
33 |
Correct |
3 ms |
11100 KB |
Output is correct |
34 |
Correct |
4 ms |
11352 KB |
Output is correct |
35 |
Correct |
61 ms |
16876 KB |
Output is correct |
36 |
Correct |
78 ms |
19024 KB |
Output is correct |
37 |
Correct |
97 ms |
19120 KB |
Output is correct |
38 |
Correct |
74 ms |
18960 KB |
Output is correct |
39 |
Correct |
3 ms |
11096 KB |
Output is correct |
40 |
Correct |
4 ms |
11356 KB |
Output is correct |
41 |
Correct |
4 ms |
11352 KB |
Output is correct |
42 |
Correct |
4 ms |
11100 KB |
Output is correct |
43 |
Correct |
4 ms |
11356 KB |
Output is correct |
44 |
Correct |
4 ms |
11264 KB |
Output is correct |
45 |
Correct |
4 ms |
11352 KB |
Output is correct |
46 |
Correct |
3 ms |
11100 KB |
Output is correct |
47 |
Correct |
5 ms |
11356 KB |
Output is correct |
48 |
Correct |
4 ms |
11356 KB |
Output is correct |
49 |
Correct |
9 ms |
11984 KB |
Output is correct |
50 |
Correct |
78 ms |
19284 KB |
Output is correct |
51 |
Correct |
7 ms |
11624 KB |
Output is correct |
52 |
Correct |
85 ms |
18068 KB |
Output is correct |
53 |
Correct |
9 ms |
11868 KB |
Output is correct |
54 |
Correct |
71 ms |
18612 KB |
Output is correct |
55 |
Correct |
5 ms |
11612 KB |
Output is correct |
56 |
Correct |
5 ms |
11612 KB |
Output is correct |
57 |
Correct |
6 ms |
11612 KB |
Output is correct |
58 |
Correct |
5 ms |
11612 KB |
Output is correct |
59 |
Correct |
4 ms |
11100 KB |
Output is correct |
60 |
Correct |
61 ms |
16228 KB |
Output is correct |
61 |
Correct |
5 ms |
11356 KB |
Output is correct |
62 |
Correct |
74 ms |
18812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11100 KB |
Output is correct |
2 |
Correct |
2 ms |
11100 KB |
Output is correct |
3 |
Correct |
3 ms |
11100 KB |
Output is correct |
4 |
Correct |
2 ms |
11096 KB |
Output is correct |
5 |
Correct |
3 ms |
11100 KB |
Output is correct |
6 |
Correct |
2 ms |
11212 KB |
Output is correct |
7 |
Correct |
3 ms |
11100 KB |
Output is correct |
8 |
Correct |
3 ms |
11352 KB |
Output is correct |
9 |
Correct |
4 ms |
11292 KB |
Output is correct |
10 |
Correct |
2 ms |
11100 KB |
Output is correct |
11 |
Correct |
4 ms |
11224 KB |
Output is correct |
12 |
Correct |
3 ms |
11100 KB |
Output is correct |
13 |
Correct |
3 ms |
11100 KB |
Output is correct |
14 |
Correct |
2 ms |
11256 KB |
Output is correct |
15 |
Correct |
2 ms |
11096 KB |
Output is correct |
16 |
Correct |
3 ms |
11100 KB |
Output is correct |
17 |
Correct |
3 ms |
11100 KB |
Output is correct |
18 |
Correct |
3 ms |
11216 KB |
Output is correct |
19 |
Correct |
3 ms |
11352 KB |
Output is correct |
20 |
Correct |
3 ms |
11096 KB |
Output is correct |
21 |
Correct |
3 ms |
11252 KB |
Output is correct |
22 |
Correct |
3 ms |
11100 KB |
Output is correct |
23 |
Correct |
3 ms |
11188 KB |
Output is correct |
24 |
Correct |
3 ms |
11100 KB |
Output is correct |
25 |
Correct |
3 ms |
11100 KB |
Output is correct |
26 |
Correct |
3 ms |
11100 KB |
Output is correct |
27 |
Correct |
3 ms |
11096 KB |
Output is correct |
28 |
Correct |
2 ms |
11352 KB |
Output is correct |
29 |
Correct |
2 ms |
11100 KB |
Output is correct |
30 |
Correct |
2 ms |
11100 KB |
Output is correct |
31 |
Correct |
3 ms |
11100 KB |
Output is correct |
32 |
Correct |
4 ms |
11100 KB |
Output is correct |
33 |
Correct |
3 ms |
11100 KB |
Output is correct |
34 |
Correct |
4 ms |
11352 KB |
Output is correct |
35 |
Correct |
61 ms |
16876 KB |
Output is correct |
36 |
Correct |
78 ms |
19024 KB |
Output is correct |
37 |
Correct |
97 ms |
19120 KB |
Output is correct |
38 |
Correct |
74 ms |
18960 KB |
Output is correct |
39 |
Correct |
3 ms |
11096 KB |
Output is correct |
40 |
Correct |
4 ms |
11356 KB |
Output is correct |
41 |
Correct |
4 ms |
11352 KB |
Output is correct |
42 |
Correct |
4 ms |
11100 KB |
Output is correct |
43 |
Correct |
4 ms |
11356 KB |
Output is correct |
44 |
Correct |
4 ms |
11264 KB |
Output is correct |
45 |
Correct |
4 ms |
11352 KB |
Output is correct |
46 |
Correct |
3 ms |
11100 KB |
Output is correct |
47 |
Correct |
5 ms |
11356 KB |
Output is correct |
48 |
Correct |
4 ms |
11356 KB |
Output is correct |
49 |
Correct |
9 ms |
11984 KB |
Output is correct |
50 |
Correct |
78 ms |
19284 KB |
Output is correct |
51 |
Correct |
7 ms |
11624 KB |
Output is correct |
52 |
Correct |
85 ms |
18068 KB |
Output is correct |
53 |
Correct |
9 ms |
11868 KB |
Output is correct |
54 |
Correct |
71 ms |
18612 KB |
Output is correct |
55 |
Correct |
5 ms |
11612 KB |
Output is correct |
56 |
Correct |
5 ms |
11612 KB |
Output is correct |
57 |
Correct |
6 ms |
11612 KB |
Output is correct |
58 |
Correct |
5 ms |
11612 KB |
Output is correct |
59 |
Correct |
4 ms |
11100 KB |
Output is correct |
60 |
Correct |
61 ms |
16228 KB |
Output is correct |
61 |
Correct |
5 ms |
11356 KB |
Output is correct |
62 |
Correct |
74 ms |
18812 KB |
Output is correct |
63 |
Correct |
362 ms |
49816 KB |
Output is correct |
64 |
Correct |
325 ms |
49708 KB |
Output is correct |
65 |
Correct |
307 ms |
49724 KB |
Output is correct |
66 |
Correct |
59 ms |
15696 KB |
Output is correct |
67 |
Correct |
117 ms |
20428 KB |
Output is correct |
68 |
Correct |
61 ms |
15852 KB |
Output is correct |
69 |
Correct |
191 ms |
21144 KB |
Output is correct |
70 |
Correct |
60 ms |
15664 KB |
Output is correct |
71 |
Correct |
77 ms |
15640 KB |
Output is correct |
72 |
Correct |
171 ms |
20608 KB |
Output is correct |
73 |
Correct |
132 ms |
20420 KB |
Output is correct |
74 |
Correct |
434 ms |
31572 KB |
Output is correct |
75 |
Correct |
238 ms |
26232 KB |
Output is correct |
76 |
Correct |
313 ms |
30032 KB |
Output is correct |
77 |
Correct |
337 ms |
29780 KB |
Output is correct |
78 |
Correct |
129 ms |
20020 KB |
Output is correct |
79 |
Correct |
189 ms |
23816 KB |
Output is correct |
80 |
Correct |
105 ms |
20224 KB |
Output is correct |
81 |
Correct |
183 ms |
23468 KB |
Output is correct |
82 |
Correct |
370 ms |
36916 KB |
Output is correct |
83 |
Correct |
392 ms |
36944 KB |
Output is correct |
84 |
Correct |
326 ms |
37156 KB |
Output is correct |
85 |
Correct |
289 ms |
37144 KB |
Output is correct |
86 |
Correct |
64 ms |
14808 KB |
Output is correct |
87 |
Correct |
82 ms |
17220 KB |
Output is correct |
88 |
Correct |
143 ms |
20524 KB |
Output is correct |
89 |
Correct |
364 ms |
29132 KB |
Output is correct |