#include using namespace std; struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } template istream &operator>>(istream &is, vector &vec){ for (auto &v : vec) is >> v; return is; } template ostream &operator<<(ostream &os, const vector &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } #define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl; // lowest common ancestor (LCA) class for undirected weighted tree // 無向重み付きグラフの最小共通祖先 // struct UndirectedWeightedTree { using T = long long int; // Arbitrary data structure (operator+, operator- must be defined) int INVALID = -1; int V, lgV; int E; int root; vector>> adj; // (nxt_vertex, edge_id) // vector edge; // edges[edge_id] = (vertex_id, vertex_id) vector weight; // w[edge_id] vector par; // parent_vertex_id[vertex_id] vector depth; // depth_from_root[vertex_id] vector acc_weight; // w_sum_from_root[vertex_id] void _fix_root_dfs(int now, int prv, int prv_edge_id) { par[now] = prv; if (prv_edge_id != INVALID) acc_weight[now] = acc_weight[prv] + weight[prv_edge_id]; for (auto nxt : adj[now]) if (nxt.first != prv) { depth[nxt.first] = depth[now] + 1; _fix_root_dfs(nxt.first, now, nxt.second); } } UndirectedWeightedTree() = default; UndirectedWeightedTree(int N) : V(N), E(0), adj(N) { lgV = 1; while (1 << lgV < V) lgV++; } void add_edge(int u, int v, T w) { adj[u].emplace_back(v, E); adj[v].emplace_back(u, E); // edge.emplace_back(u, v); weight.emplace_back(w); E++; } void fix_root(int r) { root = r; par.resize(V); depth.resize(V); depth[r] = 0; acc_weight.resize(V); acc_weight[r] = 0; _fix_root_dfs(root, INVALID, INVALID); } vector> doubling; void doubling_precalc() { doubling.assign(lgV, vector(V)); doubling[0] = par; for (int d = 0; d < lgV - 1; d++) for (int i = 0; i < V; i++) { if (doubling[d][i] == INVALID) doubling[d + 1][i] = INVALID; else doubling[d + 1][i] = doubling[d][doubling[d][i]]; } } int kth_parent(int x, int k) { if (depth[x] < k) return INVALID; for (int d = 0; d < lgV; d++) { if (x == INVALID) return INVALID; if (k & (1 << d)) x = doubling[d][x]; } return x; } int lowest_common_ancestor(int u, int v) { if (depth[u] > depth[v]) swap(u, v); v = kth_parent(v, depth[v] - depth[u]); if (u == v) return u; for (int d = lgV - 1; d >= 0; d--) { if (doubling[d][u] != doubling[d][v]) u = doubling[d][u], v = doubling[d][v]; } return par[u]; } T path_length(int u, int v) { // Not distance, but the sum of weights int r = lowest_common_ancestor(u, v); return (acc_weight[u] - acc_weight[r]) + (acc_weight[v] - acc_weight[r]); } }; // Nonrecursive Segment Tree (point-update, range-get) // - Conditions for operations: // - datamerge: [TDATA, TDATA] -> TDATA, e(x, y) == e(y, x) // - data2ret: [TDATA, TQUERY] -> TRET // - retmerge: [TRET, TRET] -> TRET, g(defaultRET, x) == x, g(x, y) = g(y, x) // - commutability f(e(x, y), q) == g(f(x, q), f(y, q)) template struct NonrecursiveSegmentTree { int N; TRET defaultRET; virtual TDATA datamerge(const TDATA &, const TDATA &) = 0; virtual TRET data2ret(const TDATA &, const TQUERY &) = 0; virtual TRET retmerge(const TRET &, const TRET &) = 0; std::vector data; inline TDATA& at(int i) { return data[i]; } inline void _merge(int i) { at(i) = datamerge(at(i << 1), at((i << 1) + 1)); } void initialize(const std::vector &seq, TRET RET_ZERO) { N = seq.size(); defaultRET = RET_ZERO; data = seq; data.insert(data.end(), seq.begin(), seq.end()); for (int i = N - 1; i; i--) _merge(i); } NonrecursiveSegmentTree() = default; void update(int pos, const TDATA &x) { assert(pos >= 0 and pos < N); at(pos + N) = x; for (int i = pos + N; i > 1;) i >>= 1, _merge(i); } // [l, r), 0-indexed TRET get(int l, int r, TQUERY query = NULL) { assert(l >= 0 and r <= N); TRET retl = defaultRET, retr = defaultRET; l += N, r += N; while (l < r) { if (l & 1) retl = retmerge(retl, data2ret(data[l++], query)); if (r & 1) retr = retmerge(data2ret(data[--r], query), retr); l >>= 1, r >>= 1; } return retmerge(retl, retr); } // Calculate smallest r that satisfies g(f(x_l, q), ..., f(x_{r - 1}, q)) >= threshold // Assumption: Monotonicity of g(x_l, ..., x_r) about r (l: fixed) // Complexity: O(log N) int binary_search(int l, const TRET &threshold, TQUERY query = NULL) { std::stack rs; l += N; int r = N * 2; TRET retl = defaultRET; if (threshold <= retl) return l - N; while (l < r) { if (l & 1) { TRET ret_tmp = retmerge(retl, data2ret(data[l], query)); if (threshold <= ret_tmp) { while (l * 2 < N * 2) { ret_tmp = retmerge(retl, data2ret(data[l * 2], query)); if (threshold <= ret_tmp) l *= 2; else retl = ret_tmp, l = l * 2 + 1; } return l - N; } l++; retl = ret_tmp; } if (r & 1) rs.push(--r); l >>= 1, r >>= 1; } while (!rs.empty()) { l = rs.top(); rs.pop(); TRET ret_tmp = retmerge(retl, data2ret(data[l], query)); if (threshold <= ret_tmp) { while (l * 2 < N * 2) { ret_tmp = retmerge(retl, data2ret(data[l * 2], query)); if (threshold <= ret_tmp) l *= 2; else retl = ret_tmp, l = l * 2 + 1; } return l - N; } retl = ret_tmp; } return N; } template friend std::ostream &operator<<(std::ostream &os, NonrecursiveSegmentTree s) { os << "[SegmentTree (len: " << s.N << ')'; for (int i = 0; i < s.N; i++) os << s.at(i + s.N) << ','; os << "]"; return os; } }; UndirectedWeightedTree tree; // Range Minimum Query // - get: return min(x_l, ..., x_{r - 1}) struct RangeLCAQuery : public NonrecursiveSegmentTree { using T = int; using SegTree = NonrecursiveSegmentTree; T datamerge(const T &vl, const T &vr) override { if (vl == -1) return vr; if (vr == -1) return vl; return tree.lowest_common_ancestor(vl, vr); }; T data2ret(const T &v, const bool &q) override { return v; } T retmerge(const T &vl, const T &vr) override { return datamerge(vl, vr); }; RangeLCAQuery(const std::vector &seq) : SegTree::NonrecursiveSegmentTree() { SegTree::initialize(seq, -1); }; }; vector> to; vector C; void dfs(int now, int par, int am) { chmax(C[now], am); for (auto nxt : to[now]) if (nxt != par) dfs(nxt, now, C[now]); } int main() { int N, K, Q; cin >> N >> K >> Q; tree = UndirectedWeightedTree(N); C.resize(N); vector A(K); cin >> C >> A; for (auto &a : A) a--; to.resize(N); REP(_, N - 1) { int e, f; cin >> e >> f; e--, f--; tree.add_edge(e, f, 1); to[e].push_back(f); to[f].push_back(e); } tree.fix_root(0); tree.doubling_precalc(); RangeLCAQuery lca(A); dfs(0, -1, 0); while (Q--) { int t; cin >> t; if (t == 1) { int X, Y; cin >> X >> Y; X--, Y--; A[X] = Y; lca.update(X, A[X]); } else { int L, R; cin >> L >> R; cout << C[lca.get(L - 1, R)] << '\n'; } } }