首页 > 代码库 > 2014080503梦幻布丁(C++)
2014080503梦幻布丁(C++)
#include<cstdio>
#define N 100010
#define M 1000010
int c[N],sum[M];
int next[N],last[M];
int head[M];
int f[M];
int n,m,ans;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
return x*f;
}
inline void move(int from,int to)
{
for (int i=head[from];i;i=next[i])
{
if (c[i+1]==to)ans--;
if (c[i-1]==to)ans--;
}
for (int i=head[from];i;i=next[i])c[i]=to;
next[last[from]]=head[to];head[to]=head[from];
sum[to]+=sum[from];sum[from]=head[from]=last[from]=0;
}
int main()
{
n=read();m=read();
for(int i=1;i<=n;i++)
{
c[i]=read();f[c[i]]=c[i];
if (c[i]!=c[i-1])ans++;
if (!head[c[i]])last[c[i]]=i;
next[i]=head[c[i]];head[c[i]]=i;sum[c[i]]++;
}
int opr,x,y,a,b;
for (int i=1;i<=m;i++)
{
opr=read();
if (opr==2){printf("%d\n",ans);continue;}
x=read();y=read();
if (x==y)continue;
if (sum[f[x]]>sum[f[y]])
{
int t=f[x];
f[x]=f[y];
f[y]=t;
}
a=f[x];b=f[y];
if (sum[a]==0)continue;
move(a,b);
}
}
2014080503梦幻布丁(C++)