結果

問題 No.1030 だんしんぐぱーりない
ユーザー 👑 emthrmemthrm
提出日時 2020-04-17 22:58:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 253 ms / 2,000 ms
コード長 4,745 bytes
コンパイル時間 2,947 ms
コンパイル使用メモリ 216,800 KB
実行使用メモリ 21,792 KB
最終ジャッジ日時 2024-04-14 15:18:32
合計ジャッジ時間 10,142 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 175 ms
18,984 KB
testcase_06 AC 136 ms
14,976 KB
testcase_07 AC 87 ms
8,064 KB
testcase_08 AC 93 ms
10,112 KB
testcase_09 AC 142 ms
19,364 KB
testcase_10 AC 58 ms
6,944 KB
testcase_11 AC 150 ms
11,392 KB
testcase_12 AC 147 ms
17,416 KB
testcase_13 AC 130 ms
15,856 KB
testcase_14 AC 176 ms
15,404 KB
testcase_15 AC 71 ms
6,944 KB
testcase_16 AC 150 ms
13,440 KB
testcase_17 AC 148 ms
19,660 KB
testcase_18 AC 195 ms
16,316 KB
testcase_19 AC 96 ms
7,424 KB
testcase_20 AC 118 ms
11,512 KB
testcase_21 AC 111 ms
14,392 KB
testcase_22 AC 116 ms
11,988 KB
testcase_23 AC 149 ms
11,000 KB
testcase_24 AC 103 ms
6,944 KB
testcase_25 AC 133 ms
10,880 KB
testcase_26 AC 74 ms
6,948 KB
testcase_27 AC 86 ms
6,940 KB
testcase_28 AC 155 ms
13,872 KB
testcase_29 AC 113 ms
17,108 KB
testcase_30 AC 105 ms
12,204 KB
testcase_31 AC 115 ms
11,136 KB
testcase_32 AC 145 ms
15,112 KB
testcase_33 AC 151 ms
17,704 KB
testcase_34 AC 59 ms
6,940 KB
testcase_35 AC 253 ms
21,672 KB
testcase_36 AC 244 ms
21,668 KB
testcase_37 AC 243 ms
21,792 KB
testcase_38 AC 248 ms
21,676 KB
testcase_39 AC 244 ms
21,536 KB
testcase_40 AC 2 ms
6,944 KB
testcase_41 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

using CostType = int;
struct Edge {
  int src, dst; CostType cost;
  Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
  inline bool operator<(const Edge &x) const {
    return cost != x.cost ? cost < x.cost : dst != x.dst ? dst < x.dst : src < x.src;
  }
  inline bool operator<=(const Edge &x) const { return !(x < *this); }
  inline bool operator>(const Edge &x) const { return x < *this; }
  inline bool operator>=(const Edge &x) const { return !(*this < x); }
};

struct LCADoubling {
  vector<int> depth;
  vector<CostType> dist;

  LCADoubling(const vector<vector<Edge>> &graph) : graph(graph) {
    n = graph.size();
    depth.resize(n);
    dist.resize(n);
    while ((1 << table_h) <= n) ++table_h;
    parent.resize(table_h, vector<int>(n));
  }

  void build(int root = 0) {
    dfs(-1, root, 0, 0);
    for (int i = 0; i + 1 < table_h; ++i) REP(ver, n) {
      parent[i + 1][ver] = (parent[i][ver] == -1 ? -1 : parent[i][parent[i][ver]]);
    }
  }

  int query(int u, int v) {
    if (depth[u] > depth[v]) swap(u, v);
    REP(i, table_h) {
      if ((depth[v] - depth[u]) >> i & 1) v = parent[i][v];
    }
    if (u == v) return u;
    for (int i = table_h - 1; i >= 0; --i) {
      if (parent[i][u] != parent[i][v]) {
        u = parent[i][u];
        v = parent[i][v];
      }
    }
    return parent[0][u];
  }

  CostType distance(int u, int v) { return dist[u] + dist[v] - dist[query(u, v)] * 2; }

private:
  int n, table_h = 1;
  vector<vector<Edge>> graph;
  vector<vector<int>> parent;

  void dfs(int par, int ver, int now_depth, CostType now_dist) {
    depth[ver] = now_depth;
    dist[ver] = now_dist;
    parent[0][ver] = par;
    for (const Edge &e : graph[ver]) {
      if (e.dst != par) dfs(ver, e.dst, now_depth + 1, now_dist + e.cost);
    }
  }
};

template <typename Monoid>
struct SegmentTree {
  using Fn = function<Monoid(Monoid, Monoid)>;

  SegmentTree(int sz, Fn fn, const Monoid UNITY) : fn(fn), UNITY(UNITY) {
    init(sz);
    dat.assign(n << 1, UNITY);
  }

  SegmentTree(const vector<Monoid> &a, Fn fn, const Monoid UNITY) : fn(fn), UNITY(UNITY) {
    int a_sz = a.size();
    init(a_sz);
    dat.resize(n << 1);
    REP(i, a_sz) dat[i + n] = a[i];
    for (int i = n - 1; i > 0; --i) dat[i] = fn(dat[i << 1], dat[(i << 1) + 1]);
  }

  void update(int node, Monoid val) {
    node += n;
    dat[node] = val;
    while (node >>= 1) dat[node] = fn(dat[node << 1], dat[(node << 1) + 1]);
  }

  Monoid query(int left, int right) {
    Monoid l_res = UNITY, r_res = UNITY;
    for (left += n, right += n; left < right; left >>= 1, right >>= 1) {
      if (left & 1) l_res = fn(l_res, dat[left++]);
      if (right & 1) r_res = fn(dat[--right], r_res);
    }
    return fn(l_res, r_res);
  }

  Monoid operator[](const int idx) const { return dat[idx + n]; }

private:
  int n = 1;
  Fn fn;
  const Monoid UNITY;
  vector<Monoid> dat;

  void init(int sz) { while (n < sz) n <<= 1; }
};

int main() {
  int n, k, q; cin >> n >> k >> q;
  vector<int> c(n), a(k);
  REP(i, n) cin >> c[i];
  REP(i, k) cin >> a[i], --a[i];
  // REP(i, k) cout << a[i] << " \n"[i + 1 == k];
  vector<vector<Edge>> graph(n);
  REP(_, n - 1) {
    int e, f; cin >> e >> f; --e; --f;
    graph[f].emplace_back(f, e, 1);
  }
  LCADoubling lca(graph);
  lca.build(0);
  SegmentTree<int> seg(a, [&](int l, int r) { if (l == -1) return r; if (r == -1) return l; return lca.query(l, r); }, -1);
  vector<int> dp(n, 0);
  function<void(int)> dfs = [&](int ver) {
    chmax(dp[ver], c[ver]);
    for (const Edge &e : graph[ver]) {
      chmax(dp[e.dst], dp[ver]);
      dfs(e.dst);
    }
  };
  dfs(0);
  while (q--) {
    int t; cin >> t;
    if (t == 1) {
      int x, y; cin >> x >> y; --x; --y;
      seg.update(x, y);
    } else if (t == 2) {
      int l, r; cin >> l >> r; --l; --r;
      cout << dp[seg.query(l, r + 1)] << '\n';
    }
  }
  return 0;
}
0