Tree-like array-the solving of artificial lake

Source: Internet
Author: User

Title: Artificial Lake

Describe:

"Title description"

There is a lake, surrounded by cities, and each city is connected to only two cities adjacent to him. Suppose there are N cities, numbered 1-n, highways are bidirectional, highways are sometimes good, sometimes bad, and now ask you whether two cities can reach each other.

"Input Format"

The first row is two numbers, one 2<=n<=100000 and 1<=m<=100000, each representing the number of cities and the number of queries; the next M-line, three-digit f,a,b per line. F=0, if the road between a A and a is good before, now it becomes bad, if it is bad, now it becomes good. When f=1, ask if the two cities of a, B can reach each other.

"Output Format"

For each f=1 query, the output "YES" can be reached, otherwise the output "NO".

"Sample Input"5 Ten
1 2 5
0 4 5
1 4 5
0 2 3
1 3 4
1 1 3
0 1 2
0 2 3
1 2 4
1 2 5
"Sample Output"
YES
YES
YES
NO
YES
NO
Prompted

30% 2<=n,m<=100

50% 2<=n,m<=10000

100% 2<=n,m<=100000

At first glance, it is easy to think of a search can be used, but because M is too large, plus the range of n is not small, the complexity of the maximum $o (NM) $, so 30 points abandoned therapy ... However, a tree-like array can be a good solution to this problem. Open the array to 2*n, subscript 1. 2*n, the X-cell represents the path state from the X-to (x+1) mod 2*n, which is indicated by 1, and 0 means no. Open an array to store the sum of the roads in some intervals, then make a judgment on each change, and then change the values in the tree array two times. In the case of a query operation, it is calculated two times:

1. Seek the total state of the path between the x~y and then judge (note that if it is ' no ', do not jump out)

2. Seek the total state of the path between the y~x+n and then judge it.

The implementation of the concrete is not difficult, the middle of the addition of some small tricks. But...... F=0, the size of x, Y is not necessarily small to large ... I was all over WA once ... Change it again before AC ... In a word, I finally see a tree-like array of questions that are not so fruit ...

AC Code:

{

Program zht;vari,j,k,m,n,y,x,f,t,z1,z2:longint;a:array[0..200000] of longint;c:array[0..200000] of Longint;   Procedure Change;vark:longint;begink:=0;if a[x]=1 then BEGIN t:=-1;   a[x]:=0;   a[x+n]:=0;    end ELSE begin t:=1;     A[x]:=1;     A[x+n]:=1;                    End Change the state of k:=x; While K<=2*n does begin c[k]:=c[k]+t; K:=k+ (k and (k)); End;k:=x+n;while k<=2*n do begin c[k]:=c[k]+t; K:=k+ (k and (k));                                  End       Tree-like array two-time change end; Gai Bian Zhuang taiprocedure chazhao;vark:longint;begink:=0;k:=x-1; While k>0 do begin z1:=z1+c[k]; K:=k-(k and (k)); End;k:=0;k:=y-1; While k>0 do begin z2:=z2+c[k]; K:=k-(k and (k)); End;z1:=z2-z1;if Z1=y-x THEN BEGIN Writeln (' YES '); Exit        End Directly find z1:=0;z2:=0;k:=y-1;  While k>0 do begin z1:=z1+c[k];  K:=k-(k and (k)); end;k:=x+n-1;  While k>0 do begin z2:=z2+c[k];  K:=k-(k and (k)); End;if z2-z1=n-(y-x) then Writeln (' YES ') Else writeln (' NO '); Reverse two lookup end;beginassign (input, ' lakee.in '); Assign (output, ' lakee.out '); reset (input); rewrite (output); Readln (n,m); For I:=1 to 2*n-1 doa[i]:=1;for i:=1 to 2*n-1 dobegint:=i and (-i); For j:=i-t+1 to I does c[i]:=c[i]+1;end;for i:=1 to M dobeginreadln (f,x,y), t:=0;z1:=0;z2:=0;if x>y then begin t:=x; X:=y; y:=t;        End                        Pit Point ~~~t:=0;if f=0 then change else chazhao;end;                   The classification discusses close (input), close (output), and end. A short main program
}

<marvolo Original, non-reprint >

Tree-like array-the solving of artificial lake

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.