Problem Description
There are nn 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) .
Input
n q
z1 x1
...
zq xqq is the number of queries, z is the type of operations, and x is the index of operations. 1≤x<n<109,1≤q<106 and z is 1 or 2
Output
Output the answer for each query.
Sample Input
5 3
1 2
2 2
2 1Sample Output
3
1
题意:给出一个数 n,代表存在一个从 1~n 的数列,再给出 m 个操作,每个操作为 1 x 或 2 x,1 x 代表将数列中的 x 数进行标记,2 x 代表查询数列中的 x,若 x 未被标记,就输出 x,若 x 已被标记,就查询其后面的数
思路:一个使用 map 即可以做的并查集裸题,但卡时间,利用 Hash 表或 unordered_map 均可解决
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL quickModPow(LL a,LL b,LL mod){ LL res=1; a=a%mod; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1;} return res; }
LL getInv(LL a,LL mod){ return quickModPow(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-10;
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
unordered_map<int,int> mp;
int f(int y) {
if(y==-1)
return -1;
if(mp[y]==0)
return y;
return mp[y]=f(mp[y]);
}
int main() {
int n,m;
scanf("%d%d",&n,&m);
mp[n]=-1;
for(int i=1; i<=m; i++) {
int x,y;
scanf("%d%d",&x,&y);
if(x==1)
mp[y]=y+1;
else
printf("%d\n",f(y));
}
return 0;
}