计蒜客41384 so easy
https://nanti.jisuanke.com/t/41384
并查集
数据范围极大,用链表直接爆炸
我们考虑到询问次数无法达到\(10^9\),有许多数都是无用的
并查集同样能够模拟链表,一个节点链向他的祖先(在本题中,祖先为下一个索引)
我们用一个\(map\)来作为\(father\)数组,动态开点
\(C++ Code:\)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
int n,m,x,y;
map<int,int>f;
int getf(int x)
{
return (f[x]==x)?x:(f[x]=getf(f[x]));
}
void New(int x)
{
f[x]=x;
}
int main()
{
scanf("%d%d",&n,&m);
while (m --> 0)
{
scanf("%d%d",&x,&y);
if (!f[y])
New(y);
switch (x)
{
case 1:
f[y]=y+1;
if (!f[y+1])
New(y+1);
break;
case 2:
printf("%d\n",getf(y));
break;
}
}
}

浙公网安备 33010602011771号