有\(n\)个点排成一列,每一个点都有一个颜色\(c_i\)。你需要支持下面两个操作:
我们可以考虑对于每一种颜色开一棵线段树维护一下颜色出现的位置,线段树的每一个节点记录当前区间的颜色段数以及两个端点是否有颜色。然后就直接合并,如果左区间的右端点和右区间的左端点都有颜色那么就将颜色段数减一
然后对于\(1?\)操作我们就直接线段树合并,然后\(2?\)操作就直接查询一下对应颜色的根结点就可以了
#include <bits/stdc++.h>
using namespace std;
#define fst first
#define snd second
#define mp make_pair
#define squ(x) ((LL)(x) * (x))
#define debug(...) fprintf(stderr, __VA_ARGS__)
typedef long long LL;
typedef pair<int, int> pii;
template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
inline int read() {
int sum = 0, fg = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == ‘-‘) fg = -1;
for (; isdigit(c); c = getchar()) sum = (sum << 3) + (sum << 1) + (c ^ 0x30);
return fg * sum;
}
const int maxn = 1e5 + 10;
const int maxm = 1e6 + 10;
const int inf = 1e6;
int n, m, rt[maxm];
namespace ST {
int cnt;
struct node {
int ls, rs, v;
bool L, R;
}A[maxn << 6];
#define ls(x) A[x].ls
#define rs(x) A[x].rs
inline void push_up(int rt) {
A[rt].L = A[ls(rt)].L, A[rt].R = A[rs(rt)].R;
A[rt].v = A[ls(rt)].v + A[rs(rt)].v;
if (A[ls(rt)].R && A[rs(rt)].L) --A[rt].v;
}
inline void merge(int &x, int y, int l, int r) {
if (!y) return;
if (!x) { x = y; return; }
int mid = (l + r) >> 1;
merge(ls(x), ls(y), l, mid);
merge(rs(x), rs(y), mid + 1, r);
push_up(x);
}
inline void change(int &rt, int l, int r, int x) {
if (!rt) rt = ++cnt;
if (l == r) { ++A[rt].v, A[rt].L = A[rt].R = 1; return; }
int mid = (l + r) >> 1;
if (x <= mid) change(ls(rt), l, mid, x);
else change(rs(rt), mid + 1, r, x);
push_up(rt);
}
inline int query(int rt) { return A[rt].v; }
}
int main() {
#ifdef xunzhen
freopen("dream.in", "r", stdin);
freopen("dream.out", "w", stdout);
#endif
n = read(), m = read();
for (int i = 1; i <= n; i++) ST::change(rt[read()], 1, n, i);
int ans = 0;
for (int i = 1; i <= inf; i++) ans += ST::query(rt[i]);
for (int i = 1; i <= m; i++) {
int op = read();
if (op == 2) printf("%d\n", ans);
else {
int x = read(), y = read();
if (x == y) continue;
ans -= ST::query(rt[x]) + ST::query(rt[y]);
ST::merge(rt[y], rt[x], 1, n);
ans += ST::query(rt[y]);
rt[x] = 0;
}
}
return 0;
}
原文:https://www.cnblogs.com/xunzhen/p/10364958.html