#include<bits/stdc++.h>
#define fi first
#define se second
#define rep(a, b) for(int a = 0; a < (b); a++)
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<ll> vl;
const int N=1e5+10;
vi f[N];
int fau[N];
ll w[N];
set<int> ini[N];
set<int> outi[N];
set<int> ini2[N];
ll res;
vi Union(int a,int b){
res-=(ll)(f[a].size())*(ll)(f[a].size()-1);
res-=(ll)(f[b].size())*(ll)(f[b].size()-1);
a=fau[a],b=fau[b];
vi p;
if(f[a].size()>f[b].size()) swap(a,b);
rep(i,f[a].size()){
fau[f[a][i]]=b,f[b].push_back(f[a][i]);
auto it=ini2[b].lower_bound(f[a][i]);
if(it!=ini2[b].end() and (*it)==f[a][i]) ini2[b].erase(f[a][i]);
}
f[a].clear();
for(auto it=ini2[a].begin();it!=ini2[a].end();it++) if(fau[(*it)]!=b) ini2[b].insert(*it);
ini2[a].clear();
for(auto it=ini[a].begin();it!=ini[a].end();it++){
auto it2=outi[b].lower_bound(*it);
if(it2!=outi[b].end() and *it2==*it) p.push_back(*it2);
}
for(auto it=outi[a].begin();it!=outi[a].end();it++){
auto it2=ini[b].lower_bound(*it);
if(it2!=ini[b].end() and *it==*it2) p.push_back(*it2);
}
for(auto it=ini[a].begin();it!=ini[a].end();it++){
if((*it)!=b){
outi[*it].erase(a);
outi[*it].insert(b);
}
}
for(auto it=outi[a].begin();it!=outi[a].end();it++){
if((*it)!=b){
ini[*it].erase(a);
ini[*it].insert(b);
}
}
for(auto it=ini[a].begin();it!=ini[a].end();it++) if(*it!=b) ini[b].insert(*it);
for(auto it=outi[a].begin();it!=outi[a].end();it++) if(*it!=b) outi[b].insert(*it);
outi[b].erase(a),ini[b].erase(a);
ini[a].clear(),outi[a].clear();
res-=w[a],res-=w[b];
w[b]=(ll)(ini2[b].size())*(ll)(f[b].size());
res+=w[b];
res+=(ll)(f[b].size())*(ll)(f[b].size()-1);
return p;
}
void solve(){
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++) fau[i]=i,f[i].push_back(i);
rep(i,q){
int a,b;
cin>>a>>b;
b=fau[b];
auto it2=ini2[b].lower_bound(a);
if((it2!=ini2[b].end() and (*it2)==a) or fau[a]==b){cout<<res<<"\n";continue;}
ini2[b].insert(a);
a=fau[a];
res-=w[b];
outi[a].insert(b),ini[b].insert(a);
w[b]=(ll)(ini2[b].size())*(ll)(f[b].size());
res+=w[b];
auto it=outi[b].lower_bound(a);
if(it==outi[b].end() or (*it)!=a){cout<<res<<"\n"; continue;}
vi xd=Union(a,b);
int v=fau[a];
while(xd.size()){
vi pom=Union(v,xd.back());
xd.pop_back();
rep(j,pom.size()) xd.push_back(pom[j]);
v=fau[v];
}
cout<<res<<"\n";
}
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
solve();
}
Compilation message
joitter2.cpp: In function 'vi Union(int, int)':
joitter2.cpp:4:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
4 | #define rep(a, b) for(int a = 0; a < (b); a++)
| ^
joitter2.cpp:23:2: note: in expansion of macro 'rep'
23 | rep(i,f[a].size()){
| ^~~
joitter2.cpp: In function 'void solve()':
joitter2.cpp:4:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
4 | #define rep(a, b) for(int a = 0; a < (b); a++)
| ^
joitter2.cpp:84:4: note: in expansion of macro 'rep'
84 | rep(j,pom.size()) xd.push_back(pom[j]);
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
16716 KB |
Output is correct |
2 |
Correct |
8 ms |
16720 KB |
Output is correct |
3 |
Correct |
8 ms |
16760 KB |
Output is correct |
4 |
Correct |
9 ms |
16720 KB |
Output is correct |
5 |
Correct |
8 ms |
16768 KB |
Output is correct |
6 |
Correct |
10 ms |
16900 KB |
Output is correct |
7 |
Runtime error |
1208 ms |
1048580 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
16716 KB |
Output is correct |
2 |
Correct |
8 ms |
16720 KB |
Output is correct |
3 |
Correct |
8 ms |
16760 KB |
Output is correct |
4 |
Correct |
9 ms |
16720 KB |
Output is correct |
5 |
Correct |
8 ms |
16768 KB |
Output is correct |
6 |
Correct |
10 ms |
16900 KB |
Output is correct |
7 |
Runtime error |
1208 ms |
1048580 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
16716 KB |
Output is correct |
2 |
Correct |
8 ms |
16720 KB |
Output is correct |
3 |
Correct |
8 ms |
16760 KB |
Output is correct |
4 |
Correct |
9 ms |
16720 KB |
Output is correct |
5 |
Correct |
8 ms |
16768 KB |
Output is correct |
6 |
Correct |
10 ms |
16900 KB |
Output is correct |
7 |
Runtime error |
1208 ms |
1048580 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |