tju3243 Blocked Road
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的更多相关文章
- 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://) ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- POJ 3204 Ikki's Story I - Road Reconstruction
Ikki's Story I - Road Reconstruction Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 7 ...
- 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:/ ...
- 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 ...
- Linux 日志报错 xxx blocked for more than 120 seconds
监控作业发现一台服务器(Red Hat Enterprise Linux Server release 5.7)从凌晨1:32开始,有一小段时间无法响应,数据库也连接不上,后面又正常了.早上检查了监听 ...
- App Transport Security has blocked a cleartext HTTP (http://)
使用SDWebImage加载“http://”开头的图片报错,错误如下: App Transport Security has blocked a cleartext HTTP (http://) r ...
- 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 ...
- dp or 贪心 --- hdu : Road Trip
Road Trip Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 29 ...
随机推荐
- leetcode1637-两点之间不包括任何点的最宽垂直面积
题目 给你 n 个二维平面上的点 points ,其中 points[i] = [xi, yi] ,请你返回两点之间内部不包含任何点的 最宽垂直面积 的宽度. 垂直面积 的定义是固定宽度,而 y 轴上 ...
- torch.nn 的本质
torch.nn 的本质 PyTorch 提供了各种优雅设计的 modules 和类 torch.nn,torch.optim,Dataset 和 DataLoader 来帮助你创建并训练神经网络.为 ...
- Java基础学习总结笔记
Java基础 Java常用内存区域 栈内存空间:存储引用堆内存空间的地址 堆内存空间:保存每个对象的具体属性内容 全局数据区:保存static类型的属性 全局代码区:保存所有的方法定义 修饰符 权限修 ...
- windows下使用mingw和msvc静态编译Qt5.15.xx
windows下使用mingw和msvc静态编译Qt5.15.xx 下载并安装相关依赖软件 Python version 2.7 https://www.python.org/downloads/ ( ...
- MySQL使用SQL操作数据表的增加、修改和删除
表的修改和删除 修改 -- 修改表名称 -- ALTER TABLE 旧表名 RENAME AS 新表名 ALTER TABLE test RENAME AS test1 -- 增加表字段 -- AL ...
- L(kali)A(apache)M(mysql)P(php)环境+wordpress站点搭建
一:LAMP环境配置 首先LAMP(linux+apache+mysql+php)即为本次搭建网站所需的环境,由于本次使用的debian衍生版kali版本自带lamp,因此只要在服务器上启动相应服务既 ...
- show slave status常用参数备忘
mysql> show slave status\G*************************** 1. row *************************** Slave_IO ...
- load data local带来的安全问题
load data默认读的是服务器上的文件,但是加上local参数后,就可以将本地具有访问权限的文件加载到数据库中,这在带来方便的同时,也带来了以下安全问题, 可以任意加载本地文件到数据库, 在web ...
- 【Nginx】使用keepalive和nginx搭载高可用
首先介绍一下Keepalived,它是一个高性能的服务器高可用或热备解决方案,Keepalived主要来防止服务器单点故障的发生问题,可以通过其与Nginx的配合实现web服务端的高可用. Keepa ...
- 【EXPDP】expdp/impdp数据泵远程导入导出
Oracle在10g的时候有一个很好用的导出工具expdp(数据泵) 但是这个工具好用的同时,有一个局限,就是必须用本地的用户才可以导出数据,也就是说数据泵只能导出本地数据库的数据 但是如果业务需求是 ...