洛谷 P2486 染色
因为多加了一个等号,然后100->0,开个博客纪念一下
题目大意
solution
求的是连续的颜色段的个数.
因为给出的111221的颜色段为三.
我们考虑怎么从下边上传.
如果我们是从111 221 来的,因为中间没有相同的可以接加上左右儿子的颜色段.
但是如果是这样11 12 那么合并的时候需要判断一下左右儿子相连 的那一部分是不是相同.
如果相同那么需要-1,不相同则不用-1.然后我们就能写出push_up 啦.
push_up
obviously
void push_up(int rt) {
tree[rt].lc = tree[lson].lc;
tree[rt].rc = tree[rson].rc;
tree[rt].sum = tree[lson].sum + tree[rson].sum;
if (tree[lson].rc == tree[rson].lc) tree[rt].sum--;
}
因为区间查询的时候也需要上传,那么我们就需要判断所求区间是不是需要合并一下子,
如果需要合并那么还要判断是不是需要-1的那种情况.
那么我们query也有了.
query
obviously
void query(int rt, int l, int r, int L, int R) {
if (L <= l && r <= R) {
ans += tree[rt].sum;
if (l == L) LC = tree[rt].lc;
if (r == R) RC = tree[rt].rc;
return;
}
push_down(rt);
int mid = (l + r) >> 1;
if (R <= mid) query(lson, l, mid, L, R);
else if (L > mid) query(rson, mid + 1, r, L, R);
else {
query(lson, l, mid, L, R);
query(rson, mid + 1, r, L, R);
if (tree[lson].rc == tree[rson].lc) ans--;
}
}
还有树剖的时候向上跳,显然我们需要维护上一个区间的lc和rc来判断当前需不需要减一
int ask(int x, int y) {
int res = 0, x1 = 0, y1 = 0;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y), swap(x1, y1);
ans = 0;
Seg::query(1, 1, n, dfn[top[x]], dfn[x]);
res += ans;
if (RC == x1) res--;
x1 = LC, x = fath[top[x]];
}
if (dfn[x] < dfn[y]) swap(x, y), swap(x1, y1);
ans = 0;
Seg::query(1, 1, n, dfn[y], dfn[x]);
res += ans;
if (LC == y1) res--;
if (RC == x1) res--;
return res;
}
code
/*
Auther:_Destiny
time:2020.5.9
*/
#include <bits/stdc++.h>
#define N 100010
#define M 1010
using namespace std;
struct TTT {
int lc, rc, sum, lazy;
}tree[N << 2];
int n, m, ans, LC, RC;
int dfn[N], dep[N], fath[N], w[N], pre[N], son[N], top[N], siz[N];
int read() {
int s = 0, f = 0; char ch = getchar();
while (!isdigit(ch)) f |= (ch == '-'), ch = getchar();
while (isdigit(ch)) s = s * 10 + (ch ^ 48), ch = getchar();
return f ? -s : s;
}
namespace Seg {
#define lson rt << 1
#define rson rt << 1 | 1
void push_up(int rt) {
tree[rt].lc = tree[lson].lc;
tree[rt].rc = tree[rson].rc;
tree[rt].sum = tree[lson].sum + tree[rson].sum;
if (tree[lson].rc == tree[rson].lc) tree[rt].sum--;
}
void build(int rt, int l, int r) {
if (l == r) {
tree[rt].lc = tree[rt].rc = w[pre[l]];
tree[rt].sum = 1;
return;
}
int mid = (l + r) >> 1;
build(lson, l, mid);
build(rson, mid + 1, r);
push_up(rt);
}
void push_down(int rt) {
if (!tree[rt].lazy) return;
tree[lson].lazy = tree[rson].lazy = tree[rt].lazy;
tree[lson].sum = tree[rson].sum = 1;
tree[lson].lc = tree[lson].rc = tree[rt].lazy;
tree[rson].lc = tree[rson].rc = tree[rt].lazy;
tree[rt].lazy = 0;
}
void update(int rt, int c, int l, int r, int L, int R) {
if (L <= l && r <= R) {
tree[rt].lazy = c;
tree[rt].lc = tree[rt].rc = c;
tree[rt].sum = 1;
return;
}
push_down(rt);
int mid = (l + r) >> 1;
if (L <= mid) update(lson, c, l, mid, L, R);
if (R > mid) update(rson, c, mid + 1, r, L, R);
push_up(rt);
}
void query(int rt, int l, int r, int L, int R) {
if (L <= l && r <= R) {
ans += tree[rt].sum;
if (l == L) LC = tree[rt].lc;
if (r == R) RC = tree[rt].rc;
return;
}
push_down(rt);
int mid = (l + r) >> 1;
if (R <= mid) query(lson, l, mid, L, R);
else if (L > mid) query(rson, mid + 1, r, L, R);
else {
query(lson, l, mid, L, R);
query(rson, mid + 1, r, L, R);
if (tree[lson].rc == tree[rson].lc) ans--;
}
}
}
namespace Cut {
int add_edge, cnt, head[N << 1];
struct node {
int next, to;
}edge[N << 1];
void add(int from, int to) {
edge[++add_edge].next = head[from];
edge[add_edge].to = to;
head[from] = add_edge;
}
void dfs(int x, int fa) {
siz[x] = 1, fath[x] = fa, dep[x] = dep[fa] + 1;
for (int i = head[x]; i; i = edge[i].next) {
int to = edge[i].to;
if (to == fa) continue;
dfs(to, x); siz[x] += siz[to];
if (siz[son[x]] < siz[to]) son[x] = to;
}
}
void dfs2(int x, int tp) {
dfn[x] = ++cnt, pre[cnt] = x, top[x] = tp;
if (son[x]) dfs2(son[x], tp);
for (int i = head[x]; i; i = edge[i].next) {
int to = edge[i].to;
if (to == son[x] || to == fath[x]) continue;
dfs2(to, to);
}
}
void change(int x, int y, int d) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
Seg::update(1, d, 1, n, dfn[top[x]], dfn[x]);
x = fath[top[x]];
}
if (dfn[x] > dfn[y]) swap(x, y);
Seg::update(1, d, 1, n, dfn[x], dfn[y]);
}
int ask(int x, int y) {
int res = 0, x1 = 0, y1 = 0;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y), swap(x1, y1);
ans = 0;
Seg::query(1, 1, n, dfn[top[x]], dfn[x]);
res += ans;
if (RC == x1) res--;
x1 = LC, x = fath[top[x]];
}
if (dfn[x] < dfn[y]) swap(x, y), swap(x1, y1);
ans = 0;
Seg::query(1, 1, n, dfn[y], dfn[x]);
res += ans;
if (LC == y1) res--;
if (RC == x1) res--;
return res;
}
}
int main() {
char s;
n = read(), m = read();
for (int i = 1; i <= n; i++) w[i] = read();
for (int i = 1, x, y; i < n; i++) {
x = read(), y = read();
Cut::add(x, y), Cut::add(y, x);
}
Cut::dfs(1, 1); Cut::dfs2(1, 1);
Seg::build(1, 1, n);
for (int i = 1, x, y, d; i <= m; i++) {
cin >> s;
x = read(), y = read();
if (s == 'C') {
d = read();
Cut::change(x, y, d);
}
if (s == 'Q') printf("%d\n", Cut::ask(x, y));
}
}