General
 
 
# Author Problem Lang Verdict Time Memory Sent Judged  
176480079 Practice:
Yu_Jie
1140F - 11 C++14 (GCC 6-32) Accepted 1060 ms 147760 KB 2022-10-16 14:27:45 2022-10-16 14:27:45
→ Source
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define x first
#define y second
using namespace std;
const int N=600005;
int q,n=3e5,ans[N],fa[N],ls[N],rs[N],top,res;
pii stk[N];
vector<pii> vc[N<<2];
map<pii,int> mp;
int read() {
	int x=0,f=1; char c=getchar();
	for(;c<'0'||c>'9';c=getchar()) if(c=='-') f=-1;
	for(;c>='0'&&c<='9';c=getchar()) x=x*10+c-48;
	return x*f;
}
void modify(int L,int R,pii v,int p=1,int l=1,int r=q) {
	if(L<=l&&r<=R) {vc[p].push_back(v); return ;}
	int mid=l+r>>1;
	if(L<=mid) modify(L,R,v,p<<1,l,mid);
	if(R>mid) modify(L,R,v,p<<1|1,mid+1,r);
}
int get(int x) {return fa[x]==x?x:get(fa[x]);}
void merge(int x,int y) {
	x=get(x); y=get(y);
	if(x==y) return ;
	if(ls[x]+rs[x]>ls[y]+rs[y]) swap(x,y);
	res-=ls[x]*rs[x]+ls[y]*rs[y];
	fa[x]=y; ls[y]+=ls[x]; rs[y]+=rs[x];
	res+=ls[y]*rs[y];
	stk[++top]={x,y};
}
void delet(int x,int y) {
	res-=ls[y]*rs[y];
	fa[x]=x; ls[y]-=ls[x]; rs[y]-=rs[x];
	res+=ls[x]*rs[x]+ls[y]*rs[y];
}
void solve(int p=1,int l=1,int r=q) {
	int pre=top;
	for(pii x:vc[p]) merge(x.x,x.y);
	if(l==r) ans[l]=res;
	else {
		int mid=l+r>>1;
		solve(p<<1,l,mid); solve(p<<1|1,mid+1,r);
	}
	for(;top!=pre;top--) delet(stk[top].x,stk[top].y);
}
signed main() {
	q=read();
	for(int i=1;i<=q;i++) {
		int x=read(),y=read()+n;
		if(mp.find({x,y})==mp.end()) mp[{x,y}]=i;
		else {modify(mp[{x,y}],i-1,{x,y}); mp.erase({x,y});}
	}
	for(auto x:mp) modify(x.y,q,x.x);
	for(int i=1;i<=2*n;i++) {fa[i]=i; (i<=n?ls[i]:rs[i])=1;}
	solve();
	for(int i=1;i<=q;i++) printf("%lld ",ans[i]);
	return 0;
}
?
Time: ? ms, memory: ? KB
Verdict: ?
Input
?
Participant's output
?
Jury's answer
?
Checker comment
?
Diagnostics
?
Click to see test details