結果

問題 No.2676 A Tourist
ユーザー KudeKude
提出日時 2024-03-15 22:51:51
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4,135 ms / 5,000 ms
コード長 8,868 bytes
コンパイル時間 3,810 ms
コンパイル使用メモリ 294,892 KB
実行使用メモリ 77,996 KB
最終ジャッジ日時 2024-09-30 02:23:17
合計ジャッジ時間 49,809 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,424 KB
testcase_01 AC 520 ms
24,960 KB
testcase_02 AC 3,478 ms
62,240 KB
testcase_03 AC 3,208 ms
62,428 KB
testcase_04 AC 3,824 ms
62,244 KB
testcase_05 AC 4,135 ms
62,300 KB
testcase_06 AC 188 ms
25,344 KB
testcase_07 AC 828 ms
63,104 KB
testcase_08 AC 800 ms
63,072 KB
testcase_09 AC 966 ms
63,152 KB
testcase_10 AC 778 ms
63,084 KB
testcase_11 AC 1,218 ms
58,068 KB
testcase_12 AC 317 ms
30,100 KB
testcase_13 AC 839 ms
77,924 KB
testcase_14 AC 813 ms
77,972 KB
testcase_15 AC 1,263 ms
77,924 KB
testcase_16 AC 491 ms
25,008 KB
testcase_17 AC 3,419 ms
62,364 KB
testcase_18 AC 3,322 ms
62,324 KB
testcase_19 AC 3,864 ms
62,332 KB
testcase_20 AC 3,990 ms
62,424 KB
testcase_21 AC 30 ms
7,396 KB
testcase_22 AC 30 ms
7,368 KB
testcase_23 AC 14 ms
7,524 KB
testcase_24 AC 31 ms
7,552 KB
testcase_25 AC 9 ms
7,352 KB
testcase_26 AC 3 ms
7,264 KB
testcase_27 AC 170 ms
25,348 KB
testcase_28 AC 841 ms
63,192 KB
testcase_29 AC 763 ms
63,088 KB
testcase_30 AC 893 ms
63,084 KB
testcase_31 AC 878 ms
63,216 KB
testcase_32 AC 797 ms
77,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
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>;


vector<int> euler_tour_vertices(const vector<vector<int>>& tree, int root=0) {
  const int n = tree.size();
  assert(n > 0);
  vector<int> res;
  res.reserve(2 * n - 1);  // 1 + 2|E|
  vector<pair<int, int>> st;
  st.reserve(n);
  st.emplace_back(root, 0);
  while(!st.empty()) {
    auto& [u, i] = st.back();
    res.emplace_back(u);
    while(true) {
      if (i == (int)tree[u].size()) {
        st.pop_back();
        break;
      }
      int v = tree[u][i++];
      const int sz = st.size();
      if (sz >= 2 && v == st[sz - 2].first) continue;
      st.emplace_back(v, 0);
      break;
    }
  }
  return res;
}

vector<int> vertex_indices(const vector<int>& et) {
  const int n = *max_element(et.begin(), et.end()) + 1;
  vector<int> res(n);
  for (int i = (int)et.size() - 1; i >= 0; i--) res[et[i]] = i;
  return res;
}

template <class S, S (*op)(S, S)>
struct DisjointSparseTable {
  const int n;
  const vector<unsigned char> msb;
  const vector<vector<S>> d;
  DisjointSparseTable(vector<S> a) : n(a.size()), msb(build_msb_table(n)), d(build_table(move(a))) {}

  vector<unsigned char> build_msb_table(int n) {
    if (n <= 1) return {};
    unsigned char k_max = 32 - __builtin_clz(n - 1);
    vector<unsigned char> res(1 << k_max);
    unsigned char* p = res.data();
    for (unsigned char k = 0; k < k_max; k++) {
      memset(p + (1U << k), k, 1U << k);
    }
    return res;
  }
  vector<vector<S>> build_table(vector<S> a) {
    const int n = a.size();
    vector<vector<S>> res(1);
    if (n >= 2) {
      const int i_max = n - 1, k_max = 32 - __builtin_clz(i_max);
      res.resize(k_max);
      for (int k = 1; k < k_max; k++) {
        vector<S> t(i_max >> k & 1 ? n : i_max & ~0U << k);
        for (int c = 1 << k; c < n; c += 1 << (k + 1)) {
          int l = c - (1 << k);
          int r = min(n, c + (1 << k));
          t[c - 1] = a[c - 1];
          for (int i = c - 2; i >= l; i--) t[i] = op(a[i], t[i + 1]);
          t[c] = a[c];
          for (int i = c + 1; i < r; i++) t[i] = op(t[i - 1], a[i]);
        }
        res[k] = move(t);
      }
    }
    res[0] = move(a);
    return res;
  }

