so easy 2019徐州网络赛

题目链接:https://nanti.jisuanke.com/t/41384

There are nn points in an array with index from 11 to nn, and there are two operations to those points.

1: 1 x1 x marking the point xx is not available

2: 2 x2 x query for the index of the first available point after that point (including xx itself) .

Input

 q is the number of queries, z is the type of operations, and x is the index of operations. 1≤x<n<109,1≤x<n<109 q<106and z is 1 or 2.

Output

Output the answer for each query.

样例输入

5 3
1 2
2 2
2 1

样例输出

3
1

因为数字比较多,我们不能开数组并查集,但是询问次数不多,所以询问涉及的数字不多,我们哈希即可。

#pragma GCC optimize(2)
#include<bits/stdc++.h>
//#define int long long
using namespace std;
unordered_map<int,int> mp;
int n,q;
int find(int x){
	if(!mp[x])	return x;
	if(mp[x]==-1)	return -1;
	else return mp[x]=find(mp[x]);
}
signed main(){
	scanf("%d %d",&n,&q);	mp[n]=-1;
	while(q--){
		int z,x;	scanf("%d %d",&z,&x);
		if(z==1)	mp[x]=find(x+1);
		else	printf("%d
",find(x));
	}
	return 0;
}
原文地址:https://www.cnblogs.com/shmilky/p/14089030.html