洛谷 4246 BZOJ 1018 [SHOI2008]堵塞的交通

洛谷 4246 BZOJ 1018 [SHOI2008]堵塞的交通

【题解】

  原来线段树还可以这么玩。。

  我们用线段树维护连通性。对于一个矩形,我们用4个标记维护4个点的联通情况,再用两个标记维护右边两个点与它们右边的与它们在同一行的点的联通情况。

  画图表示,就是

    洛谷 4246 BZOJ 1018 [SHOI2008]堵塞的交通

  另一个关键问题是对于询问(r1,c1,r2,c2),并不是只可以走c1到c2之间的部分,它可以绕路走,这就需要我们在处理询问的时候把c1,c2进行扩展。具体说来,就是让c1一直向左走,让c2一直向右走,

然后查询新的(r1,c1,r2,c2). 为什么这样做是对的呢?

  洛谷 4246 BZOJ 1018 [SHOI2008]堵塞的交通

  洛谷 4246 BZOJ 1018 [SHOI2008]堵塞的交通

  通过上图我们可以发现要绕路走必须走到跟r1,r2不同的行,也就是一定会通过c1左边的竖着的边以及c2右边的竖着的边。而且一定存在一种走法使得绕路走的部分形成一个类似括号的形状。我们把c1一直左移得到c1',就可以保证[c1',c1]之间一定有竖着的边(如果c1到c1左边联通的部分之间有竖边存在的话)。右边的c2也是同理。这样查询新的c1,c2就转化成了没有绕路走的情况。

 // luogu-judger-enable-o2
#include<cstdio>
#include<algorithm>
#define N 100010
#define rg register
#define ls (u<<1)
#define rs (u<<1|1)
using namespace std;
int n;
struct tree{
int t1,t2,t3,t4,t5,t6;
}a[N<<];
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
inline void pushup(int u){
a[u].t1=(a[ls].t1&&a[ls].t5&&a[rs].t1)||(a[ls].t3&&a[ls].t6&&a[rs].t4);
a[u].t2=(a[ls].t2&&a[ls].t6&&a[rs].t2)||(a[ls].t4&&a[ls].t5&&a[rs].t3);
a[u].t3=(a[ls].t1&&a[ls].t5&&a[rs].t3)||(a[ls].t3&&a[ls].t6&&a[rs].t2);
a[u].t4=(a[ls].t4&&a[ls].t5&&a[rs].t1)||(a[ls].t2&&a[ls].t6&&a[rs].t4);
a[u].t5=a[rs].t5;
a[u].t6=a[rs].t6;
}
void build(int u,int l,int r){
if(l<r){
int mid=(l+r)>>;
build(ls,l,mid); build(rs,mid+,r);
}
else a[u].t1=a[u].t2=;
}
void update(int u,int l,int r,int pos,int type,int del){
if(l==r){
if(type==||type==) a[u].t3=a[u].t4=del;
if(type==) a[u].t5=del;
if(type==) a[u].t6=del;
return;
}
int mid=(l+r)>>;
if(pos<=mid) update(ls,l,mid,pos,type,del);
else update(rs,mid+,r,pos,type,del);
pushup(u);
}
tree query(int u,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr) return a[u];
tree ret,L,R; int mid=(l+r)>>;
L=R=(tree){,,,,,};
if(ql<=mid) ret=L=query(ls,l,mid,ql,qr);
if(qr>mid) ret=R=query(rs,mid+,r,ql,qr);
if(ql<=mid&&qr>mid){
ret.t1=(L.t1&&L.t5&&R.t1)||(L.t3&&L.t6&&R.t4);
ret.t2=(L.t2&&L.t6&&R.t2)||(L.t4&&L.t5&&R.t3);
ret.t3=(L.t1&&L.t5&&R.t3)||(L.t3&&L.t6&&R.t2);
ret.t4=(L.t4&&L.t5&&R.t1)||(L.t2&&L.t6&&R.t4);
}
return ret;
}
int goleft(int u,int l,int r,int type,int pos){
if(r==pos&&((type==&&a[u].t1)||(type==&&a[u].t2))) return l;
int mid=(l+r)>>;
if(pos<=mid) return goleft(ls,l,mid,type,pos);
int L=goleft(rs,mid+,r,type,pos);
if(L==mid+&&((type==&&a[ls].t5)||(type==&&a[ls].t6)))
return goleft(ls,l,mid,type,mid);
return L;
}
int goright(int u,int l,int r,int type,int pos){
if(l==pos&&((type==&&a[u].t1)||(type==&&a[u].t2))) return r;
int mid=(l+r)>>;
if(pos>mid) return goright(rs,mid+,r,type,pos);
int R=goright(ls,l,mid,type,pos);
if(R==mid&&((type==&&a[ls].t5)||(type==&&a[ls].t6)))
return goright(rs,mid+,r,type,mid+);
return R;
}
int main(){
n=read(); build(,,n);
while(){
char s[]; scanf("%s",s+);
while(s[]!='E'&&s[]!='C'&&s[]!='O'&&s[]!='A') scanf("%s",s+);
if(s[]=='E') break;
int r1=read(),c1=read(),r2=read(),c2=read();
if(c1>c2) swap(c1,c2),swap(r1,r2);
if(s[]=='C'){
if(c1==c2) update(,,n,c1,,);
if(r1==r2) update(,,n,c1,r1==?:,);
}
if(s[]=='O'){
if(c1==c2) update(,,n,c1,,);
if(r1==r2) update(,,n,c1,r1==?:,);
}
if(s[]=='A'){
c1=goleft(,,n,r1,c1); c2=goright(,,n,r2,c2);
tree ans=query(,,n,c1,c2);
bool flag=;
if(r1==&&r2==) flag=ans.t1;
if(r1==&&r2==) flag=ans.t2;
if(r1==&&r2==) flag=ans.t3;
if(r1==&&r2==) flag=ans.t4;
puts(flag?"Y":"N");
}
}
return ;
}
上一篇:BZOJ1018[SHOI2008]堵塞的交通——线段树


下一篇:(转载)猫都能学会的Unity3D Shader入门指南(一)