AtCoder Beginner Contest 228D - Linear Probing

并查集
\(p[i]\)表示当前要插入的位置第一个可用位置
\(a[i]\)表示当前位置的值

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define LL long long
using namespace std;
const int N = 2e6 + 10, MOD = 1048576;
int Q;
LL p[N], v[N];
int find(int x)
{
	if (p[x] != x)	p[x] = find(p[x]);
	return p[x];
}
int main() {
	IOS;
	for (int i = 0; i < 1048576; i ++ )	p[i] = i, v[i] = -1;
	cin >> Q;
	while (Q -- ) {
		int t;
		LL x;
		cin >> t >> x;
		if (t == 1)	{
			LL k = find(x % MOD);
			v[k] = x;
			p[k] = (k + 1) % MOD;
		}
		else cout << v[x % MOD] << endl;
	}
	return 0;
}
posted @ 2022-02-28 22:58  Angels_of_Death  阅读(28)  评论(0)    收藏  举报