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. Docker学习笔记之创建Ubuntu基础镜像

    在创建基础镜像之前需要安装Bootstrap工具debootstrap,所以执行命令: sudo apt install debootstrap 软件安装完成后就可以使用debootstrap工具下载 ...

  2. selenium自动化 | 借助百度AI开放平台识别验证码登录职教云

    #通过借助百度AI开放平台识别验证码登录职教云 from PIL import Image from aip import AipOcr import unittest # driver.get(zj ...

  3. 十五:SQL注入之oracle,Mangodb注入

    Access,Mysql,mssql,mangoDB,postgresql,sqlite,oracle,sybase JSON类型的数据注入: 键名:键值 {"a":"1 ...

  4. 【Linux】NFS搭建及使用详解

    环境:CentOS release 6.8 server  192.168.25.100 client1 192.168.25.101 client2 192.168.25.102 1.服务端操作 1 ...

  5. 用kubeadm+dashboard部署一个k8s集群

    kubeadm是官方社区推出的一个用于快速部署kubernetes集群的工具. 这个工具能通过两条指令完成一个kubernetes集群的部署: 1. 安装要求 在开始之前,部署Kubernetes集群 ...

  6. GitLab-CI/CD入门实操

    以Spring boot项目为例.传统方式是本地生成jar包,FTP上传服务器,重启服务:如果是内网测试服,也可以在服务器上安装git,在服务器上编译打包.但这都需要人为干预,于是CI/CD就出现了. ...

  7. openshift 3.11安装部署

    openshift 3.11 安装部署 openshift安装部署 1 环境准备(所有节点) openshift 版本 v3.11 1.1 机器环境 ip cpu mem hostname OSsys ...

  8. git 基本命令和操作

    设置全局用户名+密码 $ git config --global user.name 'runoob' $ git config --global user.email test@runoob.com ...

  9. mysqldumpslow基本使用

    参数解释 -s, 是表示按照何种方式排序 c: 访问计数 l: 锁定时间 r: 返回记录 t: 查询时间 al:平均锁定时间 ar:平均返回记录数 at:平均查询时间 -t, 是top n的意思,即为 ...

  10. Linux的环境变量配置在/etc/profile或/etc/profile.d/*.sh文件中的区别是什么?

    @ 目录 login shell non-login shell 它们的区别 Linux的环境变量可在多个文件中配置,如/etc/profile,/etc/profile.d/*.sh,~/.bash ...