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 x and 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.
Input
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 xand y work at the same department. Note that x can be equal to y in the query of any type.
Output
For each question of type 3 print "YES" or "NO" (without the quotes), depending on whether the corresponding people work in the same department.
Examples
Input
8 6 3 2 5 1 2 5 3 2 5 2 4 7 2 1 2 3 1 7
Output
NO YES YES
题目大意:刚开始每人一个组,然后有两种操作,第一种操作是吧x和y所在的组合为一个,第二种操作是把从x到y的所有人所在的组合为一个
解题思路:并查集,但是要把第二种操作优化一下,因为一个一个找父节点,再合起来,这个操作会被重复操作,所以,要把他合为一个包,下次再次访问到这里时,就将整个包直接连到父节点
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct node
{
int l,r,data;
}b[20011900];
void build(int l,int r,int i)
{
b[i].l=l;
b[i].r=r;
if(l==r){
scanf("%d",&b[i].data);
return ;
}
int mid=((l+r)>>1);
build(l,mid,i<<1);
build(mid+1,r,(i<<1)|1);
b[i].data=max(b[i<<1].data,b[(i<<1)|1].data);
}
int ask(int i,int l,int r)
{
if(l==b[i].l&&r==b[i].r)
return b[i].data;
int mid=((b[i].l+b[i].r)>>1);
int ans=0;
if(l>mid) ans=ask((i<<1)|1,l,r);
else if(r<=mid) ans=ask(i<<1,l,r);
else ans=max(ask(i<<1,l,mid),ask((i<<1)|1,mid+1,r));
return ans;
}
void up(int i,int t,int s)
{
b[i].data=max(b[i].data,s);
if(b[i].l==b[i].r) return ;
int mid=((b[i].l+b[i].r)>>1);
if(t<=mid) up(i<<1,t,s);
else up((i<<1)|1,t,s);
}
int main()
{
int n,m,i,x,y;
char DO;
while(~scanf("%d %d",&n,&m))
{
build(1,n,1);
getchar();
for(i=1;i<=m;i++)
{
scanf("%c %d %d",&DO,&x,&y);
getchar();
if(DO=='Q')
{
printf("%d\n",ask(1,x,y));
}
else
{
up(1,x,y);
}
}
}
return 0;
}