Bzoj 1018 SHOI2008 blocked traffic traffic segment tree

Source: Internet
Author: User

Topic: Given a 2*n grid diagram, several times to change whether an edge is available, many times query whether a two-point is connected


More (Yi) years ago I saw the first response to this question is: This problem can be done?

The path between the two points might be this:


This may also be the case:


This may even be the case:


Can you write this question?

This is a really good question.


We first ignore the third case, assuming that all sides that contribute to the answer are in the middle of two points


So we set up a segment tree with each column as a leaf node

Each node of the segment tree is opened with a two-dimensional array a[2][2]

where A[x][y] Records whether the line x and the right endpoint of the left end of the current interval are connected

So the merger is as follows:


For example, a[0][0] There are two ways to go

The left gray box represents the left interval of the segment tree, and the right gray box represents the right node of the segment tree.

The remaining three of the same

Then the query will go straight to the line tree query. (Ignoring the effect of both sides on the answer)

There are two types of modifications:

If you are modifying a vertical edge, then modify the leaf node directly and update it up.

Pre-open an array to store all the sideways edges, if the modified is a horizontal edge then directly modify the corresponding position of the array, and then find the side of the line is separated by the tree node, from the node start up update can

So the problem is done .... Wait a minute


How is this situation handled?

It's easy to go from the left to the left, from the point on the right, down to the farthest side of the way. All the sides that contribute to the answer are in the middle of these two points.

This can be done with a line tree.


In the middle of the night to write a mess to change the results of direct 1A this you believe?



#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M    100100using namespace Std;struct abcd{bool a[2][2];        ABCD (bool _=false) {a[0][0]=a[1][1]=true;    A[0][1]=a[1][0]=_;    } bool* operator [] (int x) {return a[x];        } friend ABCD Merge (bool Sta[2],abcd x,abcd y) {ABCD re; Re[0][0]= (X[0][0]&sta[0]&y[0][0]) |        (X[0][1]&sta[1]&y[1][0]); Re[1][1]= (x[1][1]&sta[1]&y[1][1]) |        (X[1][0]&sta[0]&y[0][1]); Re[0][1]= (x[0][0]&sta[0]&y[0][1]) |        (X[0][1]&sta[1]&y[1][1]); Re[1][0]= (X[1][1]&sta[1]&y[1][0]) |        (X[1][0]&sta[0]&y[0][0]);    return re;    }};int n;bool a[m][2];struct segtree{segtree *ls,*rs;    ABCD status; Segtree (): LS (0x0), RS (0x0) {} #define PUSH_UP ();    Status=merge (A[mid],ls->status,rs->status);        void Build_tree (int x,int y) {int mid=x+y>>1; if (x==y) return;        (Ls=new segtree)->build_tree (X,mid);        (Rs=new segtree)->build_tree (mid+1,y);    Push_up ();        } void Modify (int x,int y,int pos,int flag) {int mid=x+y>>1;            if (x==y) {new (&status) ABCD (flag);        return;        } if (Pos<=mid) ls->modify (X,mid,pos,flag);        else Rs->modify (Mid+1,y,pos,flag);    Push_up ();        } void Refresh (int x,int y,int pos) {int mid=x+y>>1;            if (pos==mid) {push_up ();        return;        } if (Pos<mid) Ls->refresh (X,mid,pos);        else Rs->refresh (Mid+1,y,pos);    Push_up ();        } void _get_left (int x,int y,int &AMP;POS,ABCD &sta,bool flag) {int mid=x+y>>1;        if (x==y) return;        ABCD Temp=merge (A[y],rs->status,sta); if (Temp[0][flag] | | temp[1][flag]) pos=mid+1,sta=temp,ls->_get_left (x,mid,pos,sTa,flag);    else Rs->_get_left (Mid+1,y,pos,sta,flag);        } void Get_left (int x,int y,int &AMP;POS,ABCD &sta,bool flag) {int mid=x+y>>1;        if (x==y) return;        if (pos<=mid) ls->get_left (X,mid,pos,sta,flag);            else {rs->get_left (mid+1,y,pos,sta,flag);            if (pos!=mid+1) return;            ABCD Temp=merge (A[mid],ls->status,sta);            if (Temp[0][flag] | | temp[1][flag]) pos=x,sta=temp;        else Ls->_get_left (X,mid,pos,sta,flag);        }} void _get_right (int x,int y,int &AMP;POS,ABCD &sta,bool flag) {int mid=x+y>>1;        if (x==y) return;        ABCD Temp=merge (a[x-1],sta,ls->status);        if (Temp[flag][0] | | temp[flag][1]) pos=mid,sta=temp,rs->_get_right (Mid+1,y,pos,sta,flag);    else Ls->_get_right (X,mid,pos,sta,flag); } void get_right (int x,int y,int &pos,abCD &sta,bool flag) {int mid=x+y>>1;        if (x==y) return;        if (pos>mid) rs->get_right (Mid+1,y,pos,sta,flag);            else {ls->get_right (x,mid,pos,sta,flag);            if (Pos!=mid) return;            ABCD Temp=merge (a[mid],sta,rs->status);            if (Temp[flag][0] | | temp[flag][1]) pos=y,sta=temp;        else Rs->_get_right (Mid+1,y,pos,sta,flag);        }} ABCD Get_ans (int x,int y,int l,int r) {int mid=x+y>>1;        if (x==l&&y==r) return status;        if (R<=mid) return Ls->get_ans (X,MID,L,R);        if (L>mid) return Rs->get_ans (MID+1,Y,L,R);    Return Merge (A[mid],ls->get_ans (X,mid,l,mid), Rs->get_ans (Mid+1,y,mid+1,r)); }}*tree=new segtree;void Modify (int x1,int y1,int x2,int y2,bool flag) {if (x1==x2) {if (y1>y2) swap (Y1,Y2)        ;        A[y1][x1-1]=flag; Tree->reFresh (1,N,Y1);    return; } tree->modify (1,n,y1,flag);}    void Query (int x1,int y1,int x2,int y2) {if (y1>y2) swap (X1,X2), swap (Y1,Y2);    ABCD Temp (FALSE);    Tree->get_left (1,n,y1,temp,x1-1);    X1=temp[0][x1-1]?1:2;    New (&temp) ABCD (false);    Tree->get_right (1,n,y2,temp,x2-1);    X2=temp[x2-1][0]?1:2;    Temp=tree->get_ans (1,N,Y1,Y2); Puts (temp[x1-1][x2-1]? " Y ":" N ");}    int main () {int x1,y1,x2,y2;    Char p[10];    cin>>n;    Tree->build_tree (1,n);        while (1) {scanf ("%s", p);        if (p[0]== ' C ') scanf ("%d%d%d%d", &x1,&y1,&x2,&y2), Modify (X1,y1,x2,y2,false);        if (p[0]== ' O ') scanf ("%d%d%d%d", &x1,&y1,&x2,&y2), Modify (x1,y1,x2,y2,true);        if (p[0]== ' A ') scanf ("%d%d%d%d", &x1,&y1,&x2,&y2), Query (X1,Y1,X2,Y2);    if (p[0]== ' E ') break; } return 0;}


Bzoj 1018 SHOI2008 blocked traffic traffic segment tree

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.