https://nanti.jisuanke.com/t/41384
There are n points in an array with index from 1 to n, and there are two operations to those points.
1: 1 x marking the point x is not available
2: 2 x query for the index of the first available point after that point (including x itself) .
Output
Output the answer for each query.
样例输入 复制
5 3
1 2
2 2
2 1
样例输出 复制
3
1
题目大意:有
n
n
n个点,分别标号为
1
−
n
1-n
1−n,两种操作,(1)
1
x
1\ \ x
1 x,表示去掉第
x
x
x个点。(2)
2
x
2\ \ x
2 x,表示查询
[
x
,
n
]
[x,n]
[x,n]内第一个存在的点。
思路:当使用
m
a
p
map
map写超时了,后来才想到用
u
n
o
r
d
e
r
e
d
_
m
a
p
unordered\_map
unordered_map这个数据结构。其实就是用这个模拟并查集。
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
unordered_map<int,int> m;
int father(int x)
{
if(!m.count(x))//m中不含x
return x;
return m[x]=father(m[x]);
}
int main()
{
int n,q;
scanf("%d%d",&n,&q);
int op,x,ans;
while(q--)
{
scanf("%d%d",&op,&x);
if(op==1)
m[x]=father(x+1);
else
{
ans=father(x);
if(ans>n)
ans=-1;
printf("%d\n",ans);
}
}
return 0;
}