結果

問題 No.2163 LCA Sum Query
ユーザー KudeKude
提出日時 2022-12-14 05:22:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,505 ms / 6,000 ms
コード長 5,827 bytes
コンパイル時間 2,883 ms
コンパイル使用メモリ 237,112 KB
実行使用メモリ 31,104 KB
最終ジャッジ日時 2024-04-25 10:59:37
合計ジャッジ時間 22,895 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 342 ms
6,144 KB
testcase_13 AC 424 ms
25,600 KB
testcase_14 AC 312 ms
27,136 KB
testcase_15 AC 127 ms
5,376 KB
testcase_16 AC 250 ms
25,600 KB
testcase_17 AC 307 ms
14,208 KB
testcase_18 AC 227 ms
6,016 KB
testcase_19 AC 155 ms
5,376 KB
testcase_20 AC 27 ms
14,848 KB
testcase_21 AC 149 ms
14,592 KB
testcase_22 AC 1,008 ms
27,648 KB
testcase_23 AC 684 ms
27,648 KB
testcase_24 AC 969 ms
27,648 KB
testcase_25 AC 644 ms
27,776 KB
testcase_26 AC 1,495 ms
27,520 KB
testcase_27 AC 883 ms
27,648 KB
testcase_28 AC 1,505 ms
27,520 KB
testcase_29 AC 913 ms
27,648 KB
testcase_30 AC 435 ms
30,592 KB
testcase_31 AC 361 ms
31,104 KB
testcase_32 AC 437 ms
29,824 KB
testcase_33 AC 347 ms
30,720 KB
testcase_34 AC 567 ms
27,776 KB
testcase_35 AC 352 ms
27,776 KB
testcase_36 AC 528 ms
27,904 KB
testcase_37 AC 375 ms
27,904 KB
testcase_38 AC 580 ms
28,800 KB
testcase_39 AC 446 ms
28,928 KB
testcase_40 AC 611 ms
28,416 KB
testcase_41 AC 437 ms
28,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic pop
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

struct HLD {
  const vector<vector<int>>& to;
  int root, n;
  vector<int> sz, parent, depth, idx, ridx, head, inv;

  HLD(const vector<vector<int>>& to, int root=0)
      : to(to), root(root), n(to.size()),
        sz(n), parent(n), depth(n), idx(n), ridx(n), head(n), inv(n) {
    init_tree_data(root, -1, 0);
    int nxt = 0;
    assign_idx(root, root, nxt);
  }
  void init_tree_data(int u, int p, int d) {
    parent[u] = p;
    depth[u] = d;
    int s = 1;
    for (int v: to[u]) if (v != p) {
      init_tree_data(v, u, d+1);
      s += sz[v];
    }
    sz[u] = s;
  }
  void assign_idx(int u, int h, int& nxt, int p=-1) {
    head[u] = h;
    idx[u] = nxt;
    inv[nxt] = u;
    nxt++;
    int heaviest = -1;
    int mxweight = 0;
    for (int v: to[u]) if (v != p) {
      if (sz[v] > mxweight) {
        heaviest = v;
        mxweight = sz[v];
      }
    }
    if (heaviest != -1) {
      assign_idx(heaviest, h, nxt, u);
      for (int v: to[u]) if (v != p && v != heaviest) {
        assign_idx(v, v, nxt, u);
      }
    }
    ridx[u] = nxt;
  }

