tju3243 Blocked Road

Source: Internet
Author: User

there isNseaside villages on X Island, numbered from 1 toN. Nroads is built to connect all of them, which is also numbered from 1 toN, and the road with numberIconnects the villageI andI % N+ 1. Sometimes, for some reasons, some roads is blocked, so some villages is not connected anymore. Now, your is assigned to write a program to offer dynamic information about the connectivity.

At first, all roads is not blocked. The input would tell you the road with number I is blocked or unblocked, or ask you if village I and j is connected. Here, villages is connected means we can reach another village from one via some unblocked road. BTW, all the roads is bidirectional.

InputThe first line of the input contains one integerT, which indicate the number of test cases. The very first line of contains, integers,N andM. N(where 2≤N≤100000) is the total number of the villages,M(where 1≤M≤100000) is the number of queries. The nextMlines each describe one query. For each line, the first integer (0 or 1) indicates the type of the query. If The first integer is 0, there'll be another integerIfollowed, if the roadIis blocked at present, it'll be unblocked, and vice versa. If the query type is 1, there 'll be and both more integersI andJfollowed, if the villageI andJwas connected at present, the answer was 1, otherwise it shall be 0.Outputfor each query of type 1, output its answerSample Input
15 101 2 50 41 4 50 21 3 41 1 30 10 21 2 41 2 5
Sample Output
111010
At first thought is and check set, later thought can not be realized, looked at other people's thinking, found that because the connection of the road is very regular, so you can use a tree array to achieve, this question is mainly to determine whether two points are connected, here because is the ring, so two points there are two possible connection sequence, one is from small to large number The other is from the large number to the small number.
#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include < vector> #include <map> #include <queue> #include <stack> #include <string> #include < Algorithm>using namespace Std;int b[100005],n,zhi[100006];int lowbit (int x) {return x& (-X);} void update (int pos,int num) {while (pos<=n) {b[pos]+=num;pos+=lowbit (pos);}} int getsum (int pos) {int num=0;while (pos>0) {Num+=b[pos];p os-=lowbit (POS);} return num;} int main () {int m,i,j,t,a,c,d;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n,&m); for (i=1;i<=n;i++) {zhi[ I]=1;b[i]=lowbit (i);} for (i=1;i<=m;i++) {scanf ("%d", &a), if (a==0) {scanf ("%d", &c), if (zhi[c]==1) {update (c,-1); zhi[c]=0;} else {update (c,1); zhi[c]=1;}} ELSE{SCANF ("%d%d", &c,&d), if (c>d) swap (c,d), if (Getsum (d-1)-getsum (c-1) ==d-c   | |   Getsum (n)-getsum (d-1) +getsum (c-1) ==c+n-d) printf ("1\n"), Else printf ("0\n");}} return 0;}



tju3243 Blocked Road

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.