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
| #include<bits/stdc++.h> using namespace std; const int N = 1e5 + 5, M = 1e6 + 5; int n, m; int idx; int h[M], e[M], ne[M]; int color[M], sz[M], p[M]; int ans; template <class T> inline void read(T &res) { res = 0; bool flag = 0; char c = getchar(); while(c < '0' || c > '9') { if(c == '-') flag = 1; c = getchar(); } while(c >= '0' && c <= '9') res = (res << 3) + (res << 1) + c - '0', c = getchar(); if(flag) res = -res; } void add(int x, int y) { idx ++; e[idx] = y, ne[idx] = h[x], h[x] = idx; sz[x] ++; } void merge(int &x, int &y) { if(x == y) return ; if(sz[x] > sz[y]) swap(x, y); for(int i = h[x]; ~i;i = ne[i]) { int j = e[i]; ans -= (color[j - 1] == y) + (color[j + 1] == y); } for(int i = h[x]; ~i;i = ne[i]) { int j = e[i]; color[j] = y; if(ne[i] == -1) { ne[i] = h[y], h[y] = h[x]; break; } } h[x] = -1; sz[y] += sz[x], sz[x] = 0; } int main() { read(n), read(m); memset(h, -1, sizeof(h)); for(int i = 1;i <= n;i ++) { read(color[i]); if(color[i] != color[i - 1]) ans ++; add(color[i], i); } for(int i = 0;i < M;i ++) p[i] = i; while(m --) { int op; read(op); if(op == 2) printf("%d\n", ans); else { int x, y; read(x), read(y); merge(p[x], p[y]); } } return 0; }
|