题意:给一个长度为N=2e20的序列,初始每个序列的值为-1,这里记为数组a
问Q个问题,每次输入t和x
当t=1时,令h=x,当a[h%N]=-1时,令a[h%N]=x,否则h++,直到赋值了为止
当t=2时,输出a[x%N]。
解法:用并查集将已经赋值过的x,与x+1合并即可,就是令f[x]=x+1
#include<bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef long long ll;
ll f[2000005], a[2000005],N= 1048576;
ll find(ll x) {
return f[x] == x ? x : f[x] = find(f[x]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll t, n,x;
cin >> t;
for (int i = 0; i <= N; i++)f[i] = i, a[i] = -1;
while (t--) {
cin >> n >> x;
if (n == 1) {
ll k = find(x % N);
a[k] = x; f[k] = (k + 1)%N;
// cout << k << ' ' << a[k] << endl;
}
else {
ll k = (x % N);
cout << a[k] << endl;
}
}
return 0;
}