#include #include #include #define N 100005 #define M 1000005 using namespace std; int n,m,cnt,ans; int c[N],nxt[N],ft[M],head[M],s[M],st[M],w[N],h[N]; inline int lb(int i) {return i&(-i);} inline int qry(int *arr,int pos) { int ans=0; for (;pos;pos-=lb(pos)) ans+=arr[pos]; return ans; } inline int mdf(int *arr,int pos,int key) { for (;pos<=n;pos+=lb(pos)) arr[pos]+=key; } void solve(int a,int b) { for(int i=head[a];i;i=nxt[i]) { if(c[i+1]==b) mdf(w,i+1,-1),h[i+1]=0; if(c[i-1]==b) mdf(w,i,-1),h[i]=0; } for(int i=head[a];i;i=nxt[i])c[i]=b; nxt[st[a]]=head[b];head[b]=head[a];s[b]+=s[a]; head[a]=st[a]=s[a]=0; } int main() { int T,x,a,b; scanf("%d",&T); while (T--) { for (int i=0;is[ft[b]]) swap(ft[a],ft[b]); a=ft[a];b=ft[b]; if(s[a]==0)continue; s[b]+=s[a];s[a]=0; solve(a,b); } } } //printf("%d",qry(w,5)); }