General
 
 
# Author Problem Lang Verdict Time Memory Sent Judged  
132006523 Practice:
Starch
1140F - 11 C++14 (GCC 6-32) Accepted 811 ms 97112 KB 2021-10-15 11:12:32 2021-10-15 11:12:32
→ Source
#include<bits/stdc++.h>
using namespace std;
const int N=6e5+5;
int n,x,y,mx=3e5,f[N],sz1[N],sz2[N],dep[N],top;
long long ans;
vector<pair<int,int> >a[N<<2];
pair<pair<int,int>,int>s[N];
map<pair<int,int>,int>v;
int find(int x){return x==f[x]?x:find(f[x]);} 
void merge(int x,int y){
	x=find(x),y=find(y);
	if(x!=y){
		if(dep[x]<dep[y]) swap(x,y);
		ans-=1ll*sz1[x]*sz2[x],ans-=1ll*sz1[y]*sz2[y];
		s[++top]={{x,y},dep[x]==dep[y]},dep[x]+=(dep[x]==dep[y]);
		sz1[x]+=sz1[y],sz2[x]+=sz2[y],f[y]=x,ans+=1ll*sz1[x]*sz2[x];
	}
}
void delet(int x){
	while(x<top){
		int x=s[top].first.first,y=s[top].first.second,z=s[top].second; top--;
		ans-=1ll*sz1[x]*sz2[x],dep[x]-=z;
		sz1[x]-=sz1[y],sz2[x]-=sz2[y],f[y]=y;
		ans+=1ll*sz1[x]*sz2[x],ans+=1ll*sz1[y]*sz2[y];
	}
}
void modify(int p,int l,int r,int lx,int rx,pair<int,int>v){
	if(l>=lx&&r<=rx){a[p].push_back(v);return ;}
	int mid=(l+r)/2;
	if(lx<=mid) modify(p<<1,l,mid,lx,rx,v);
	if(rx>mid) modify(p<<1|1,mid+1,r,lx,rx,v); 
}
void dfs(int p,int l,int r){
	int x=top;
	for(auto i:a[p]) merge(i.first,i.second+mx);
	int mid=(l+r)/2;
	if(l==r) printf("%lld ",ans);
	else dfs(p<<1,l,mid),dfs(p<<1|1,mid+1,r);
	delet(x);
}
signed main(){
	scanf("%d",&n);
	for(int i=1;i<=mx;i++) f[i]=i,f[i+mx]=i+mx,sz1[i]=sz2[i+mx]=1,dep[i]=dep[i+mx]=1;
	for(int i=1;i<=n;i++){
		scanf("%d%d",&x,&y);
		if(!v[{x,y}]) v[{x,y}]=i;
		else modify(1,1,n,v[{x,y}],i-1,{x,y}),v[{x,y}]=0;
	}
	for(auto p:v)
		if(v[p.first]) modify(1,1,n,p.second,n,p.first);
	dfs(1,1,n);
	return 0;
}
?
Time: ? ms, memory: ? KB
Verdict: ?
Input
?
Participant's output
?
Jury's answer
?
Checker comment
?
Diagnostics
?
Click to see test details