結果

問題 No.1030 だんしんぐぱーりない
ユーザー 👑 emthrmemthrm
提出日時 2020-04-17 22:52:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 8,967 bytes
コンパイル時間 3,154 ms
コンパイル使用メモリ 225,216 KB
実行使用メモリ 41,732 KB
最終ジャッジ日時 2024-04-14 15:09:25
合計ジャッジ時間 12,731 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 1 ms
6,940 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 AC 2 ms
6,940 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 = -1, r_res = -1;
    for (left += n, right += n; left < right; left >>= 1, right >>= 1) {
      if (left & 1) l_res = l_res == -1 ? dat[left++] : fn(l_res, dat[left++]);
      if (right & 1) r_res = r_res == -1 ? dat[--right] : fn(dat[--right], r_res);
    }
    if (l_res == -1) return r_res;
    if (r_res == -1) return l_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; }
};

struct HLD {
  vector<int> parent, subtree, id, inv, head;
  vector<CostType> cost;

  HLD(const vector<vector<Edge>> &graph, int root = 0) : graph(graph) {
    int n = graph.size();
    parent.assign(n, -1);
    subtree.assign(n, 1);
    id.resize(n);
    inv.resize(n);
    head.resize(n);
    dfs1(root);
    head[root] = root;
    int now_id = 0;
    dfs2(root, now_id);
  }

  void v_update(int u, int v, function<void(int, int)> f) {
    while (true) {
      if (id[u] > id[v]) swap(u, v);
      f(max(id[head[v]], id[u]), id[v] + 1);
      if (head[u] == head[v]) return;
      v = parent[head[v]];
    }
  }

  template <typename T>
  T v_query(int u, int v, function<T(int, int)> f, function<T(T, T)> g, const T UNITY) {
    T left = UNITY, right = UNITY;
    while (true) {
      if (id[u] > id[v]) {
        swap(u, v);
        swap(left, right);
      }
      left = g(left, f(max(id[head[v]], id[u]), id[v] + 1));
      if (head[u] == head[v]) break;
      v = parent[head[v]];
    }
    return g(left, right);
  }

  void subtree_v_update(int ver, function<void(int, int)> f) { f(id[ver], id[ver] + subtree[ver]); }

  template <typename T>
  T subtree_v_query(int ver, function<T(int, int)> f) { return f(id[ver], id[ver] + subtree[ver]); }

  void e_update(int u, int v, function<void(int, int)> f) {
    while (true) {
      if (id[u] > id[v]) swap(u, v);
      if (head[u] == head[v]) {
        f(id[u], id[v]);
        break;
      } else {
        f(id[head[v]] - 1, id[v]);
        v = parent[head[v]];
      }
    }
  }

  template <typename T>
  T e_query(int u, int v, function<T(int, int)> f, function<T(T, T)> g, const T UNITY) {
    T left = UNITY, right = UNITY;
    while (true) {
      if (id[u] > id[v]) {
        swap(u, v);
        swap(left, right);
      }
      if (head[u] == head[v]) {
        left = g(left, f(id[u], id[v]));
        break;
      } else {
        left = g(left, f(id[head[v]] - 1, id[v]));
        v = parent[head[v]];
      }
    }
    return g(left, right);
  }

  void subtree_e_update(int ver, function<void(int, int)> f) { f(id[ver], id[ver] + subtree[ver] - 1); }

  template <typename T>
  T subtree_e_query(int ver, function<T(int, int)> f) { return f(id[ver], id[ver] + subtree[ver] - 1); }

  int lca(int u, int v) {
    while (true) {
      if (id[u] > id[v]) swap(u, v);
      if (head[u] == head[v]) return u;
      v = parent[head[v]];
    }
  }

private:
  vector<vector<Edge>> graph;

  void dfs1(int ver) {
    for (Edge &e : graph[ver]) {
      if (e.dst != parent[ver]) {
        parent[e.dst] = ver;
        dfs1(e.dst);
        subtree[ver] += subtree[e.dst];
        if (subtree[e.dst] > subtree[graph[ver].front().dst]) swap(e, graph[ver].front());
      }
    }
  }

  void dfs2(int ver, int &now_id) {
    id[ver] = now_id++;
    inv[id[ver]] = ver;
    for (const Edge &e : graph[ver]) {
      if (e.dst != parent[ver]) {
        head[e.dst] = (e.dst == graph[ver].front().dst ? head[ver] : e.dst);
        cost.emplace_back(e.cost);
        dfs2(e.dst, now_id);
      }
    }
  }
};

template <typename MeetSemilattice>
struct SparseTable {
  using Fn = function<MeetSemilattice(MeetSemilattice, MeetSemilattice)>;

  SparseTable() {}

  SparseTable(const vector<MeetSemilattice> &a, Fn fn, MeetSemilattice UNITY = 0) { init(a, fn); }

  void init(const vector<MeetSemilattice> &a, Fn fn_) {
    fn = fn_;
    int n = a.size(), table_h = 0;
    lg.assign(n + 1, 0);
    FOR(i, 2, n + 1) lg[i] = lg[i >> 1] + 1;
    while ((1 << table_h) <= n) ++table_h;
    dat.assign(table_h, vector<MeetSemilattice>(n));
    REP(j, n) dat[0][j] = a[j];
    FOR(i, 1, table_h) for (int j = 0; j + (1 << i) <= n; ++j) {
      dat[i][j] = fn(dat[i - 1][j], dat[i - 1][j + (1 << (i - 1))]);
    }
  }

  MeetSemilattice query(int left, int right) {
    assert(left < right);
    int h = lg[right - left];
    return fn(dat[h][left], dat[h][right - (1 << h)]);
  }

private:
  Fn fn;
  vector<int> lg;
  vector<vector<MeetSemilattice>> dat;
};

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);
    graph[e].emplace_back(e, f, 1);
  }
  LCADoubling lca(graph);
  lca.build(0);
  SegmentTree<int> seg(k, [&](int l, int r) { return lca.query(l, r); }, 0);
  REP(i, k) seg.update(i, a[i]);
  HLD hld(graph);
  vector<int> vigor(n);
  REP(i, n) vigor[i] = c[hld.id[i]];
  SparseTable<int> st(vigor, [](int l, int r) { return max(l, r); });
  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;
      int ver = seg.query(l, r + 1);
      cout << hld.v_query<int>(0, ver, [&](int L, int R) { return st.query(L, R); }, [](int L, int R) { return max(L, R); }, 0) << '\n';
    }
  }
  return 0;
}
0