LG4246 堵塞的交通

#include<bits/stdc++.h>
#define forUp(i,a,b) for(int i=(a);i<=(b);++i)
#define forUP(i,a,b) for(int i=(a);i<(b);++i)
#define forDown(i,a,b) for(int i=(a);i>=(b);--i)
#define forG(i,u,v) for(int i=head[u],v=to[i];i;i=nxt[i],v=to[i])
#define pb emplace_back
using ll=long long;using ull=unsigned long long;using uint=unsigned int;using db=double;using ld=long db;using pii=std::pair<int,int>;using pdi=std::pair<db,int>;using vl=__int128;using uvl=unsigned __int128;
constexpr int INF=0x3f3f3f3f,MINF=0xcfcfcfcf;constexpr long long INFLL=0x3f3f3f3f3f3f3f3f,MINFLL=0xcfcfcfcfcfcfcfcf;constexpr double INFDB=1e50,eps=1e-9;
template<class _Tp>void chkMax(_Tp &x,const _Tp &y){x<y?x=y:0;}template<class _Tp>void chkMin(_Tp &x,const _Tp &y){x>y?x=y:0;}
constexpr int N=1e5+10;int __test_num=1,__test_id;using namespace std;void __init();

int n,r1,c1,r2,c2;char op[5];bool conR1[N],conR2[N];

struct segNode{
	int l,r;
	bool LL,RR,UU,UD,DU,DD;
}segTree[N<<2];
segNode operator+(const segNode &x,const segNode &y){
	segNode ans;
	ans.l=x.l,ans.r=y.r;
	ans.LL=x.LL|(x.UU&conR1[x.r]&y.LL&conR2[x.r]&x.DD);
	ans.RR=y.RR|(y.UU&conR1[x.r]&x.RR&conR2[x.r]&y.DD);
	ans.UU=(x.UU&conR1[x.r]&y.UU)|(x.UD&conR2[x.r]&y.DU);
	ans.DD=(x.DD&conR2[x.r]&y.DD)|(x.DU&conR1[x.r]&y.UD);
	ans.UD=(x.UU&conR1[x.r]&y.UD)|(x.UD&conR2[x.r]&y.DD);
	ans.DU=(x.DD&conR2[x.r]&y.DU)|(x.DU&conR1[x.r]&y.UU);
	return ans;
}
void pushup(int rt){
	segTree[rt]=segTree[rt<<1]+segTree[rt<<1|1];
}
void build(int rt,int l,int r){
	if(l==r){
		segTree[rt]={l,r,false,false,true,false,false,true};
		return;
	}
	int mid=l+r>>1;
	build(rt<<1,l,mid);build(rt<<1|1,mid+1,r);
	pushup(rt);
}
void updateSameC(int rt,int l,int r,int pos,bool val){
	if(l==r){
		segTree[rt]={l,r,val,val,true,val,val,true};
		return;
	}
	int mid=l+r>>1;
	if(pos<=mid)updateSameC(rt<<1,l,mid,pos,val);
	else updateSameC(rt<<1|1,mid+1,r,pos,val);
	pushup(rt);
}
void updateSameR(int rt,int l,int r,int pos,int row,bool val){
	int mid=l+r>>1;
	if(pos==mid){
		if(row==1)conR1[pos]=val;
		else conR2[pos]=val;
		pushup(rt);
		return;
	}
	if(pos<=mid)updateSameR(rt<<1,l,mid,pos,row,val);
	else updateSameR(rt<<1|1,mid+1,r,pos,row,val);
	pushup(rt);
}
segNode query(int rt,int l,int r,int L,int R){
	if(L<=l&&r<=R)return segTree[rt];
	int mid=l+r>>1;
	if(R<=mid)return query(rt<<1,l,mid,L,R);
	else if(mid<L)return query(rt<<1|1,mid+1,r,L,R);
	else return query(rt<<1,l,mid,L,R)+query(rt<<1|1,mid+1,r,L,R);
}

void __solve(int __test_id){
	scanf("%d",&n);
	build(1,1,n);
	while(scanf("%s",op)){
		if(op[0]=='E')break;
		scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
		if(c1>c2)swap(r1,r2),swap(c1,c2);
		if(op[0]=='O'){
			if(r1==r2)updateSameR(1,1,n,c1,r1,true);
			else updateSameC(1,1,n,c1,true);
		}
		else if(op[0]=='C'){
			if(r1==r2)updateSameR(1,1,n,c1,r1,false);
			else updateSameC(1,1,n,c1,false);
		}
		else{
			segNode ansL=query(1,1,n,1,c1),ansMid=query(1,1,n,c1,c2),ansR=query(1,1,n,c2,n);
			bool ans=false;
			if(r1==1&&r2==1){
				if(ansMid.UU)ans=true;
				if(ansL.RR&&ansMid.DD&&ansR.LL)ans=true;
				if(ansL.RR&&ansMid.DU)ans=true;
			}
			if(r1==2&&r2==2){
				if(ansMid.DD)ans=true;
				if(ansL.RR&&ansMid.UU&&ansR.LL)ans=true;
				if(ansL.RR&&ansMid.UD)ans=true;
			}
			if(r1==1&&r2==2){
				if(ansMid.UD)ans=true;
				if(ansL.RR&&ansMid.DD)ans=true;
				if(ansMid.UU&&ansR.LL)ans=true;
				if(ansL.RR&&ansMid.DU&&ansR.LL)ans=true;
			}
			if(r1==2&&r2==1){
				if(ansMid.DU)ans=true;
				if(ansL.RR&&ansMid.UU)ans=true;
				if(ansMid.DD&&ansR.LL)ans=true;
				if(ansL.RR&&ansMid.UD&&ansR.LL)ans=true;
			}
			printf(ans?"Y\n":"N\n");
		}
	}
}
int main(){
	__init();
	forUp(i,1,__test_num)__solve(i);
	return 0;
}
void __init(){
	//const string __file_name="test";freopen((__file_name+".in").c_str(),"r",stdin);freopen((__file_name+".out").c_str(),"w",stdout);
	//scanf("%d",&__test_num);
}
posted @ 2026-03-13 13:01  LXcjh4998  阅读(2)  评论(0)    收藏  举报