1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
#include <cstdio> #include <vector> #include <cstring> #include <algorithm> #ifdef LB_IS_TIMING #include <chrono> #endif
using namespace std;
const auto U = [](auto x) { return [x](auto ...y) { return x(x, y...); }; };
class mp5matrix { public: long long val[5][5]; friend mp5matrix operator*(const mp5matrix &x, const mp5matrix &y) { mp5matrix res; memset(&res, -0x3f, sizeof res); for(int i=0;i<5;i++) { for(int k=0;k<5;k++) { for(int j=0;j<5;j++) { res.val[i][j] = max(res.val[i][j], x.val[i][k] + y.val[k][j]); } } } return res; }; } tofa[20][100005], afot[20][100005];
int c[100005], p[100005]; vector<int> ch[100005]; int fa[20][100005]; int dep[100005];
int main() { #ifdef LB_IS_TIMING auto bgn = chrono::high_resolution_clock::now(); #endif int n, q; scanf("%d%d", &n, &q); for(int i=1;i<=n;i++) { scanf("%d", c + i); } for(int i=1;i<=n;i++) { scanf("%d", p + i); } for(int i=2;i<=n;i++) { int x, y; scanf("%d%d", &x, &y); ch[x].push_back(y); ch[y].push_back(x); } dep[1] = 1; U([&](auto &&self, int u) -> void { if(fa[0][u]) ch[u].erase(find(ch[u].begin(), ch[u].end(), fa[0][u])); mp5matrix vm; memset(&vm, -0x3f, sizeof vm); vm.val[0][1] = vm.val[1][2] = vm.val[2][3] = vm.val[3][4] = p[u]; vm.val[1][0] = vm.val[2][1] = vm.val[3][2] = vm.val[4][3] = c[u]; tofa[0][u] = afot[0][u] = vm; for(int i=1;(1<<i)<=dep[u];i++) { tofa[i][u] = tofa[i-1][u] * tofa[i-1][fa[i-1][u]]; afot[i][u] = afot[i-1][fa[i-1][u]] * afot[i-1][u]; fa[i][u] = fa[i-1][fa[i-1][u]]; } for(int v : ch[u]) { dep[v] = dep[u] + 1; fa[0][v] = u; self(self, v); } })(1); auto LCA = [](int x, int y) { for(int i=16;i>=0;i--) { if(dep[x] >= dep[y] + (1 << i)) x = fa[i][x]; if(dep[y] >= dep[x] + (1 << i)) y = fa[i][y]; } for(int i=16;i>=0;i--) if(fa[i][x] != fa[i][y]) { x = fa[i][x]; y = fa[i][y]; } return x == y ? x : fa[0][x]; }; auto multofa = [](int x, int len) { mp5matrix res; memset(&res, -0x3f, sizeof res); res.val[0][0] = res.val[1][1] = res.val[2][2] = res.val[3][3] = res.val[4][4] = 0; if(len == 0) return res; for(int i=16;i>=0;i--) if(len&(1<<i)) { res = res * tofa[i][x]; x = fa[i][x]; } return res; }; auto mulafto = [](int x, int len) { mp5matrix res; memset(&res, -0x3f, sizeof res); res.val[0][0] = res.val[1][1] = res.val[2][2] = res.val[3][3] = res.val[4][4] = 0; if(len == 0) return res; for(int i=16;i>=0;i--) if(len&(1<<i)) { res = afot[i][x] * res; x = fa[i][x]; } return res; }; while(q--) { int u, v; scanf("%d%d", &u, &v); if(u == v) { printf("%d\n", max(p[u], c[u])); continue; } int w = LCA(u, v); int d1 = dep[u] - dep[w], d2 = dep[v] - dep[w]; mp5matrix mv = multofa(v, d2) * tofa[0][w] * mulafto(u, d1); printf("%lld\n", max({mv.val[0][2], mv.val[1][2], mv.val[2][2], mv.val[3][2], mv.val[4][2]})); } #ifdef LB_IS_TIMING fprintf(stderr, "Time: %llu ms\n", chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - bgn).count()); #endif return 0; }
|