There are N seaside villages on X island, numbered from 1 to N. N roads
are built to connect all of them, which are also numbered from 1 to N, and the road with number i connects
the village i and i % N +
1. Sometimes, for some reasons, some roads are blocked, so some villages are not connected anymore. Now, you are assigned to write a program to offer dynamic information about the connectivity.

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

Input

The first line of the input contains one integer T, which indicate
the number of test cases. The very first line of each case contains two integers, N and M. N (where
2 ≤ N ≤ 100000) is the total number of the villages, M (where
1 ≤ M ≤ 100000) is the number of queries. The next M lines
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 will be another integer i followed,
if the road i is blocked at present, it will be unblocked, and vice versa. If the query type is 1, there will be two more
integers i and j followed,
if the village i and j are
connected at present, the answer is 1, otherwise it shall be 0.

Output

For each query of type 1, output its answer in a single line

Sample Input

1
5 10
1 2 5
0 4
1 4 5
0 2
1 3 4
1 1 3
0 1
0 2
1 2 4
1 2 5

Sample Output

1
1
1
0
1
0
一开始以为是并查集,后来想想不能实现,看了别人的思路,发现因为连接的道路很有规律,所以可以用树状数组来实现,这题主要是判断两个点是否是相连的,这里因为是环装,所以两点有两种可能的连接顺序,一种是从小的数到大的数,另一种是从大的数到小的数。

#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];pos-=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的更多相关文章

  1. iOS App 不支持http协议 App Transport Security has blocked a cleartext HTTP (http://)

    目前iOS已经不支持http协议了,不过可以通过info.plist设置允许 App Transport Security has blocked a cleartext HTTP (http://) ...

  2. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  3. POJ 3204 Ikki's Story I - Road Reconstruction

    Ikki's Story I - Road Reconstruction Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 7 ...

  4. Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:9001/api/size/get. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

    Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http:/ ...

  5. App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file

    ios进行http请求,会出现这个问题: App Transport Security has blocked a cleartext HTTP (http://) resource load sin ...

  6. Linux 日志报错 xxx blocked for more than 120 seconds

    监控作业发现一台服务器(Red Hat Enterprise Linux Server release 5.7)从凌晨1:32开始,有一小段时间无法响应,数据库也连接不上,后面又正常了.早上检查了监听 ...

  7. App Transport Security has blocked a cleartext HTTP (http://)

    使用SDWebImage加载“http://”开头的图片报错,错误如下: App Transport Security has blocked a cleartext HTTP (http://) r ...

  8. Codeforces #380 div2 C(729C) Road to Cinema

    C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  9. dp or 贪心 --- hdu : Road Trip

    Road Trip Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 29 ...

随机推荐

  1. 十四:SQL注入之类型及提交注入

    简要明确参数类型 数字,字符,搜索,json等 简要明确请求方法 GET,POST,COOKIE,REQUEST,HTTP头 其中SQL语句干扰符号:' " % ) } 等,具体查看用法 非 ...

  2. Centos搭建Git服务端

    首先需要安装git,可以使用yum源在线安装 yum install -y git 创建一个git用户,用来运行管理git服务 adduser git 初始化git仓库(这里我们选择/home/git ...

  3. zabbix_server上的问题

    不要写成127.0.0.1,要不然一直包zabbix agent没有启动.

  4. 【Software Test】Introduction to Software Testing

    Introduction to Software Testing 文章目录 Going to Learn --. Evolution of The Software Industry Errors, ...

  5. JavaScript入门-对象

    js对象 本篇主要介绍js里如何创建对象,以及for循环访问对象的成员... 什么是对象? 对象,并不是中文里有男女朋友意思,它是从英文里翻译来的,英文叫[Object],目标,物体,物品的意思. 在 ...

  6. 【MySql】[ERROR] Can't read from messagefile '/usr/share/mysql/english/errmsg.sys'

    [root@zhang bin]# ./mysql_install_db --datadir=/usr/local/mysql/mydata/data/ 2018-08-18 03:09:14 [WA ...

  7. 关于cin, cin.get(), getchar(),getline()的字符问题

    一.getchar()和cin.get() getchar()会将开头的空格或者回车作为输入 1 #include<iostream> 2 using namespace std; 3 i ...

  8. ctfshow—web—web5

    打开靶机,代码审计 附上代码 <?php error_reporting(0); ?> <html lang="zh-CN"> <head> & ...

  9. Vitis下载安装尝试

    Vitis下载安装记录 一.下载安装 文章目录 一.下载安装 提示:以下是本篇文章正文内容,下面案例可供参考 一.下载安装 首先本次下载主要使用的是linux系统,所以我们先看一下Vitis支持的li ...

  10. uni-app开发经验分享十五: uni-app 蓝牙打印功能

    最近在做uni-app项目时,遇到了需要蓝牙打印文件的功能需要制作,在网上找到了一个教程,这里分享给大家. 引入tsc.js 简单得引入到自己所需要得页面中去,本次我们只要到了标签模式,他同时还有账单 ...