vp了一场gym,我又开心地划水了。

A. Coloring Roads

题意:给定一棵树,树边一开始都是无色的,每次操作可以把一个点到根的路径染成某个颜色,每次询问当前树上出现过某个次数的颜色种数。

题解:看到操作与$Access$类似,考虑使用$lct$解决。由于一条重链的颜色一定是相同的,也就是一棵$splay$中的颜色都是相同的,所以$Access$时每次把整棵$splay$修改掉算一下变化就好了。

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;

int n, m, nq, edg;
int fe[N], cnt[N], ccnt[N];
vector<int> g[N];

void Change(int a, int b, int z) {
  --ccnt[cnt[a]];
  cnt[a] -= z;
  ++ccnt[cnt[a]];
  --ccnt[cnt[b]];
  cnt[b] += z;
  ++ccnt[cnt[b]];
}

namespace T {
  const int N = ::N * 2;
  int fa[N], lc[N], rc[N], si[N], co[N], lzy[N];

  bool Is_root(int x) {
    return lc[fa[x]] != x && rc[fa[x]] != x;
  }

  bool Is_right(int x) {
    return rc[fa[x]] == x;
  }

  void U(int x, int _c) {
    co[x] = lzy[x] = _c;
  }
  
  void Up(int x) {
    si[x] = x > n;
    if (lc[x]) si[x] += si[lc[x]];
    if (rc[x]) si[x] += si[rc[x]];
  }

  void Down(int x) {
    if (lzy[x]) {
      if (lc[x]) U(lc[x], lzy[x]);
      if (rc[x]) U(rc[x], lzy[x]);
      lzy[x] = 0;
    }
  }

  void Roll(int x) {
    if (!Is_root(x)) Roll(fa[x]);
    Down(x);
  }

  void Rotate(int x) {
    int y = fa[x], z = fa[y];
    int d = Is_right(x), s = d? lc[x] : rc[x];
    if (!Is_root(y)) (Is_right(y)? rc[z] : lc[z]) = x;
    fa[x] = z;
    (d? rc[y] : lc[y]) = s;
    if (s) fa[s] = y;
    (d? lc[x] : rc[x]) = y;
    fa[y] = x;
    Up(y), Up(x);
  }

  void Splay(int x) {
    Roll(x);
    for (int y; !Is_root(x); Rotate(x)) {
      if (!Is_root(y = fa[x])) {
        Rotate(Is_right(y) == Is_right(x)? y : x);
      }
    }
  }

  void Access(int x, int _c) {
    for (int y = 0; x; y = x, x = fa[x]) {
      Splay(x);
      Change(co[x], _c, si[x] - si[rc[x]]);
      rc[x] = y;
      Up(x);
      U(x, _c);
    }
  }

}

void Dfs(int x, int ft) {
  for (int v : g[x]) {
    if (v == ft) continue;
    fe[v] = ++edg;
    T::fa[n + edg] = x;
    T::fa[v] = n + edg;
    Dfs(v, x);
  }
}

int main() {
  scanf("%d%d%d", &n, &m, &nq);
  for (int i = 1, x, y; i < n; ++i) {
    scanf("%d%d", &x, &y);
    g[x].push_back(y);
    g[y].push_back(x);
  }

  cnt[0] = n - 1;
  ccnt[n - 1] = 1;
  ccnt[0] += m;
  
  Dfs(1, 0);
  for (int i = 1; i <= n + edg; ++i) {
    T::Up(i);
  }

  for (int u, c, z; nq--; ) {
    scanf("%d%d%d", &u, &c, &z);
    T::Access(u, c);
    printf("%d\n", ccnt[z] - (cnt[0] == z));
  }
  
  return 0;
}
View Code

相关文章: