活动介绍
file-type

使用ST表实现最近公共祖先(LCA)查询的算法详解

版权申诉

ZIP文件

5星 · 超过95%的资源 | 1KB | 更新于2024-12-01 | 24 浏览量 | 1 下载量 举报 收藏
download 限时特惠:#11.90
ST表是一种预处理数据结构,用于在对数时间内解决动态规划问题的子问题。本资源将详细介绍ST表的构建方法,并且结合欧拉序(Euler Tour)来实现对LCA的高效查询。此外,还会提供一个C++编程语言实现的示例代码文件,即'St表欧拉序倍增搞LCA.cpp',供读者参考学习。" 知识点详细说明: 1. 最近公共祖先(LCA)问题简介: LCA问题通常出现在树形结构中,需要找出两个节点的最近公共祖先节点。该问题在计算机科学领域有着广泛的应用,例如在图形用户界面、网络路由、分布式系统中寻找对象的共同祖先是常见的需求。 2. ST表(Sparse Table)数据结构: ST表是一种用于快速查询区间最值的数据结构,例如最小值、最大值、GCD(最大公约数)等。其核心思想是在预处理阶段通过动态规划填充一个二维表格,之后的查询可以迅速通过表格中的信息直接计算得出,而无需重新遍历区间。 3. ST表构建方法: 构建ST表的过程通常包括: - 选择一个单调性函数,例如min、max、gcd等。 - 在预处理阶段,根据所选函数和数据范围,计算出所有可能区间的最值。 - 填充ST表时,利用已知的两个相邻区间的最值,通过合并已知信息来计算新区间最值。 4. 欧拉序(Euler Tour): 欧拉序是树的一种遍历方法,它按照先根遍历的顺序为树中的每个节点分配一个唯一的访问顺序号。在此过程中,每个节点会被访问两次,一次是进入该节点,另一次是离开。这个属性使得欧拉序非常适合用于处理LCA问题,因为它能够将树的节点在一条线上展开,方便使用ST表进行查询。 5. LCA查询实现: 结合ST表和欧拉序,可以通过以下步骤进行LCA查询: - 使用欧拉序得到两个节点在树中的访问顺序号。 - 利用访问顺序号,计算它们之间的区间,并使用ST表查询该区间内的最值,这个最值就是所求的最近公共祖先。 6. 示例代码分析: 给定的文件"St表欧拉序倍增搞LCA.cpp"提供了一个使用C++语言实现ST表结合欧拉序处理LCA查询的示例。代码中可能包含了数据结构的定义、ST表的构建函数、欧拉序的计算过程以及LCA查询函数。通过阅读和理解该示例代码,可以加深对ST表和LCA查询算法的理解,并学习如何在实际编程中应用这些概念。 总结,本资源通过提供详细的知识点介绍和实际代码示例,帮助读者全面理解使用ST表结合欧拉序解决LCA查询问题的原理和方法。这对于数据结构和算法的学习者、开发者来说,是一个宝贵的学习资源。

相关推荐

filetype

下面的代码是干什么用的,请生成说明注释,同时还有什么改进: #include <iostream> #include <vector> #include <cmath> #include <algorithm> using namespace std; const int MAXN = 100010; const int MAXLOG = 20; vector<int> tree[MAXN]; int depth[MAXN], parent[MAXN][MAXLOG]; void dfs(int u, int p) { depth[u] = depth[p] + 1; parent[u][0] = p; for (int j = 1; j < MAXLOG; j++) { parent[u][j] = (parent[u][j-1] == -1) ? -1 : parent[parent[u][j-1]][j-1]; } for (int v : tree[u]) { if (v != p) dfs(v, u); } } void preprocess(int root, int n) { fill(depth, depth + n + 1, 0); for (int i = 0; i <= n; i++) { for (int j = 0; j < MAXLOG; j++) parent[i][j] = -1; } depth[root] = 0; for (int v : tree[root]) dfs(v, root); } int lca(int a, int b) { if (depth[a] < depth[b]) swap(a, b); int j = log2(depth[a]) + 1; for (int k = j; k >= 0; k--) { if (depth[a] - (1 << k) >= depth[b]) a = parent[a][k]; } if (a == b) return a; for (int k = j; k >= 0; k--) { if (parent[a][k] != -1 && parent[a][k] != parent[b][k]) { a = parent[a][k]; b = parent[b][k]; } } return parent[a][0]; } int main() { int n = 5; // 节点数 // 示例树: 1-2, 1-3, 2-4, 2-5 tree[1].push_back(2); tree[2].push_back(1); tree[1].push_back(3); tree[3].push_back(1); tree[2].push_back(4); tree[4].push_back(2); tree[2].push_back(5); tree[5].push_back(2); preprocess(1, n); // 根节点为1 cout << "LCA(4,5): " << lca(4,5) << endl; // 输出2 cout << "LCA(3,4): " << lca(3,4) << endl; // 输出1 return 0; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return new BigInteger(1, digest).toString(16); }】

filetype

调一下代码(WC2013糖果公园,1<=n,m,q<=1e5),RE了: #include<bits/stdc++.h> using namespace std; long long n, m, Q, block, top, v[200010], w[200010], cn[200010]; vector<long long> vec[200010]; long long ans, cnt1, cnt2, pt, pl, pr; long long stk[200010], t1[200010], t2[200010]; long long dep[200010], q[200010], fa[26][200010]; long long sum[200010], cnt[200010], st[200010], pp[200010]; struct node{ long long l, r, pp; long long time, qtime; friend bool operator<(node p, node q){ return (p.l / block != q.l / block) ? (p.l < q.l) : ((p.r / block != q.r / block ? (p.r < q.r) : (p.time < q.time))); } }b1[200010]; struct node2{ long long l, r, time; }b2[200010]; void dfs(long long x, long long fa){ stk[++ top] = x; t1[x] = top; for(auto i : vec[x]){ if(i == fa) continue; dfs(i, x); } stk[++ top] = x; t2[x] = top; } void bfs(long long rt){ memset(dep, 0x3f, sizeof(dep)); dep[0] = 0, dep[rt] = 1; long long hh = 0, tt = 0; q[0] = rt; while(hh <= tt){ long long t = q[hh ++]; for(auto i : vec[t]){ if(dep[i] > dep[t] + 1){ dep[i] = dep[t] + 1; q[++ tt] = i; fa[i][0] = t; for(long long k = 1; k <= 25; ++ k){ fa[i][k] = fa[fa[i][k - 1]][k - 1]; } } } } } long long lca(long long x, long long y){ if(dep[x] < dep[y]) swap(x, y); for(long long i = 25; i >= 0; -- i){ if(dep[fa[x][i]] >= dep[y]){ x = fa[x][i]; } } if(x == y) return x; for(long long i = 25; i >= 0; -- i){ if(fa[x][i] != fa[y][i]){ x = fa[x][i], y = fa[y][i]; } } return fa[x][0]; } void add(long long x){ st[x] ^= 1; if(st[x]){ cnt[cn[x]] ++; ans += v[cn[x]] * w[cnt[cn[x]]]; } else{ ans -= v[cn[x]] * w[cnt[cn[x]]]; cnt[cn[x]] --; } } int main(){ scanf("%lld %lld %lld", &n, &m, &Q); for(long long i = 1; i <= m; ++ i){ scanf("%lld", &v[i]); } for(long long i = 1; i <= n; ++ i){ scanf("%lld", &w[i]); } for(long long i = 1; i < n; ++ i){ long long x, y; scanf("%lld %lld", &x, &y); vec[x].push_back(y); vec[y].push_back(x); } for(long long i = 1; i <= n; ++ i){ scanf("%lld", &cn[i]); } dfs(1, -1); bfs(1); for(long long i = 1; i <= Q; ++ i){ long long op, l, r; scanf("%lld %lld %lld", &op, &l, &r); if(op == 0){ b2[++ cnt2].l = l; b2[cnt2].r = r; } else{ if(t1[l] > t1[r]) swap(l, r); long long p = lca(l, r); if(p == l){ b1[++ cnt1].l = t1[l]; b1[cnt1].r = t1[r]; b1[cnt1].pp = 0; } else{ b1[++ cnt1].l = t2[l]; b1[cnt1].r = t1[r]; b1[cnt1].pp = p; } b1[cnt1].time = cnt1; b1[cnt1].qtime = cnt2; } } block = max(1ll, (long long)cbrt(top * top)); sort(b1 + 1, b1 + cnt1 + 1); pt = 0, pl = 1, pr = 0; for(long long i = 1; i <= cnt1; ++ i){ while(pr < b1[i].r) ++ pr, add(stk[pr]); while(pr > b1[i].r) add(stk[pr]), -- pr; while(pl > b1[i].l) -- pl, add(stk[pl]); while(pl < b1[i].l) add(stk[pl]), ++ pl; while(pt < b1[i].qtime){ ++ pt; if(st[b2[pt].l]){ add(b2[pt].l); swap(b2[pt].r, cn[b2[pt].l]); add(b2[pt].l); } else{ swap(b2[pt].r, cn[b2[pt].l]); } } while(pt > b1[i].qtime){ if(st[b2[pt].l]){ add(b2[pt].l); swap(b2[pt].r, cn[b2[pt].l]); add(b2[pt].l); } else{ swap(b2[pt].r, cn[b2[pt].l]); } -- pt; } if(b1[i].pp){ add(b1[i].pp); } pp[b1[i].time] = ans; if(b1[i].pp){ add(b1[i].pp); } } for(long long i = 1; i <= cnt1; ++ i){ printf("%lld\n", pp[i]); } return 0; }

