BZOJ1483
来自蒟蒻XXJ的做题记录
== set水过
代码在luogu上AC……在BZOJ上面就RE==
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1000005;
int in(){
int a(0);char c=getchar();
while(c<'0'||c>'9') c=getchar();
while(c>='0'&&c<='9') a=(a<<1)+(a<<3)+c-'0',c=getchar();
return a;
}
int n,ans,m;
int col[100010],tank[MAXN];
set<int> t[MAXN];
void merge(int a,int b){
for(set<int>::iterator i=t[a].begin();i!=t[a].end();++i){
if(col[*i-1]==b) --ans;
if(col[*i+1]==b) --ans;
t[b].insert(*i);
}
for(set<int>::iterator i=t[a].begin();i!=t[a].end();++i) col[*i]=b;
t[a].clear();
}
void input(){
n=in();m=in();
for(int i=1;i<=n;i++) col[i]=in();
for(int i=0;i<MAXN;i++) tank[i]=i;
for(int i=1;i<=n;i++){
if(col[i]!=col[i-1]) ++ans;
t[col[i]].insert(i);
}
}
void xxj(){
for(int i=0;i<m;i++){
int typ;typ=in();
if(typ==2) cout<<ans<<endl;
else{
int a,b;
a=in();b=in();
if(a==b) continue;
if(t[tank[a]].size()>t[tank[b]].size()) swap(tank[a],tank[b]);//Well……We are going to merge OK?So it's wu guan jin yao
a=tank[a];b=tank[b];
merge(a,b);//a go out,b stay
}
}
}
int main(){
input();
xxj();
return 0;
}