結果

問題 No.2163 LCA Sum Query
ユーザー 👑 hos.lyrichos.lyric
提出日時 2022-12-16 02:55:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 504 ms / 6,000 ms
コード長 10,059 bytes
コンパイル時間 1,803 ms
コンパイル使用メモリ 113,820 KB
実行使用メモリ 19,396 KB
最終ジャッジ日時 2023-08-09 12:04:10
合計ジャッジ時間 16,966 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 184 ms
4,680 KB
testcase_13 AC 242 ms
14,816 KB
testcase_14 AC 179 ms
15,368 KB
testcase_15 AC 82 ms
4,376 KB
testcase_16 AC 146 ms
14,804 KB
testcase_17 AC 153 ms
9,104 KB
testcase_18 AC 114 ms
4,416 KB
testcase_19 AC 88 ms
4,376 KB
testcase_20 AC 16 ms
9,160 KB
testcase_21 AC 77 ms
8,904 KB
testcase_22 AC 403 ms
15,548 KB
testcase_23 AC 329 ms
15,692 KB
testcase_24 AC 385 ms
15,520 KB
testcase_25 AC 320 ms
15,632 KB
testcase_26 AC 482 ms
15,596 KB
testcase_27 AC 398 ms
15,636 KB
testcase_28 AC 504 ms
15,584 KB
testcase_29 AC 390 ms
15,624 KB
testcase_30 AC 234 ms
19,004 KB
testcase_31 AC 266 ms
19,396 KB
testcase_32 AC 243 ms
17,828 KB
testcase_33 AC 258 ms
18,756 KB
testcase_34 AC 193 ms
15,920 KB
testcase_35 AC 174 ms
15,716 KB
testcase_36 AC 184 ms
15,792 KB
testcase_37 AC 160 ms
15,792 KB
testcase_38 AC 268 ms
16,888 KB
testcase_39 AC 255 ms
16,908 KB
testcase_40 AC 270 ms
16,676 KB
testcase_41 AC 258 ms
16,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }


