题目链接:http://codeforces.com/problemset/problem/566/D点击打开链接
Even the most successful company can go through a crisis period when you have to make a hard decision — to restructure, discard and merge departments, fire employees and do other unpleasant stuff. Let's consider the following model of a company.
There are n people working for the Large Software Company. Each person belongs to some department. Initially, each person works on his own project in his own department (thus, each company initially consists of n departments, one person in each).
However, harsh times have come to the company and the management had to hire a crisis manager who would rebuild the working process in order to boost efficiency. Let's use team(person) to represent a team where person person works. A crisis manager can make decisions of two types:
- Merge departments team(x) and team(y) into one large department containing all the employees of team(x) and team(y), where xand y (1 ≤ x, y ≤ n) — are numbers of two of some company employees. If team(x) matches team(y), then nothing happens.
- Merge departments team(x), team(x + 1), ..., team(y), where x and y (1 ≤ x ≤ y ≤ n) — the numbers of some two employees of the company.
At that the crisis manager can sometimes wonder whether employees x and y (1 ≤ x, y ≤ n) work at the same department.
Help the crisis manager and answer all of his queries.
The first line of the input contains two integers n and q (1 ≤ n ≤ 200 000, 1 ≤ q ≤ 500 000) — the number of the employees of the company and the number of queries the crisis manager has.
Next q lines contain the queries of the crisis manager. Each query looks like type x y, where . If type = 1 or type = 2, then the query represents the decision of a crisis manager about merging departments of the first and second types respectively. If type = 3, then your task is to determine whether employees x and y work at the same department. Note that x can be equal to y in the query of any type.
For each question of type 3 print "YES" or "NO" (without the quotes), depending on whether the corresponding people work in the same department.
8 6 3 2 5 1 2 5 3 2 5 2 4 7 2 1 2 3 1 7
NO YES YES
开一个next数组表示该点所在集合的下一个几个 因为是连续区间 所以能够跳过集合内部部分到达下一个集合 很经典的题目
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int pre[222222];
int nnext[222222];
int findx(int x)
{
int r=x;
while(pre[r]!=r)
{
r=pre[r];
}
int i=x;int j;
while(pre[i]!=r)
{
j=pre[i];
pre[i]=r;
i=j;
}
return r;
}
void join(int x,int y)
{
int p1=findx(x);
int p2=findx(y);
if(p1!=p2);
{
pre[p2]=p1;
}
}
void linejoin(int x,int y)
{
int fi=nnext[y];
int ii=x;int j;
while(ii<=y)
{
j=nnext[ii];
if(j<fi)
join(ii,j);
nnext[ii]=fi;
ii=j;
}
}
int judgepre(int x,int y)
{
int p1=findx(x);
int p2=findx(y);
if(p1==p2)
return 1;
return 0;
}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
pre[i]=i;
nnext[i]=i+1;
}
for(int i=0;i<k;i++)
{
int op;int x;int y;
scanf("%d%d%d",&op,&x,&y);
if(op==1)
{
join(x,y);
}
else if(op==2)
{
linejoin(x,y);
}
else
{
if(judgepre(x,y))
printf("YES\n");
else
printf("NO\n");
}
}
}