P3703 [SDOI2017]树点涂色
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; }