#include <bits/stdc++.h>
using namespace std;
const int M=1e6+5;
int a[M],now[M];
int ans;
vector<int>g[M];
void merge(int x,int y) {
for(auto i:g[x]) {
if(a[i-1]==y)ans--;
if(a[i+1]==y)ans--;
}
for(auto i:g[x])a[i]=y;
for(auto i:g[x])g[y].push_back(i);
g[x].clear();
}
int main() {
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++) {
cin>>a[i];
now[a[i]]=a[i];
if(a[i]!=a[i-1])ans++;
g[a[i]].push_back(i);
}
while(m--) {
int op;cin>>op;
if(op==2)cout<<ans<<endl;
else {
int x,y;
cin>>x>>y;
if(x==y)continue;
if(g[now[x]].size()>g[now[y]].size())swap(now[x],now[y]);
merge(now[x],now[y]);
}
}
}
//暴力是合并
//将小的合并到大的,只会合并log次
标签:int,auto,布丁,梦幻,--,ans,now
From: https://www.cnblogs.com/basicecho/p/16994049.html