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;
}

时间: 2025-01-01 05:29:37

tju3243 Blocked Road的相关文章

交换机死机,导致ceph ( requests are blocked ) 异常解决方法

问题描述: 万兆交换机死机后,导致在交换机上的ceph 的cluster网络会中断,用户正在对数据块的访问没有完成导致请求被blocked,同时部分pg会处于不同步状态,因此交换机重启后,通过ceph health会发现ceph集群不在OK 状态 health HEALTH_ERR 1 pgs inconsistent; 1 pgs repair; 2 requests are blocked > 32 sec; 1 scrub errorspg 6.89 is active+clean+inc

poj 3204 Ikki&#39;s Story I - Road Reconstruction

Ikki's Story I - Road Reconstruction 题意:有N个顶点和M条边N, M (N ≤ 500, M ≤ 5,000)  ,试图改变图中的一条边使得从0到N-1的流量增加:问这样的边有几条? 思路:刚最大流入门,之后一看就觉得满流的边就是答案..真是太天真了.之后看了题解,发现满流只是前提(即使满流是几次残量的叠加也是一样),还有一个条件是,该路径的最大流量只受该边影响:即可以从S和T遍历到该边的两个端点,这就是为什么之后还要dfs给点涂色的原因:涂色前要对残余网络

hdoj 1596 find the safest road 【dijkstra】

find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9144    Accepted Submission(s): 3225 Problem Description XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1

HDU4081 Qin Shi Huang&#39;s National Road System【Kruska】【次小生成树】

Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3979    Accepted Submission(s): 1372 Problem Description During the Warring States Period of ancient China(4

Restoring Road Network

D - Restoring Road Network Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem Statement In Takahashi Kingdom, which once existed, there are N cities, and some pairs of cities are connected bidirectionally by roads. The following are

AtCoder Regular Contest 083 D:Restoring Road Network

In Takahashi Kingdom, which once existed, there are N cities, and some pairs of cities are connected bidirectionally by roads. The following are known about the road network: People traveled between cities only through roads. It was possible to reach

暑假练习赛 003 B Chris and Road

B - Chris and Road Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice _ uDebug Description Input Output Sample Input Sample Output Hint Description And while Mis

HDU 4081 Qin Shi Huang&#39;s National Road System 最小生成树

分析:http://www.cnblogs.com/wally/archive/2013/02/04/2892194.html 这个题就是多一个限制,就是求包含每条边的最小生成树,这个求出原始最小生成树然后查询就好了 然后预处理那个数组是O(n^2)的,这样总时间复杂度是O(n^2+m) 这是因为这个题n比较小,如果n大的时候,就需要路径查询了,比如LCA 或者树链剖分达到O(mlogn) #include <iostream> #include <algorithm> #incl

Xcode 7提示App Transport Security has blocked a cleartext HTTP (http://) resource load的解决办法

Xcode 7提示App Transport Security has blocked a cleartext HTTP (http://) resource load的解决办法 今天使用Xcode 7打开用Xcode 6开发的网络请求项目,Xcode 7 控制台提示如下: App TransportSecurity has blocked a cleartext HTTP (http://) resource load since it isinsecure. Temporary except