[HNOI2009]梦幻布丁 BZOJ1483 set
题目描述
N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为1,2,2,1的四个布丁一共有3段颜色.
输入输出格式
输入格式:第一行给出N,M表示布丁的个数和好友的操作次数. 第二行N个数A1,A2...An表示第i个布丁的颜色从第三行起有M行,对于每个操作,若第一个数字是1表示要对颜色进行改变,其后的两个整数X,Y表示将所有颜色为X的变为Y,X可能等于Y. 若第一个数字为2表示要进行询问当前有多少段颜色,这时你应该输出一个整数. 0
输出格式:针对第二类操作即询问,依次输出当前有多少段颜色.
输入输出样例
说明
1<=n,m<=100,000; 0<Ai,x,y<1,000,000
用 set 进行暴力处理;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 1000005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
}
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }
/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/
ll qpow(ll a, ll b, ll c) {
ll ans = 1;
a = a % c;
while (b) {
if (b % 2)ans = ans * a%c;
b /= 2; a = a * a%c;
}
return ans;
}
int n, m, ans;
int fath[maxn], v[maxn];
set<int>st[maxn];
void sol(int a, int b) {
set<int>::iterator it;
for (it = st[a].begin(); it != st[a].end(); it++) {
if (v[*it - 1] == b)ans--;
if (v[*it + 1] == b)ans--;
st[b].insert(*it);
}
for (it = st[a].begin(); it != st[a].end(); it++) {
v[*it] = b;// 修改为 b
}
st[a].clear();
}
int main()
{
//ios::sync_with_stdio(0);
rdint(n); rdint(m);
for (int i = 1; i <= n; i++)rdint(v[i]);
for (int i = 1; i <= n; i++) {
fath[v[i]] = v[i];
if (v[i] != v[i - 1])ans++;
st[v[i]].insert(i);
}
for (int i = 1; i <= m; i++) {
int op; rdint(op);
int a, b;
if (op == 2)cout << ans << endl;
else {
rdint(a); rdint(b);
if (a == b)continue;
if (st[fath[a]].size() > st[fath[b]].size())swap(fath[a], fath[b]);
sol(fath[a], fath[b]);
}
}
return 0;
}