filetype

# CF1479D Odd Mineral Resource ## 题目描述 In Homer's country, there are $ n $ cities numbered $ 1 $ to $ n $ and they form a tree. That is, there are $ (n-1) $ undirected roads between these $ n $ cities and every two cities can reach each other through these roads. Homer's country is an industrial country, and each of the $ n $ cities in it contains some mineral resource. The mineral resource of city $ i $ is labeled $ a_i $ . Homer is given the plans of the country in the following $ q $ years. The plan of the $ i $ -th year is described by four parameters $ u_i, v_i, l_i $ and $ r_i $ , and he is asked to find any mineral resource $ c_i $ such that the following two conditions hold: - mineral resource $ c_i $ appears an odd number of times between city $ u_i $ and city $ v_i $ ; and - $ l_i \leq c_i \leq r_i $ . As the best friend of Homer, he asks you for help. For every plan, find any such mineral resource $ c_i $ , or tell him that there doesn't exist one. ## 输入格式 The first line contains two integers $ n $ ( $ 1 \leq n \leq 3 \cdot 10^5 $ ) and $ q $ ( $ 1 \leq q \leq 3 \cdot 10^5 $ ), indicating the number of cities and the number of plans. The second line contains $ n $ integers $ a_1, a_2, \dots, a_n $ ( $ 1 \leq a_i \leq n $ ). Then the $ i $ -th line of the following $ (n-1) $ lines contains two integers $ x_i $ and $ y_i $ ( $ 1 \leq x_i, y_i \leq n $ ) with $ x_i \neq y_i $ , indicating that there is a bidirectional road between city $ x_i $ and city $ y_i $ . It is guaranteed that the given roads form a tree. Then the $ i $ -th line of the following $ q $ lines contains four integers $ u_i $ , $ v_i $ , $ l_i $ , $ r_i $ ( $ 1 \leq u_i \leq n $ , $ 1 \leq v_i \leq n $ , $ 1 \leq l_i \leq r_i \leq n $ ), indicating the plan of the $ i $ -th year. ## 输出格式 Print $ q $ lines, the $ i $ -th of which contains an integer $ c_i $ such that - $ c_i = {-1} $ if there is no such mineral resource that meets the required condition; or - $ c_i $ is the label of the chosen mineral resource of the $ i $ -th year. The chosen mineral resource $ c_i $ should meet those conditions in the $ i $ -th year described above in the problem statement. If there are multiple choices of $ c_i $ , you can print any of them. ## 输入输出样例 #1 ### 输入 #1 ``` 6 8 3 2 1 3 1 3 1 2 1 3 2 4 2 5 4 6 3 5 1 1 3 5 1 3 3 5 1 3 1 1 2 2 1 1 3 3 1 4 1 5 1 6 1 3 1 6 1 3 ``` ### 输出 #1 ``` -1 2 3 -1 3 2 2 3 ``` ## 说明/提示 In the first three queries, there are four cities between city $ 3 $ and city $ 5 $ , which are city $ 1 $ , city $ 2 $ , city $ 3 $ and city $ 5 $ . The mineral resources appear in them are mineral resources $ 1 $ (appears in city $ 3 $ and city $ 5 $ ), $ 2 $ (appears in city $ 2 $ ) and $ 3 $ (appears in city $ 1 $ ). It is noted that - The first query is only to check whether mineral source $ 1 $ appears an odd number of times between city $ 3 $ and city $ 5 $ . The answer is no, because mineral source $ 1 $ appears twice (an even number of times) between city $ 3 $ and city $ 5 $ . - The second and the third queries are the same but they can choose different mineral resources. Both mineral resources $ 2 $ and $ 3 $ are available. 为什么WA了 ```cpp #include <bits/stdc++.h> using namespace std; const int BITS = 20; const int MAX_N = 4e5 + 50; const int BSIZE = 1; int n, m, tot, a[MAX_N]; int fa[MAX_N][BITS], dep[MAX_N]; int dfn[MAX_N], in[MAX_N], out[MAX_N]; vector<int> e[MAX_N]; int L[MAX_N], R[MAX_N], pos[MAX_N]; int cnt[MAX_N], sum[MAX_N], ans[MAX_N]; struct Query { int l, r, p, L, R, id; bool operator < (const Query& rhs) const { if (pos[l] != pos[rhs.l]) return pos[l] < pos[rhs.l]; return (pos[l] & 1) ? r < rhs.r : r > rhs.r; } } q[MAX_N]; void init() { int cnt = n / BSIZE; for (int i = 1; i <= cnt; i++) { L[i] = (i - 1) * BSIZE + 1; R[i] = i * BSIZE; } if (R[cnt] < n) { L[cnt + 1] = R[cnt] + 1; R[++cnt] = n; } for (int i = 1; i <= cnt; i++) fill(pos + L[i], pos + R[i] + 1, i); } void modify(int idx) { cout << "modify " << idx << '\n'; sum[pos[idx]] -= cnt[idx]; cnt[idx] ^= 1; sum[pos[idx]] += cnt[idx]; } int query(int lt, int rt) { int lp = pos[lt], rp = pos[rt]; if (lp == rp) { for (int i = lt; i <= rt; i++) if (cnt[i]) return i; } else { for (int i = lt; i <= R[lp]; i++) if (cnt[i]) return i; for (int i = lp + 1; i < rp; i++) if (sum[i]) for (int j = L[i]; j <= R[i]; j++) if (cnt[j]) return j; for (int i = L[rp]; i <= rt; i++) if (cnt[i]) return i; } return -1; } void dfs(int u, int father, int depth) { fa[u][0] = father, dep[u] = depth; for (int k = 1; k < BITS; k++) fa[u][k] = fa[fa[u][k - 1]][k - 1]; in[u] = ++tot, dfn[tot] = u; for (int v : e[u]) if (v != father) dfs(v, u, depth + 1); out[u] = ++tot, dfn[tot] = u; } int LCA(int x, int y) { if (dep[x] > dep[y]) swap(x, y); for (int k = BITS - 1; ~k; k--) if (dep[x] <= dep[fa[y][k]]) y = fa[y][k]; if (x == y) return x; for (int k = BITS - 1; ~k; k--) if (fa[x][k] != fa[y][k]) x = fa[x][k], y = fa[y][k]; return x; } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1, u, v; i < n; i++) { cin >> u >> v; e[u].push_back(v); e[v].push_back(u); } dfs(1, 0, 1); for (int i = 1, u, v; i <= m; i++) { cin >> u >> v >> q[i].L >> q[i].R; q[i].id = i; if (in[u] > in[v]) swap(u, v); q[i].r = in[v]; int lca = LCA(u, v); if (lca == u) q[i].l = in[u], q[i].p = 0; else q[i].l = out[u], q[i].p = lca; } init(); sort(q + 1, q + m + 1); int l = 1, r = 0; for (int i = 1; i <= m; i++) { int ql = q[i].l, qr = q[i].r; while (l > ql) modify(a[dfn[--l]]); while (r < qr) modify(a[dfn[++r]]); while (l < ql) modify(a[dfn[l++]]); while (r > qr) modify(a[dfn[r--]]); if (q[i].p) modify(a[q[i].p]); cout << q[i].id << '\n'; ans[q[i].id] = query(q[i].L, q[i].R); if (q[i].p) modify(a[q[i].p]); } for (int i = 1; i <= n; i++) cout << ans[i] << '\n'; return 0; } ```