  int lca(int u, int v) {
    while (head[u] != head[v]) {
      if (depth[head[u]] > depth[head[v]]) {
        u = parent[head[u]];
      } else {
        v = parent[head[v]];
      }
    }
    return depth[u] < depth[v] ? u : v;
  }
  // returns reference to tuple of (path fragments from x upto lca (excluding lca), those from y, lca)
  // storage of retval is reused to avoid creating short vectors on each query
  tuple<vector<pair<int, int>>, vector<pair<int, int>>, int> paths_res;
  auto& paths(int x, int y) {
    auto& [x_paths, y_paths, lca] = paths_res;
    x_paths.clear();
    y_paths.clear();
    while (head[x] != head[y]) {
      int hx = head[x], hy = head[y];
      if (depth[hx] > depth[hy]) {
        x_paths.emplace_back(x, hx); x = parent[hx];
      } else {
        y_paths.emplace_back(y, hy); y = parent[hy];
      }
    }
    if (depth[x] > depth[y]) {
      x_paths.emplace_back(x, inv[idx[y] + 1]); x = y;
    } else if (depth[x] < depth[y]) {
      y_paths.emplace_back(y, inv[idx[x] + 1]); y = x;
    }
    lca = x;
    return paths_res;
  }
  int dist(int u, int v) {
    int w = lca(u, v);
    return depth[u] + depth[v] - 2 * depth[w];
  }
  template <class F> int max_ancestor(int v, F f) {
    if (!f(v)) return -1;
    int hv = head[v];
    int p = parent[hv];
    while (p != -1 && f(p)) {
      v = p;
      hv = head[v];
      p = parent[hv];
    }
    int il = idx[hv] - 1, ir = idx[v];
    while (ir - il > 1) {
      int ic = (il + ir) / 2;
      (f(inv[ic]) ? ir : il) = ic;
    }
    return inv[ir];
  }
  int ascend(int v, int k) {
    assert(depth[v] >= k);
    int td = depth[v] - k;
    int hv = head[v];
    while (depth[hv] > td) {
      v = parent[hv];
      hv = head[v];
    }
    int rest = depth[v] - td;
    return inv[idx[v] - rest];
  }
};

struct S {
  ll w, wc, wc2;
};
S op(S x, S y) {
  return S{x.w + y.w, x.wc + y.wc, x.wc2 + y.wc2};
}
S e() {
  return S{0, 0, 0};
}
int composition(int f, int g) {
  return f + g;
}
int id() {
  return 0;
}
S mapping(int f, S x) {
  // sum w(c+f)(c+f-1)/2 = sum wc(c-1)/2 + f sum wc + f(f-1)/2 * sum w
  // sum w(c+f) = sum wc + f sum w
  return S{x.w, x.wc + f * x.w, x.wc2 + f * x.wc + (ll)f * (f-1) / 2 * x.w};
}

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  VVI to(2 * n);
  rep(_, n - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    to[a].emplace_back(b);
    to[b].emplace_back(a);
  }
  rep(i, n) {
    to[i].emplace_back(i + n);
    to[i + n].emplace_back(i);
  }
  n *= 2;
  HLD hld(to);
  vector<S> init_vec(n);
  for(int i = 1; i < n; i++) init_vec[hld.idx[i]].w = i - hld.parent[i];
  lazy_segtree<S, op, e, int, mapping, composition, id> seg_descend(init_vec);
  for(int i = 1; i < n; i++) init_vec[i].w = -init_vec[i].w;
  lazy_segtree<S, op, e, int, mapping, composition, id> seg_ascend(init_vec);
  fenwick_tree<int> ft(n);
  while(q--) {
    int u, r, v;
    cin >> u >> r >> v;
    u--, r--, v--;
    if (r == v) r = v + n / 2;
    int add = ft.sum(hld.idx[u], hld.idx[u] + 1) == 0 ? 1 : -1;
    ft.add(hld.idx[u], add);
    seg_ascend.apply(0, n, add);
    for(auto [x, y]: get<0>(hld.paths(u, 0))) {
      int ix = hld.idx[x], iy = hld.idx[y];
      seg_ascend.apply(iy, ix + 1, -add);
      seg_descend.apply(iy, ix + 1, add);
    }
    ll ans = 0;
    if (hld.lca(r, v) == v) {
      int t = hld.ascend(r, hld.depth[r] - hld.depth[v] - 1);
      int cnt = ft.sum(0, hld.idx[t]) + ft.sum(hld.ridx[t], n);
      ans += (ll)(v + 1) * cnt * (cnt - 1) / 2;
      ans += seg_descend.prod(0, n).wc2 - seg_descend.prod(hld.idx[t], hld.ridx[t]).wc2;
      for(auto [x, y]: get<0>(hld.paths(v, 0))) {
        int ix = hld.idx[x], iy = hld.idx[y];
        ans -= seg_descend.prod(iy, ix + 1).wc2;
        ans += seg_ascend.prod(iy, ix + 1).wc2;
      }
    } else {
      int cnt = ft.sum(hld.idx[v], hld.ridx[v]);
      ans += (ll)(v + 1) * cnt * (cnt - 1) / 2;
      ans += seg_descend.prod(hld.idx[v] + 1, hld.ridx[v]).wc2;
    }
    cout << ans << '\n';
  }
}
0