// T: monoid representing information of an interval.
//   T()  should return the identity.
//   T(S s)  should represent a single element of the array.
//   T::push(T &l, T &r)  should push the lazy update.
//   T::merge(const T &l, const T &r)  should merge two intervals.
template <class T> struct SegmentTreeRange {
  int logN, n;
  vector<T> ts;
  SegmentTreeRange() {}
  explicit SegmentTreeRange(int n_) {
    for (logN = 0, n = 1; n < n_; ++logN, n <<= 1) {}
    ts.resize(n << 1);
  }
  template <class S> explicit SegmentTreeRange(const vector<S> &ss) {
    const int n_ = ss.size();
    for (logN = 0, n = 1; n < n_; ++logN, n <<= 1) {}
    ts.resize(n << 1);
    for (int i = 0; i < n_; ++i) at(i) = T(ss[i]);
    build();
  }
  T &at(int i) {
    return ts[n + i];
  }
  void build() {
    for (int u = n; --u; ) merge(u);
  }

  inline void push(int u) {
    ts[u].push(ts[u << 1], ts[u << 1 | 1]);
  }
  inline void merge(int u) {
    ts[u].merge(ts[u << 1], ts[u << 1 | 1]);
  }

  // Applies T::f(args...) to [a, b).
  template <class F, class... Args>
  void ch(int a, int b, F f, Args &&... args) {
    assert(0 <= a); assert(a <= b); assert(b <= n);
    if (a == b) return;
    a += n; b += n;
    for (int h = logN; h; --h) {
      const int aa = a >> h, bb = b >> h;
      if (aa == bb) {
        if ((aa << h) != a || (bb << h) != b) push(aa);
      } else {
        if ((aa << h) != a) push(aa);
        if ((bb << h) != b) push(bb);
      }
    }
    for (int aa = a, bb = b; aa < bb; aa >>= 1, bb >>= 1) {
      if (aa & 1) (ts[aa++].*f)(args...);
      if (bb & 1) (ts[--bb].*f)(args...);
    }
    for (int h = 1; h <= logN; ++h) {
      const int aa = a >> h, bb = b >> h;
      if (aa == bb) {
        if ((aa << h) != a || (bb << h) != b) merge(aa);
      } else {
        if ((aa << h) != a) merge(aa);
        if ((bb << h) != b) merge(bb);
      }
    }
  }

  // Calculates the product for [a, b).
  T get(int a, int b) {
    assert(0 <= a); assert(a <= b); assert(b <= n);
    if (a == b) return T();
    a += n; b += n;
    for (int h = logN; h; --h) {
      const int aa = a >> h, bb = b >> h;
      if (aa == bb) {
        if ((aa << h) != a || (bb << h) != b) push(aa);
      } else {
        if ((aa << h) != a) push(aa);
        if ((bb << h) != b) push(bb);
      }
    }
    T prodL, prodR, t;
    for (int aa = a, bb = b; aa < bb; aa >>= 1, bb >>= 1) {
      if (aa & 1) { t.merge(prodL, ts[aa++]); prodL = t; }
      if (bb & 1) { t.merge(ts[--bb], prodR); prodR = t; }
    }
    t.merge(prodL, prodR);
    return t;
  }

  // Calculates T::f(args...) of a monoid type for [a, b).
  //   op(-, -)  should calculate the product.
  //   e()  should return the identity.
  template <class Op, class E, class F, class... Args>
#if __cplusplus >= 201402L
  auto
#else
  decltype((std::declval<T>().*F())())
#endif
  get(int a, int b, Op op, E e, F f, Args &&... args) {
    assert(0 <= a); assert(a <= b); assert(b <= n);
    if (a == b) return e();
    a += n; b += n;
    for (int h = logN; h; --h) {
      const int aa = a >> h, bb = b >> h;
      if (aa == bb) {
        if ((aa << h) != a || (bb << h) != b) push(aa);
      } else {
        if ((aa << h) != a) push(aa);
        if ((bb << h) != b) push(bb);
      }
    }
    auto prodL = e(), prodR = e();
    for (int aa = a, bb = b; aa < bb; aa >>= 1, bb >>= 1) {
      if (aa & 1) prodL = op(prodL, (ts[aa++].*f)(args...));
      if (bb & 1) prodR = op((ts[--bb].*f)(args...), prodR);
    }
    return op(prodL, prodR);
  }

  // Find min b s.t. T::f(args...) returns true,
  // when called for the partition of [a, b) from left to right.
  //   Returns n + 1 if there is no such b.
  template <class F, class... Args>
  int findRight(int a, F f, Args &&... args) {
    assert(0 <= a); assert(a <= n);
    if ((T().*f)(args...)) return a;
    if (a == n) return n + 1;
    a += n;
    for (int h = logN; h; --h) push(a >> h);
    for (; ; a >>= 1) if (a & 1) {
      if ((ts[a].*f)(args...)) {
        for (; a < n; ) {
          push(a);
          if (!(ts[a <<= 1].*f)(args...)) ++a;
        }
        return a - n + 1;
      }
      ++a;
      if (!(a & (a - 1))) return n + 1;
    }
  }

  // Find max a s.t. T::f(args...) returns true,
  // when called for the partition of [a, b) from right to left.
  //   Returns -1 if there is no such a.
  template <class F, class... Args>
  int findLeft(int b, F f, Args &&... args) {
    assert(0 <= b); assert(b <= n);
    if ((T().*f)(args...)) return b;
    if (b == 0) return -1;
    b += n;
    for (int h = logN; h; --h) push((b - 1) >> h);
    for (; ; b >>= 1) if ((b & 1) || b == 2) {
      if ((ts[b - 1].*f)(args...)) {
        for (; b <= n; ) {
          push(b - 1);
          if (!(ts[(b <<= 1) - 1].*f)(args...)) --b;
        }
        return b - n - 1;
      }
      --b;
      if (!(b & (b - 1))) return -1;
    }
  }
};

////////////////////////////////////////////////////////////////////////////////


struct Node {
  Int sum0, sum1, sum2;
  Int lz;
  Node() : sum0(0), sum1(0), sum2(0), lz(0) {}
  void push(Node &l, Node &r) {
    if (lz) {
      l.add(lz);
      r.add(lz);
      lz = 0;
    }
  }
  void merge(const Node &l, const Node &r) {
    sum0 = l.sum0 + r.sum0;
    sum1 = l.sum1 + r.sum1;
    sum2 = l.sum2 + r.sum2;
  }
  void add(Int val) {
    // binom(x + val, 2) - binom(x, 2)
    sum2 += sum1 * val + sum0 * val * (val - 1) / 2;
    sum1 += sum0 * val;
    lz += val;
  }
};


int N, Q;
vector<int> A, B;
vector<vector<int>> graph;