filetype

# P4074 [WC2013] 糖果公园 ## 题目描述 Candyland 有一座糖果公园,公园里不仅有美丽的风景、好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来糖果公园游玩。 糖果公园的结构十分奇特,它由 $n$ 个游览点构成,每个游览点都有一个糖果发放处,我们可以依次将游览点编号为 $1$ 至 $n$。有 $n - 1$ 条双向道路连接着这些游览点,并且整个糖果公园都是连通的,即从任何一个游览点出发都可以通过这些道路到达公园里的所有其它游览点。 糖果公园所发放的糖果种类非常丰富,总共有 $m$ 种,它们的编号依次为 $1$ 至 $m$。每一个糖果发放处都只发放某种特定的糖果,我们用 $C_i$ 来表示 $i$ 号游览点的糖果。 来到公园里游玩的游客都不喜欢走回头路,他们总是从某个特定的游览点出发前往另一个特定的游览点,并游览途中的景点,这条路线一定是唯一的。他们经过每个游览点,都可以品尝到一颗对应种类的糖果。 大家对不同类型糖果的喜爱程度都不尽相同。 根据游客们的反馈打分,我们得到了糖果的美味指数, 第 $i$ 种糖果的美味指数为 $V_i$。另外,如果一位游客反复地品尝同一种类的糖果,他肯定会觉得有一些腻。根据量化统计,我们得到了游客第 $i$ 次品尝某类糖果的新奇指数 $W_i$。如果一位游客第 $i$ 次品尝第 $j$ 种糖果,那么他的愉悦指数 $H$ 将会增加对应的美味指数与新奇指数的乘积,即 $V_j \times W_i$。这位游客游览公园的愉悦指数最终将是这些乘积的和。 当然,公园中每个糖果发放点所发放的糖果种类不一定是一成不变的。有时,一些糖果点所发放的糖果种类可能会更改(也只会是 $m$ 种中的一种),这样的目的是能够让游客们总是感受到惊喜。 糖果公园的工作人员小 A 接到了一个任务,那就是根据公园最近的数据统计出每位游客游玩公园的愉悦指数。但数学不好的小 A 一看到密密麻麻的数字就觉得头晕,作为小 A 最好的朋友,你决定帮他一把。 ## 输入格式 从文件 `park.in` 中读入数据。 第一行包含三个正整数 $n, m, q$, 分别表示游览点个数、 糖果种类数和操作次数。 第二行包含 $m$ 个正整数 $V_1, V_2, \ldots, V_m$。 第三行包含 $n$ 个正整数 $W_1, W_2, \ldots, W_n$。 第四行到第 $n + 2$ 行,每行包含两个正整数 $A_i, B_i$,表示这两个游览点之间有路径可以直接到达。 第 $n + 3$ 行包含 $n$ 个正整数 $C_1, C_2, \ldots, C_n$。 接下来 $q$ 行, 每行包含三个整数 $Type, x, y$,表示一次操作: - 若 $Type$ 为 $0$,则 $1 \leq x \leq n$, $1 \leq y \leq m$,表示将编号为 $x$ 的游览点发放的糖果类型改为 $y$; - 若 $Type$ 为 $1$,则 $1 \leq x, y \leq n$,表示对出发点为 $x$,终止点为 $y$ 的路线询问愉悦指数。 ## 输出格式 输出到文件 `park.out` 中。 按照输入的先后顺序,对于每个 $Type$ 为 $1$ 的操作输出一行,用一个正整数表示答案。 ## 输入输出样例 #1 ### 输入 #1 ``` 4 3 5 1 9 2 7 6 5 1 2 3 3 1 3 4 1 2 3 2 1 1 2 1 4 2 0 2 1 1 1 2 1 4 2 ``` ### 输出 #1 ``` 84 131 27 84 ``` ## 说明/提示 【样例解释】 我们分别用 ![](https://cdn.luogu.com.cn/upload/image_hosting/isw3ib3u.png) 代表 $C_i$ 为 $1$、 $2$、 $3$ 的节点,在修改之前: ![](https://cdn.luogu.com.cn/upload/image_hosting/ttkzii1u.png) 在将 $C_2$ 修改为 $1$ 之后: ![](https://cdn.luogu.com.cn/upload/image_hosting/izro364w.png) 【数据规模与约定】 对于所有的数据: $1 \leq V_i, W_i \leq 10^6$,$1 \leq A_i, B_i \leq n$, $1 \leq C_i \leq m$, $W_1, W_2, \ldots, W_n$ 是非递增序列,即对任意 $1 < i \leq n$, 满足 $W_i \le W_{i-1}$。 其它的限制条件如下表所示: ![QQ20180113072014.png](https://cdn.luogu.com.cn/upload/image_hosting/g6884nx1.png) #include <bits/stdc++.h> #define ll long long using namespace std; const ll N=2e6; struct node{ ll l,r,f,t,ans; }a[N+5]; struct node1{ ll x,y; }b[N+5]; ll n,m,q; ll c[N+5],v[N+5],w[N+5]; ll cnta,cntb; ll lenb,blk[N+5]; ll fst[N+5],lst[N+5],dfn[N+5],dfnn; ll xl[N+5],xll; ll sum[N+5]; vector<ll> e[N+5]; ll lp=1,rp,s,now; ll cntc[N+5],jl[N+5]; ll st[N+5][21],dep[N+5]; inline ll read() { ll x=0,f=1; char c=getchar(); while (c<'0' || c>'9') { if (c=='-') f=-1; c=getchar(); } while (c>='0' && c<='9') { x=x*10+c-'0'; c=getchar(); } return x*f; } bool cmp(node l1,node l2){ if(blk[l1.l]==blk[l2.l]){ if(blk[l1.r]==blk[l2.r]){ return l1.t<l2.t; } return blk[l1.r]<blk[l2.r]; } return blk[l1.l]<blk[l2.l]; } bool cmpf(node l1,node l2){ return l1.f<l2.f; } ll get(ll col,ll x){ return sum[x]*v[col]; } void dfs(ll x,ll fa){ fst[x]=++dfnn; dfn[dfnn]=x; dep[x]=dep[fa]+1; st[x][0]=fa; xl[x]=++xll; for(ll y:e[x]){ if(y==fa) continue; dfs(y,x); } lst[x]=++dfnn; dfn[dfnn]=x; return; } void add(ll p){ ll x=dfn[p],col=c[x]; jl[x]++; if(jl[x]==2){ s-=get(col,cntc[col]); cntc[col]--; s+=get(col,cntc[col]); return; } s-=get(col,cntc[col]); cntc[col]++; s+=get(col,cntc[col]); return; } void del(ll p){ ll x=dfn[p],col=c[x]; jl[x]--; if(jl[x]==1){ s-=get(col,cntc[col]); cntc[col]++; s+=get(col,cntc[col]); return; } s-=get(col,cntc[col]); cntc[col]--; s+=get(col,cntc[col]); return; } void addd(ll col){ s-=get(col,cntc[col]); cntc[col]++; s+=get(col,cntc[col]); return; } void dell(ll col){ s-=get(col,cntc[col]); cntc[col]--; s+=get(col,cntc[col]); return; } void solve(ll l,ll r,ll t,ll &ans){ while(l<lp){ lp--; add(lp); } while(lp<l){ del(lp); lp++; } while(rp<r){ rp++; add(rp); } while(r<rp){ del(rp); rp--; } while(now<t){ now++; // cout<<l<<"oooooo"<<r<<endl; ll col=c[b[now].x],x=b[now].x; if(((l<=fst[x] && fst[x]<=r) || (l<=lst[x] && lst[x]<=r)) && jl[x]!=2){ dell(col); swap(c[b[now].x],b[now].y); col=c[b[now].x]; addd(col); } else swap(c[b[now].x],b[now].y); } while(now>t){ // cout<<l<<"rrrrrrrrr"<<r<<endl; ll col=c[b[now].x],x=b[now].x; if(((l<=fst[x] && fst[x]<=r) || (l<=lst[x] && lst[x]<=r)) && jl[x]!=2){ dell(col); swap(c[b[now].x],b[now].y); col=c[b[now].x]; addd(col); } else swap(c[b[now].x],b[now].y); now--; } // cout<<lp<<"ooooooooooooooo"<<rp<<endl; ans=s; return; } ll lca(ll x,ll y){ if(dep[x]<dep[y]) swap(x,y); for(ll i=20;i>=0;i--){ ll xx=st[x][i]; if(dep[xx]>=dep[y]) x=xx; } if(x==y) return x; for(ll i=20;i>=0;i--){ ll xx=st[x][i],yy=st[y][i]; if(xx!=yy) x=xx,y=yy; } return st[x][0]; } int main(){ // freopen("P4074_4.in","r",stdin); n=read(),m=read(),q=read(); lenb=pow(n*2.0,2.0/3.0); ll sx=1; for(ll i=1;i<=2*n;i++){ blk[i]=sx; if(i%lenb==0) sx++; } for(ll i=1;i<=m;i++) v[i]=read(); for(ll i=1;i<=n;i++){ w[i]=read(); sum[i]=sum[i-1]+w[i]; } for(ll i=1;i<n;i++){ ll x,y; x=read(),y=read(); e[x].push_back(y); e[y].push_back(x); } for(ll i=1;i<=n;i++) c[i]=read(); for(ll i=1;i<=q;i++){ ll op; op=read(); if(op==0){ cntb++; b[cntb].x=read(),b[cntb].y=read(); } else{ cnta++; a[cnta].l=read(),a[cnta].r=read(); a[cnta].t=cntb,a[cnta].f=cnta; } } dfs(1,0); for(ll i=1;i<=20;i++){ for(ll j=1;j<=n;j++){ st[j][i]=st[st[j][i-1]][i-1]; // cout<<j<<" "<<i<<" "<<st[4][0]<<endl; } } // for(ll i=1;i<=n;i++){ // cout<<fst[i]<<"ooo"<<lst[i]<<endl; // } // return 0; sort(a+1,a+cnta+1,cmp); for(ll i=1;i<=cnta;i++){ ll x=a[i].l,y=a[i].r,lcaa=lca(x,y),xx,yy,ff=0; if(lcaa==x){ ff=1; xx=fst[x],yy=fst[y]; } else if(lcaa==y){ ff=1; xx=fst[y],yy=fst[x]; } else{ if(xl[x]>xl[y]) swap(x,y); xx=lst[x],yy=fst[y]; } // cout<<xx<<" "<<yy<<" "<<ff<<endl; solve(xx,yy,a[i].t,a[i].ans); if(!ff){ ll col=c[lcaa]; // cout<<lcaa<<endl; a[i].ans+=w[cntc[col]+1]*v[col]; } // printf("%lld\n",i); } sort(a+1,a+cnta+1,cmpf); for(ll i=1;i<=cnta;i++) printf("%lld\n",a[i].ans); return 0; } TLE,求调