初始时,一个队列中有n个数,分别为1,2,……n
接下来有m个操作,每次操作删去队列中在数值范围内[l,r]内最小的数
Input
每个测试文件只有一组数据
第一行是两个整数n,m
接下m行,每行有两个整数l,r
其中n<=1e6,m<=1e6
1<l<=r<=n
其中20%的数据
n <=10000
m<=10000
其中80%的数据
n <= 100000
m<=1000000
其中100%的数据
n <=1000000
m<=1000000
Output
对于每次操作输出被删去的数,若不存在数值在[l,r]内的数则输出-1
SampleInput
10 10
2 10
3 5
1 6
1 6
4 9
4 4
3 3
5 5
6 10
4 5
SampleOutput
2
3
1
4
5
-1
-1
-1
6
-1
这题是个并查集。初始化每个点的父节点都是自己,如果选过后,把当前的点的父节点往后移动以为即可,那么每次查询 只要看查询的左端点的父节点在不在此区间内,在就输出,并且父节点后移,不在就输出-1。值得注意的一点是因为父节点后移的问题 初始化时候要初始化到n+1
下面献上low逼代码
`
#include<bits/stdc++.h>
using namespace std;
int father[1000005];
int n,m;
void chu()///初始化
{
for(int i=1; i<=n+1; i++)
father[i]=i;
}
int find(int x)///找父节点
{
int xx=x,t;
while(x!=father[x])
x=father[x];
while(xx!=x) ///压缩路径
{
t=father[xx];
father[xx]=x;
xx=t;
}
return x;
}
int main()
{
cin>>n>>m;
chu();
while(m--)
{
int l,r;
scanf("%d %d",&l,&r);
int t=find(l);
if(t>r)///父节点在区间外
printf("-1\n");
else
{
printf("%d\n",t);
father[t]=t+1;///父节点后移
}
}
}
`