vector<int> par, sz;
int zeit;
vector<int> dis, fin;
vector<int> head;
void dfsSz(int u) {
  sz[u] = 1;
  for (const int v : graph[u]) {
    graph[v].erase(find(graph[v].begin(), graph[v].end(), u));
    par[v] = u;
    dfsSz(v);
    sz[u] += sz[v];
  }
}
void dfsHLD(int u) {
  dis[u] = zeit++;
  const int deg = graph[u].size();
  if (deg > 0) {
    int vm = graph[u][0];
    int jm = 0;
    for (int j = 1; j < deg; ++j) {
      const int v = graph[u][j];
      if (sz[vm] < sz[v]) {
        vm = v;
        jm = j;
      }
    }
    swap(graph[u][0], graph[u][jm]);
    head[vm] = head[u];
    dfsHLD(vm);
    for (int j = 1; j < deg; ++j) {
      const int v = graph[u][j];
      head[v] = v;
      dfsHLD(v);
    }
  }
  fin[u] = zeit;
}
void runHLD(int rt) {
  par.assign(N, -1);
  sz.resize(N);
  zeit = 0;
  dis.resize(N);
  fin.resize(N);
  head.resize(N);
  dfsSz(rt);
  head[rt] = rt;
  dfsHLD(rt);
}


constexpr int R0 = 0;
Int tot;
vector<int> on;
SegmentTreeRange<Node> seg, segOne;

void init() {
  tot = 0;
  on.assign(N, 0);
  seg = SegmentTreeRange<Node>(N);
  segOne = SegmentTreeRange<Node>(N);
  for (int u = 0; u < N; ++u) {
    seg.at(dis[u]).sum0 = u - par[u];
    segOne.at(dis[u]).sum0 = 1;
  }
  seg.build();
  segOne.build();
}
void add(int u, int val) {
// cerr<<"add "<<u<<" "<<val<<endl;
  tot += val;
  for (int v = u; ~v; v = par[head[v]]) {
    seg.ch(dis[head[v]], dis[v] + 1, &Node::add, val);
    segOne.ch(dis[head[v]], dis[v] + 1, &Node::add, val);
  }
}
Int solve(int r, int u) {
// cerr<<"solve "<<r<<" "<<u<<"; on = "<<on<<endl;
  Int ans = 0;
  if (r == R0) {
    // \sum[v \in T(u)] (v - par(v)) binom(|S(v)|, 2)
    ans += seg.get(dis[u], dis[u] + 1).sum2 / (u - par[u]) * (u - (-1));
    ans += seg.get(dis[u] + 1, fin[u]).sum2;
  } else if (r == u) {
    // \sum[v \in path[R0, r]] (v - par[v]) |S(v)| (|S| - |S(v)|)
    ans = solve(R0, R0);
    for (int v = r; ~v; v = par[head[v]]) {
      const Node res = seg.get(dis[head[v]], dis[v] + 1);
      ans += tot * res.sum1;
      ans -= (res.sum1 + 2 * res.sum2);
    }
  } else if (dis[u] <= dis[r] && dis[r] < fin[u]) {
    const int deg = graph[u].size();
    int lo = 0, hi = deg;
    for (; lo + 1 < hi; ) {
      const int mid = (lo + hi) / 2;
      const int v = graph[u][mid];
      ((dis[v] <= dis[r]) ? lo : hi) = mid;
    }
    r = graph[u][lo];
    ans = solve(r, r);
    ans -= solve(R0, r);
    {
      const Node res = segOne.get(dis[r], dis[r] + 1);
      ans -= (r + 1) * res.sum1 * (tot - res.sum1);
    }
  } else {
    ans = solve(R0, u);
  }
  return ans;
}

int main() {
  for (; ~scanf("%d%d", &N, &Q); ) {
    A.resize(N - 1);
    B.resize(N - 1);
    for (int i = 0; i < N - 1; ++i) {
      scanf("%d%d", &A[i], &B[i]);
      --A[i];
      --B[i];
    }
    
    graph.assign(N, {});
    for (int i = 0; i < N - 1; ++i) {
      graph[A[i]].push_back(B[i]);
      graph[B[i]].push_back(A[i]);
    }
    runHLD(R0);
// cerr<<"graph = "<<graph<<endl;
// cerr<<"head = "<<head<<endl;
    
    init();
    for (; Q--; ) {
      int U, R, V;
      scanf("%d%d%d", &U, &R, &V);
      --U;
      --R;
      --V;
      if (on[U]) {
        on[U] = 0;
        add(U, -1);
      } else {
        on[U] = 1;
        add(U, +1);
      }
      const Int ans = solve(R, V);
      printf("%lld\n", ans);
    }
#ifdef LOCAL
cerr<<"========"<<endl;
#endif
  }
  return 0;
}
0