"Bzoj 1018" [SHOI2008] clogged traffic traffic

Source: Internet
Author: User

1018: [SHOI2008] blocked traffic traffic time limit: 3 Sec Memory Limit: 162 MB
Submit: 1811 Solved: 580
[Submit] [Status] 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 Input2
Open 1 1 1 2
Open 1 2 2 2
Ask 1 1 2 2
Ask 2 1 2 2
ExitSample OutputY
N

Maintain connectivity with line segment tree, very test the classification discussion ability of the code farming problem!


For an interval of l-r, the segment tree is maintained in the upper-left lower-left bottom right-hand side of the four-place connectivity, in my program:

(+)

(2,1) (2,2)


H1: (+--)

H2: (2,1)--(2,2)

S1: (2,1)

S2: (2,2)

X1: (2,2)

X2: (2,1)


To maintain these six messages, at the same time, because the interval is merged, a l[x][y][k] is used to indicate (x, y) This position is connected to the surrounding (where k=1,2,3,4 is connected to the upper and lower sides respectively).


The next step is to deal with the changes: when you start thinking about merging, you feel the route is very, very much. In fact, one should remember:


The two intervals to be merged are already connected, and the current interval is considered to be a combination of two intervals.

(See Code Update for details)


Finally, query the connectivity of L-r:


From the l-r can go directly past, may also from L to front to R, may also from R to back to L.


Therefore, the connectivity of the three intervals of the 1-l,l-r,r-n are recorded as A1,a2,a3 respectively.


If the A1.S2 is connected, the A2.S1 is connected, and the A3.S1 is connected a2.s2.


Then according to the different coordinates of the discussion on it can be (must be considered thoughtful AH!!) )。


#include <algorithm> #include <cstdio> #include <iostream> #include <cstring> #include < Cstdlib> #include <cmath>using namespace std;struct node{int l,r,h1,h2,s1,s2,x1,x2;} A[500005];int n,l[3][100005][5];void Build (int x,int l,int r) {a[x].l=l,a[x].r=r;a[x].h1=a[x].h2=a[x].s1=a[x].s2=a[x ].x1=a[x].x2=0;if (l==r) {A[x].h1=a[x].h2=1;return;} int m= (L+R) >>1; Build (X&LT;&LT;1,L,M); Build ((x<<1) +1,m+1,r);} void Modi (int x1,int y1,int x2,int y2,int k) {if (x1==x2) {if (y1>y2) swap (y1,y2); l[x1][y1][4]=l[x2][y2][3]=k;} Else{if (x1>x2) swap (x1,x2); l[x1][y1][2]=l[x2][y2][1]=k;}} void Modis (int x) {a[x].s1=l[1][a[x].l][2];a[x].s2=l[1][a[x].r][2];a[x].x1=a[x].x2=0;if (A[X].S1|A[X].S2) a[x].x1=a [X].x2=1;} Node Merge (node X,node y) {if (X.L==Y.L&AMP;&AMP;X.R==Y.R) return x; Node k;int p1=l[1][x.r][4],p2=l[2][x.r][4];k.h1= (X.H1&AMP;P1&AMP;Y.H1) | (x.x1&p2&y.x2); k.h2= (X.H2&AMP;P2&AMP;Y.H2) | (x.x2&p1&y.x1); k.s1=x.s1| (X.H1&AMP;P1&AMP;Y.S1&AMP;P2&AMP;X.H2); k. s2=y.s2| (Y.H1&AMP;P1&AMP;X.S2&AMP;P2&AMP;Y.H2); k.x1= (x.h1&p1&y.x1) | (X.X1&AMP;P2&AMP;Y.H2); k.x2= (Y.X2&AMP;P2&AMP;X.H2) | (y.h1&p1&x.x2); K.l=x.l,k.r=y.r;return K;} void Update1 (int x,int l,int r) {int m= (A[X].L+A[X].R) >>1;if (l==m) {A[x]=merge (a[x<<1],a[(x<<1) +1]) ; return;} if (l<m) Update1 (x<<1,l,r), Else Update1 ((x<<1) +1,l,r); A[x]=merge (a[x<<1],a[(x<<1) +1]);} void Update2 (int x,int k) {if (a[x].l==a[x].r&&a[x].l==k) {Modis (x); return;} int m= (A[X].L+A[X].R) >>1;if (k<=m) Update2 (x<<1,k); else Update2 ((x<<1) +1,k); A[x]=merge (a[x <<1],a[(x<<1) +1]);} Node Find (int x,int l,int R) {if (a[x].l>=l&&a[x].r<=r) return a[x];int m= (A[X].L+A[X].R) >>1;if (r &LT;=M) return Find (X<<1,l,r), if (l>m) return find ((x<<1) +1,l,r), Return Merge (Find (X<<1,l,r), Find ((x<<1) +1,l,r));} BOOL Query (int x1,int y1,int x2,int y2) {Node a1=find (1,1,y1), A2=find (1,y1,y2), A3=find (1,y2,n); a2.s1^=a1.s2,a2.s2^=a3.s1;if (x1==x2) {if (x1==1) return a2.h1| ( A2.S1&AMP;A2.H2&AMP;A2.S2) | (A2.S1&AMP;A2.X2) | (A2.X1&AMP;A2.S2); else return a2.h2| (A2.S1&AMP;A2.H1&AMP;A2.S2) | (A2.S1&AMP;A2.X1) | (A2.S2&AMP;A2.X2);} if (X1&LT;X2) return a2.x1| (A2.S1&AMP;A2.H2) |        (A2.H1&AMP;A2.S2); Return a2.x2| (A2.S1&AMP;A2.H1) | (A2.H2&AMP;A2.S2);} int main () {scanf ("%d", &n); Build (1,1,n); Char S[10];while (scanf ("%s", s)!=eof) {if (s[0]== ' E ') break;int x1,y1,x2,y2;scanf ("%d%d%d%d", &x1, &AMP;Y1,&AMP;X2,&AMP;Y2), switch (S[0]) {case ' O ': Modi (x1,y1,x2,y2,1), if (x1==x2) Update1 (1,min (Y1,y2), Max (y1,y2));                                ElseUpdate2 (1,y1); Break;case ' C ': Modi (x1,y1,x2,y2,0); if (x1==x2) Update1 (1,min (Y1,y2), Max (Y1,y2)), ElseUpdate2 (1,y1); Break;case ' A ': if (y1>y2) swap (X1,X2), swap (Y1,Y2) ; if (Query (x1,y1,x2,y2)) printf ("y\n"), Else printf ("n\n"); return 0;}



Sentiment:

1. Difficult AC history:

When the achievement of the L=r node, H1 and H2 assigned to the value of 1;

When you merge, be sure to assign a value to the LR of this node, otherwise it will be re;

When I queried, I thought each interval could be divided into two segments on the tree. Obviously not.

When querying for the x1=x2 situation, because A1 and A3 may change A2 S1 and S2, the route to be discussed!!!


2. The first time you know the line tree can maintain connectivity, it feels amazing ~

"Bzoj 1018" [SHOI2008] clogged traffic traffic

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.