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 ...
随机推荐
- LeetCode1337矩阵中最弱的K行
题目 给你一个大小为 m * n 的矩阵 mat,矩阵由若干军人和平民组成,分别用 1 和 0 表示. 请你返回矩阵中战斗力最弱的 k 行的索引,按从最弱到最强排序. 如果第 i 行的军人数量少于第 ...
- web网上书店总结(jsp+servlet)
web网上书店总结 前端的首页.效果如下: 基本上按照页面有的内容对其实现功能.按照用户划分功能模块,有后台管理员和普通用户,登录的时候会判断账户的类别,例如0权限代表普通用户登录,1权限代表管理员登 ...
- (十四)json、pickle与shelve模块
任何语言,都有自己的数据类型,那么不同的语言怎么找到一个通用的标准? 比如,后端用Python写的,前端是js,那么后端如果传一个dic字典给前端,前端肯定不认. 所以就有了序列化这个概念. 什么是序 ...
- Ubuntu下修改缺省dash shell为bash shell
Debian和Ubuntu下缺省使用的是shell是dash,而不是bash.从/bin/sh软连接的指向可以看出这点. 这是一个不同于bash的shell,它主要是为了执行脚本而出现,而不是交互,它 ...
- 手动验证MySQL Innodb RR级别加锁 需要注意的几个点
记录几个坑 优化器在表行数比较少的时候 会使用全表扫描,会造成全表所有的行加锁,所以需要使用force index 强制使用索引 来实现gap-lock(间隙锁)的应用 next-lock 加锁 会锁 ...
- 06--Docker自定义镜像Tomcat9
1. 创建目录 /zhengcj/mydockerfile/tomcat9 2.将jdk和tomcat的安装包拷贝到tomcat9下 3.在tomcat9目录下创建Dockerfile文件,并写以下命 ...
- 编年史:OI算法总结
目录(按字典序) A --A* D --DFS找环 J --基环树 S --数位动规 --树形动规 T --Tarjan(e-DCC) --Tarjan(LCA) --Tarjan(SCC) --Ta ...
- python爬虫如何提高效率
开启线程池: 线程池 asyncio 特殊的函数 协程 任务对象 任务对象绑定 事件循环 from multiprocessing.dummy import Pool map(func,alist): ...
- 2、fork函数与进程ID
1. fork函数 fork函数用于克隆一份当前的进程资源,调用fork函数之后,进程一分为二,并且两个进程的资源是一样的(只是资源内容完全一样,并不是同一份资源).fork函数的函数原型为:pid_ ...
- 转 12 jmeter性能测试实战--web程序
12 jmeter性能测试实战--web程序 项目背景 项目:XX网站环境:Windows需求:并发登录的性能测试场景:1s增加2个线程,运行2000次(线程数20,Ramp-Up seconds ...