1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> #include<algorithm> #include<ctime> #include<iomanip> #include<queue> #include<stack> #include<map> #include<vector> #define gh() getchar() #define re register typedef long long ll; template<class T> inline void read(T &x) { x=0; char ch=gh(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=gh(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=gh(); if(t) x=-x; } template<class T,class ...T1> inline void read(T &x,T1 &...x1) { read(x),read(x1...); } template<class T> inline void write(T x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } template<class T> inline bool checkMax(T &x,T &y) { return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T &y) { return x>y?x=y,1:0; } const int MAXN=1e5+10; const int MAXC=1e6+10; int N,M,Col[MAXN],Now[MAXC],ans; std::vector<int>G[MAXC]; inline void merge(int x,int y) { for(auto i:G[x]) { if(Col[i-1]==y) --ans; if(Col[i+1]==y) --ans; } for(auto i:G[x]) Col[i]=y; for(auto i:G[x]) G[y].push_back(i); G[x].clear(); } int main() { read(N,M); for(int i=1;i<=N;++i) { read(Col[i]); Now[Col[i]]=Col[i]; if(Col[i]!=Col[i-1]) ++ans; G[Col[i]].push_back(i); } while(M--) { int opt,x,y;read(opt); if(opt==2) write(ans),puts(""); else { read(x,y); if(x==y) continue; if(G[Now[x]].size()>G[Now[y]].size()) std::swap(Now[x],Now[y]); merge(Now[x],Now[y]); } } return 0; }
|