http://www.lydsy.com/JudgeOnline/problem.php?id=1018 (题目链接)
题意
一个2行C列的矩形网格,网格上的每个点代表一个城市,相邻的城市之间有一条道路。一开始每条道路都是堵塞的,堵塞即为不可经过。经过一些操作后,可能某些道路通畅了,也可能某些道路堵塞了。询问两个城市是否联通。
Solution
看了题解才醒悟,线段树神题啊。
从一座城市走到另一座城市,一共有4种方案。
若两城市在同一行(比如说s1,s2),那么:
- s1-->s2
- s1-->s3,s3-->s2
- s1-->s4,s4-->s2
- s1-->s3,s3-->s4,s4-->s2
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAN4AAAB9CAIAAABCjUqgAAAOh0lEQVR4nO2df3AVxR3A3+CgjHUsdkZBBy0yVqXORAbqoE1LKf6gmGAYpeCkVYOxYMuIpigo4MQa8AeUkE6kGGh5gcQXgRceQjAZIhJQEtOmk0YNmWqejC+TMSTy8l7fM+EE2f5x9Hjs7u3t3t3e7R37mfsrcz/29n3y3R+3PwJAIhGSgNsJkEjwSDUlgiLVlAiKVFMiKFJNiaBINSWCwqymklZay1sjBXuqZlUbHpGCPR+89uFQfIhH0iX+hlnN5tIWGikhQROxJI/US3wMs5o75u5kVVM9mktbUn1pHu8g8SXMaprzUhb0ElbcUVM96hbvl3ZK9LCqJuHMeDS+d+Few/ApS3kJFo5qqnQfiBo252X5LkHhrqYKpaAygko0HFJTIxFLkkt5GUElKk6rqRKPxsmdUAeWN5q7szkGlbNPfvTf8bsHAtv7nDnG7x54+Ejiq6HvnHxNKzB9arElxLijJgAgHo0fXPU+4d1ay1vPKGdM398Q53X0qLWsUtoVYlxTU4MQQQ+uet/6/SGENRI6xu7qLziaPJ7i+M9Jw1B8qG7xfhNSaofp2Om+mkA/gobyauytdw4qZ3/13qDr2gkeSq2ESexhrgObTU0lrfBQUyXVl8b+g1rvmfdKpBQhlCppxWKYtKtkZ1OztbyVn5oq9UUN9tpJHynvrD95qE+x93VQuhKnb33nazHjKI2X4fza3rZewk0OLG/EXsiaGDY1oUohj7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swjq/aKc35cwKe4k1x6DMVwMnwBXrDx1ZyrsHnruarEB31zsNMoP5MXSMHjViU86Y409PIEfKlsIbpo+/3NwjLh95OfTKWVdn2fsW9KzOXpOZkuK7XlL/PnLcFeNDMyFXflh57zXPTr7kqss4JQZ6HM0llPKYxJyarhxWjNRYcNsCvhnKwpQxU/QSc+mNV9646360p/OGLXdzElQ4NbfOvGCB3SljpmBPc1fK4ZU3W5dSpeKezZnv+9xPltlyW9MQft3r1tyFqunYYTHxNvDM5AsWTtHKFIjhlTd7OliqoKX5xB9MtOXOpiH8uiPHXXH9xukXr5qEMiWTQ49d72kpVYQqzVUM03PpjVe6Iqgtib8AE00zqnbWQBSUz3JISrpucxNAbXMRlnmhyXxbtjxkOgwXR6BPvAY3NX0B9Kau9BmRk0Q40zFBpzY8cvhEm72JB7aoKeyKPNYR8J9QwCTR47Sawq7IYxGGwakOImCS6OGuJroxral0ik5bRVvmOzo8nkMPT+c8dzXRuVSm0ik0SlqBlj/gMSvNBJ7Oee5q6g3Q8hPowGHesywo8XTOc1cTfYbPWkLYuRZuJ+ocYqaKEhfUVKtiH7z2oQ8cxY7OFKSiCZCc99CmbMAZNdGWkPYTJmJJc/d0HSWttJa3Rgr2oO/l/MBhPby7KRtwRk20JSRIEB2KD7UH27F6mT7ECZlAf5KQ+KCTWMjnm1QzEUva+/MLe4Tza8UJmQDXBvVKmY5OFiCfb1JNAEAilty7EF4D0k+HifXYncGLZTrasjQckGBezUzi0bjPNA3n14rwxRyLYW3KE4dh9tqjpkr3gag/SnnRCnEItEz34mH4mnaqqWWcuxE0nF/rwDpE7qLXQ+KVg6Zlab+aEgfwdJm+Y+5OmkJJqulJXC+aTB/0lSWppkRQpJoSQZFqSgRFqikRFKmmRFCkmhJBkWpKBEWqKREUqaZEUKSaEkGRakoERaopERSppkRQpJoSQZFqSgRFqikRlP8BKfUBAdnSrzoAAAAASUVORK5CYII=" alt="技术分享" />
若两城市不在同一行(比如说s1,s4),那么:
- s1-->s3,s3-->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JQZ0Uch1dwbCp+r9W7jpeREhwsRht7vWKh1wogQQwdO1TSI0O8KMn/GCSh2goAgMMbKWr0Je2QkMzFXcFoWavUyFDpvGj08aUrENqtSCSpIaqEDhlBgEUGk2EkKziQM0ToKZl1HhYcjI7xrBCaek1DMeKsDQYf41kRJ2kuTsABljaYEgNbAaTl4swHBi7GtiIRFQ1hF/0VjnmsYDDUglnBYMAwKxgMGGYFgwHDrGAwYJgVDAYMs4LBgGFWMBgwzAoGA4ZZwWDAMCsYDBhmBYMBw6xgMGCYFQwGDLOCwYBhVjAYMP8H3zTWjlYwLFoAAAAASUVORK5CYII=" alt="技术分享" />
而我们怎么维护这个东西呢?考虑用线段树。每个节点表示区间[s,t]的矩形,并且记录8个变量:U,D,l,r,u,d,p,q。
其中,mid为(s+t)/2:
U:第一行mid,mid+1两列之间是否联通
D:第二行mid,mid+1两列之间是否联通
l:s1,s3是否联通
r:s2,s4是否联通
u:s1,s2是否联通
d:s3,s4是否联通
q:s1,s4是否联通
p:s3,s2是否联通
发现如果s=t的话,也就是说只有上下2个城市,那么U,D,u,d这4个变量就没有意义了,我们将它们全部赋值为1,避免一些不必要的麻烦。
于是,线段树的操作就变得很显然了。另外注意线段树中的每一个节点只维护这个矩形四个角上的点的连通性,并且我们并不关心它的路径是什么,只考虑使其连通的路径存在于当前矩形的情况。
细节
分类讨论有点多,别犯些奇奇怪怪的错误。
代码
// bzoj1018
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=100010;
struct tree {int l,r;}tr[maxn<<2];
struct data {int U,D,l,r,u,d,p,q;}w[maxn<<2];
int c; void merge(data &k,data l,data r) {
k.l=l.l | (l.u & k.U & r.l & k.D & l.d);
k.r=r.r | (r.u & k.U & l.r & k.D & r.d);
k.u=(l.u & k.U & r.u) | (l.q & k.D & r.p);
k.d=(l.d & k.D & r.d) | (l.p & k.U & r.q);
k.q=(l.u & k.U & r.q) | (l.q & k.D & r.d);
k.p=(l.d & k.D & r.p) | (l.p & k.U & r.u);
}
void build(int k,int s,int t) {
tr[k].l=s;tr[k].r=t;
if (s==t) {w[k].U=w[k].D=w[k].u=w[k].d=1;return;}
int mid=(s+t)>>1;
build(k<<1,s,mid);
build(k<<1|1,mid+1,t);
}
void updater(int k,int x,int T,int val) {
int l=tr[k].l,r=tr[k].r,mid=(l+r)>>1;
if (x==mid) {
if (T==1) w[k].U=val;
else w[k].D=val;
merge(w[k],w[k<<1],w[k<<1|1]);
return;
}
if (x<=mid) updater(k<<1,x,T,val);
else updater(k<<1|1,x,T,val);
merge(w[k],w[k<<1],w[k<<1|1]);
}
void updatec(int k,int x,int val) {
int l=tr[k].l,r=tr[k].r,mid=(l+r)>>1;
if (l==r) {w[k].l=w[k].r=w[k].p=w[k].q=val;return;}
if (x<=mid) updatec(k<<1,x,val);
else updatec(k<<1|1,x,val);
merge(w[k],w[k<<1],w[k<<1|1]);
}
data query(int k,int s,int t) {
int l=tr[k].l,r=tr[k].r,mid=(l+r)>>1;
if (s<=l && r<=t) return w[k];
if (t<=mid) return query(k<<1,s,t);
else if (s>mid) return query(k<<1|1,s,t);
else {
data res=w[k];
merge(res,query(k<<1,s,t),query(k<<1|1,s,t));
return res;
}
}
int main() {
scanf("%d",&c);
build(1,1,c);
char s[10];
int r1,r2,c1,c2;
while (scanf("%s",s)!=EOF) {
if (s[0]=='E') break;
scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
if (c1>c2) swap(c1,c2),swap(r1,r2);
if (s[0]=='O') {
if (r1==r2) updater(1,c1,r1,1);
else updatec(1,c1,1);
}
if (s[0]=='C') {
if (r1==r2) updater(1,c1,r1,0);
else updatec(1,c1,0);
}
if (s[0]=='A') {
data l=query(1,1,c1),x=query(1,c1,c2),r=query(1,c2,c);
int ans;
if (r1==1 && r2==1)
ans=x.u | (l.r & x.p) | (x.q & r.l) | (l.r & x.d & r.l);
if (r1==1 && r2==2)
ans=x.q | (l.r & x.d) | (x.u & r.l) | (l.r & x.p & r.l);
if (r1==2 && r2==1)
ans=x.p | (l.r & x.u) | (x.d & r.l) | (l.r & x.q & r.l);
if (r1==2 && r2==2)
ans=x.d | (l.r & x.q) | (x.p & r.l) | (l.r & x.u & r.l);
puts(ans ? "Y" : "N");
}
}
return 0;
}