  S query(int l, int r) { return query_closed(l, r - 1); }
  S query_closed(int l, int r) {
    assert(0 <= l && l <= r && r < n);
    if (l == r) return d[0][l];
    auto k = msb[l ^ r];
    return op(d[k][l], d[k][r]);
  }
};

int op(int x, int y) { return min(x, y); }

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];
  }
  int move_to(int u, int v, int by) {
    int l = lca(u, v);
    int du = depth[u] - depth[l];
    int dv = depth[v] - depth[l];
    assert(by <= du + dv);
    if (by <= du) return ascend(u, by);
    else return ascend(v, du + dv - by);
  }
};

template<class T, int element_size>
struct array_map {
  int state[element_size];  // -1 if not in set else index in elems
  pair<int, T> elems[element_size];
  int size = 0;
  array_map() { memset(state, -1, sizeof(int) * element_size); }
  bool contains(int x) { return state[x] != -1; }
  pair<int, T>* begin() { return elems; }
  pair<int, T>* end() { return elems + size; }
  void clear() { while(size) state[elems[--size].first] = -1; }
  void erase(int x) {  // not tested
    int idx = state[x];
    if (idx != -1) {
      state[x] = -1;
      size--;
      if (idx != size) {
        int y = elems[size].first;
        state[y] = idx;
        elems[idx] = move(elems[size]);
      }
    }
  }
  T& operator[](int x) {
    if (state[x] == -1) {
      state[x] = size;
      elems[size].first = x;
      elems[size].second = T();
      size++;
    }
    return elems[state[x]].second;
  }
};

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  VL a(n);
  rep(i, n) cin >> a[i];
  VVI to(n);
  rep(_, n - 1) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    to[u].emplace_back(v);
    to[v].emplace_back(u);
  }
  HLD hld(to);
  VI et = euler_tour_vertices(to);
  VI pos = vertex_indices(et);
  DisjointSparseTable<int, op> dst([&]() {
    assert(ssize(et) == 2 * n - 1);
    VI d(2 * n - 1);
    rep(i, 2 * n - 1) d[i] = hld.depth[et[i]];
    return d;
  }());
  auto dist = [&](int u, int v) {
    int iu = pos[u], iv = pos[v];
    if (iu > iv) swap(iu, iv);
    return hld.depth[u] + hld.depth[v] - 2 * dst.query_closed(iu, iv);
  };
  VL a2, s1, s2;
  constexpr int B = 450;
  static array_map<ll, 200010> upd;
  rep(iq, q) {
    if (iq % B == 0) {
      for (auto [k, v] : upd) a[k] += v;
      upd.clear();
      a2.assign(n, 0);
      rep(u, n) for (int v : to[u]) a2[v] += a[u];
      s1 = a;
      s2 = a2;
      rep(i, n) if (i) {
        int u = hld.inv[i];
        s1[u] += s1[hld.parent[u]];
        s2[u] += s2[hld.parent[u]];
      }
    }
    int type;
    cin >> type;
    if (type == 0) {
      int v, x;
      cin >> v >> x;
      v--;
      upd[v] += x;
    } else {
      int u, v;
      cin >> u >> v;
      u--, v--;
      int l = hld.lca(u, v);
      ll ans = s2[u] + s2[v] - 2 * s2[l] + a2[l] + a[u] + a[v];
      ans -= s1[u] + s1[v] - 2 * s1[l] + a[l];
      for (auto [w, val] : upd) {
        int d = 1001001001;
        chmin(d, (dist(w, u) + dist(w, l) - (hld.depth[u] - hld.depth[l])) / 2);
        chmin(d, (dist(w, v) + dist(w, l) - (hld.depth[v] - hld.depth[l])) / 2);
        if (d <= 1) ans += val;
      }
      cout << ans << '\n';
    }
  }
}
0