1018: [SHOI2008] clogged traffic traffic
Time Limit:3 Sec Memory limit:162 MB
submit:2252 solved:711
[Submit] [Status] [Discuss]
Description
One day, because of some kind of cross-over phenomenon, you came to the legendary small country. The layout of the small country is very peculiar, the whole country's transportation system can be regarded as a 2 row C-column rectangular grid, each point on the grid represents a city, there is a road between adjacent cities, so there are a total of 2C cities and 3c-2 Road. The traffic situation in the small country is very bad. Sometimes, because of traffic jams, the roads between the two cities become disconnected until the congestion is resolved and the roads are unblocked. When you first arrived, you were determined to volunteer for a job at the Ministry of Transport, and the minister heard that you came from a highly technologically advanced world, and was overjoyed to ask you to write a query answering system to save the already sick little nation traffic system. The traffic Department of the Little Nation will provide you with some transportation information, and your task is to answer queries based on current traffic conditions. Traffic information can be divided into the following formats: Close R1 C1 R2 C2: The road between adjacent two cities (R1,C1) and (R2,C2) is blocked; Open R1 C1 R2 C2: The road between two adjacent cities (R1,C1) and (R2,C2) is unblocked; Ask R 1 C1 R2 C2: Ask if the City (R1,C1) and (R2,C2) are connected. Returns y if there is a path to connect the two cities, otherwise returns N;
Input
The first line has only one integer, C, which represents the number of columns in the grid. In the next few lines, each behavior is a traffic message, ending with a separate line "Exit". We assume that in the beginning all roads are clogged.
Output
For each query, output a "Y" or "N".
Sample Input
2
Open 1 1 1 2
Open 1 2 2 2
Ask 1 1 2 2
Ask 2 1 2 2
Exit
Sample Output
Y
N
A problem of maintaining connectivity with a segment tree.
The first to do this kind of problem, in fact, we need to maintain a section of the relationship between the link, and then use our maintenance of these relationships to query.
For this problem, I think there are two ways to make a contribution:
①: A leaf node in each column.
②: A leaf node with each interval (that is, about two nodes per 1 horizontal road connections).
I chose the first, we first need to maintain 6 variables (that is, the interval of 6 types of connectivity), respectively, Luru,ldrd,lurd,ldru,luld,rurd (L:left;r:right;u:up;d:down)
Then, because there are changes to the lateral adjacent roads, we also maintain a side[] to indicate whether the node can extend outward.
The last is the most troublesome query, need to be divided into a number of situations, especially when we query this section of the connectivity needs to rely on the outside of the range to be able to connect this situation, these situations do not repeat, the specific look at the code bar.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace STD;Const intn=100100;structs{BOOLluru,ldrd,lurd,ldru,luld,rurd,side[3];} tr[n*4];Charch[Ten];intN,R1,C1,R2,C2;#define MID (L+r)/2#define L K<<1,l,mid#define R K<<1|1,mid+1,rs update (s x,s y) {s ans; ans.side[1]=y.side[1]; ans.side[2]=y.side[2]; ans.luru=ans.ldrd=ans.lurd=ans.ldru=ans.luld=ans.rurd=false;if((x.luru&&x.side[1]&&y.luru) | | (x.lurd&&x.side[2]&&y.ldru)) ans.luru=true;if((x.ldrd&&x.side[2]&&Y.LDRD) | | (x.ldru&&x.side[1]&&y.lurd)) ans.ldrd=true;if((x.luru&&x.side[1]&&y.lurd) | | (x.lurd&&x.side[2]&&Y.LDRD)) ans.lurd=true;if((x.ldrd&&x.side[2]&&y.ldru) | | (x.ldru&&x.side[1]&&y.luru)) ans.ldru=true;if((x.luld) | | (x.luru&&x.side[1]&&y.luld&&x.side[2]&&X.LDRD)) ans.luld=true;if((Y.rurd) | | (y.luru&&x.side[1]&&x.rurd&&x.side[2]&&Y.LDRD)) ans.rurd=true;returnAns;}voidBuildintKintLintR) {if(L==R) {tr[k].luru=tr[k].ldrd=true;return; } build (L); build (R); Tr[k]=update (tr[k<<1],tr[k<<1|1]);}voidInsert_a (intKintLintRintXBOOLKind) {if(L==R) {tr[k].luld=tr[k].rurd=tr[k].lurd=tr[k].ldru=kind;return; }if(X<=mid) insert_a (L,x,kind);ElseInsert_a (R,x,kind); Tr[k]=update (tr[k<<1],tr[k<<1|1]);}voidInsert_b (intKintLintRintXintYBOOLKind) {if(L==R) {tr[k].side[y]=kind;return; }if(X<=mid) Insert_b (L,x,y,kind);ElseInsert_b (R,x,y,kind); Tr[k]=update (tr[k<<1],tr[k<<1|1]);} S Query (intKintLintRintXintY) {S ans1,ans2;BOOLleft=false, right=false;if(X<=L&&Y>=R)returnTR[K];if(X<=mid) Ans1=query (l,x,y), left=true;if(Y>mid) Ans2=query (r,x,y), right=true;if(Right&&left)returnUpdate (ANS1,ANS2);Else returnLeft?ans1:ans2;}BOOLCheck () {S now,pre,last;if(C1>C2) {swap (R1,R2); Swap (C1,C2); } now=query (1,1, N,C1,C2); Pre=query (1,1N1, C1); Last=query (1,1, n,c2,n);if(R1==R2) {if((r1==1) && ((Now.luru) | | (Last.luld&&now.lurd) | | (Pre.rurd&&now.ldru) | | (pre.rurd&&now.ldrd&&last.luld)))return true;if((r1==2) && ((NOW.LDRD) | | (Last.luld&&now.ldru) | | (Pre.rurd&&now.lurd) | | (pre.rurd&&now.luru&&last.luld)))return true; }Else{if((r1==1) && ((now.lurd) | | (Last.luld&&now.luru) | | (PRE.RURD&&NOW.LDRD) | | (pre.rurd&&now.ldru&&last.luld)))return true;if((r1==2) && ((Now.ldru) | | (LAST.LULD&&NOW.LDRD) | | (Pre.rurd&&now.luru) | | (pre.rurd&&now.lurd&&last.luld)))return true; }return false;}intMain () {intI,j;scanf("%d", &n); Build1,1, n); while(scanf("%*c%s", &ch)) {if(ch[0]==' E ') Break;scanf("%d%d%d%d", &R1,&C1,&R2,&C2);if(ch[0]==' O '){if(C1==C2) Insert_a (1,1, N,C1,1);ElseInsert_b (1,1, N,min (C1,C2), R1,1); }if(ch[0]==' C '){if(C1==C2) Insert_a (1,1, N,C1,0);ElseInsert_b (1,1, N,min (C1,C2), R1,0); }if(ch[0]==' A '){if(check ())printf("y\n");Else printf("n\n"); } }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"bzoj1018" "SHOI2008" "clogged traffic traffic"