1. 程式人生 > >P3703 [SDOI2017]樹點塗色

P3703 [SDOI2017]樹點塗色

find == lin 區間 segment void getch update ttr

P3703 [SDOI2017]樹點塗色

鏈接

分析:

  首先對於詢問,感覺是線段樹維護dfs序,每個點記錄到根的顏色個數。第二問差分,第三問區間取max。

  那麽考慮修改,每次將一個點的顏色變成和父節點的顏色一樣的過程中,這個點的子樹內都會-1。

  這個修改的過程我們可以認為是修改邊的過程,將一些邊設為1,一些邊設為0,那麽一次修改對於一個點就是將原來1的邊設為0,現在的邊設為1。

  1和0類似lct中實邊與虛邊,所以可以lct維護當前那些邊是1,那些是0。

  感覺跟個暴力似的,但是lct中access的操作是log的,所以修改的復雜度是log的,線段樹中再有一個log,總復雜度是$O(nlog^2)$

代碼:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
#define Root 1, n, 1
#define lson l, mid, rt << 1
#define
rson mid + 1, r, rt << 1 | 1 using namespace std; typedef long long LL; inline int read() { int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch==-)f=-1; for(;isdigit(ch);ch=getchar())x=x*10+ch-0;return x*f; } const int N = 200005, Log = 18; struct Edge{ int to, nxt; } e[N << 1
]; int head[N], f[N][19], siz[N], pos[N], deth[N], Index, En, n, m; inline void add_edge(int u,int v) { ++En; e[En].to = v, e[En].nxt = head[u]; head[u] = En; ++En; e[En].to = u, e[En].nxt = head[v]; head[v] = En; } void dfs(int u) { pos[u] = ++Index; siz[u] = 1; deth[u] = deth[f[u][0]] + 1; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].to; if (v == f[u][0]) continue; f[v][0] = u; dfs(v); siz[u] += siz[v]; } } int LCA(int u,int v) { if (deth[u] < deth[v]) swap(u, v); int d = deth[u] - deth[v]; for (int j = Log; ~j; --j) if ((d >> j) & 1) u = f[u][j]; if (u == v) return u; for (int j = Log; ~j; --j) if (f[u][j] != f[v][j]) u = f[u][j], v = f[v][j]; return f[u][0]; } struct SegmentTree{ int mx[N << 2], tag[N << 2]; inline void pushup(int rt) { mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]); } inline void pushdown(int rt) { mx[rt << 1] += tag[rt]; mx[rt << 1 | 1] += tag[rt]; tag[rt << 1] += tag[rt]; tag[rt << 1 | 1] += tag[rt]; tag[rt] = 0; } void update(int l,int r,int rt,int L,int R,int v) { if (L <= l && r <= R) { tag[rt] += v; mx[rt] += v; return ; } if (tag[rt]) pushdown(rt); int mid = (l + r) >> 1; if (L <= mid) update(lson, L, R, v); if (R > mid) update(rson, L, R, v); pushup(rt); } int query(int l,int r,int rt,int L,int R) { if (L <= l && r <= R) return mx[rt]; if (tag[rt]) pushdown(rt); int mid = (l + r) >> 1; if (R <= mid) return query(lson, L, R); else if (L > mid) return query(rson, L, R); else return max(query(lson, L, R), query(rson, L, R)); } void pr(int l,int r,int rt) { if (l == r) { cout << mx[rt] << " "; return ; } if (tag[rt]) pushdown(rt); int mid = (l + r) >> 1; pr(lson); pr(rson); } }T; struct LCT{ int fa[N], ch[N][2]; inline bool isroot(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; } inline int son(int x) { return ch[fa[x]][1] == x; } inline void rotate(int x) { int y = fa[x], z = fa[y], c = son(y), b = son(x), a = ch[x][!b]; if (!isroot(y)) ch[z][c] = x; fa[x] = z; ch[x][!b] = y; fa[y] = x; ch[y][b] = a; if (a) fa[a] = y; } void splay(int x) { while (!isroot(x)) { int y = fa[x]; if (isroot(y)) rotate(x); else { if (son(x) == son(y)) rotate(y), rotate(x); else rotate(x), rotate(x); } } } int find(int x) { while (ch[x][0]) x = ch[x][0]; return x; } void access(int x) { for (int last = 0, t; x; last = x, x = fa[x]) { splay(x); if (ch[x][1]) t = find(ch[x][1]), T.update(Root, pos[t], pos[t] + siz[t] - 1, 1); // 這裏找到原樹上的位置 ch[x][1] = last; if (ch[x][1]) t = find(ch[x][1]), T.update(Root, pos[t], pos[t] + siz[t] - 1, -1); } } }lct; int main() { n = read(), m = read(); for (int i = 1; i < n; ++i) { int u = read(), v = read(); add_edge(u, v); } dfs(1); for (int j = 1; j <= Log; ++j) for (int i = 1; i <= n; ++i) f[i][j] = f[f[i][j - 1]][j - 1]; for (int i = 1; i <= n; ++i) { T.update(Root, pos[i], pos[i], deth[i]); lct.fa[i] = f[i][0]; } while (m --) { int opt = read(), x = read(); if (opt == 1) lct.access(x); else if (opt == 2) { int y = read(), z = LCA(x, y); printf("%d\n", T.query(Root, pos[x], pos[x]) + T.query(Root, pos[y], pos[y]) - T.query(Root, pos[z], pos[z]) * 2 + 1); } else { printf("%d\n", T.query(Root, pos[x], pos[x] + siz[x] - 1)); } } return 0; }

P3703 [SDOI2017]樹點塗色