# ccz181078

:: :: :: :: :: :: :: ::
 427 随笔 :: 0 文章 :: 15 评论 :: 0 引用

## Output

#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
int x=0,c=getchar();
while(c>'9'||c<'0')c=getchar();
while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
return x;
}
inline void exch(int&a,int&b){int c=a;a=b;b=c;}
struct xy{int x,y;};
struct oper{int x,y,t;};
bool operator<(const oper&a,const oper&b){
if(a.x!=b.x)return a.x<b.x;
return a.y<b.y;
}
std::set<oper>es;
int oc=0;
int n,m;
std::vector<xy>ops[524288];
int qx[600005],qy[600005],f[100005];
char op[16];
inline void _ins(int w,int x,int y){
ops[w].push_back((xy){x,y});
}
inline void ins(int l,int r,int x,int y){
for(l+=262142,r+=262144;l^r^1;l>>=1,r>>=1){
if(~l&1)_ins(l^1,x,y);
if(r&1)_ins(r^1,x,y);
}
}
inline void get(int&w,int&h){
h=0;
while(w!=f[w])w=f[w],++h;
}
inline int get(int w){
while(w!=f[w])w=f[w];
return w;
}
int cs[600005],cp=0;
void dfs(int w){
int cpx=cp;
for(int i=0;i<ops[w].size();i++){
xy a=ops[w][i];
int h1,h2;
get(a.x,h1);get(a.y,h2);
if(a.x!=a.y){
if(h1<h2)f[a.x]=a.y,cs[cp++]=a.x;
else f[a.y]=a.x,cs[cp++]=a.y;
}
}
if(w<262144){
int lc=w<<1,rc=lc^1;
dfs(lc);
dfs(rc);
}else{
w-=262143;
if(qx[w])puts(get(qx[w])!=get(qy[w])?"N":"Y");
}
while(cp>cpx)--cp,f[cs[cp]]=cs[cp];
}
int main(){
for(int i=1;i<=n+n+2;i++)f[i]=i;
for(int i=2,x,y,r1,c1,r2,c2;;i++){
scanf("%s",op);
if(op[0]=='E'){
m=i+3;
break;
}
x=r1*n+c1;y=r2*n+c2;
if(x>y)exch(x,y);
if(op[0]=='A')qx[i]=x,qy[i]=y;
if(op[0]=='O')es.insert((oper){x,y,i});
if(op[0]=='C'){
std::set<oper>::iterator w=es.find((oper){x,y,0});
ins((*w).t,i,(*w).x,(*w).y);
es.erase(w);
}
}
for(std::set<oper>::iterator w=es.begin();w!=es.end();++w)ins((*w).t,m+1,(*w).x,(*w).y);
dfs(1);
return 0;
}

posted on 2016-04-01 19:33  nul  阅读(317)  评论(0编辑  收藏