2019亚洲区域赛徐州网络赛 B so easy unordered_map

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 1n,两种操作,(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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值