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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| #include <bits/stdc++.h> #define LL long long #define VS vector<Son> using namespace std; const int N = 1.5e5 + 5; int n, m, A; int h[N], idx; struct Edge { int ne, ver, w; } e[N << 1]; struct Father { int x, id; LL dis; }; struct Son { int age; LL dis; bool operator<(const Son &t) const { return age < t.age; } }; vector<Father> f[N]; VS s[N][3]; bool del[N]; int age[N]; void add(int x, int y, int z) { e[idx] = (Edge){h[x], y, z}, h[x] = idx++; } int get_si(int x, int fa) { if (del[x]) return 0; int res = 1; for (int i = h[x]; i != -1; i = e[i].ne) if (e[i].ver != fa) res += get_si(e[i].ver, x); return res; } int get_wc(int x, int fa, int si, int &wc) { if (del[x]) return 0; int sum = 1, mx = 0; for (int i = h[x], t; i != -1; i = e[i].ne) { if (e[i].ver == fa) continue; t = get_wc(e[i].ver, x, si, wc); mx = max(mx, t); sum += t; } mx = max(mx, si - sum); if (mx <= si / 2) wc = x; return sum; } void get_dis(int x, int fa, LL dis, int wc, int k, VS &p) { if (del[x]) return; f[x].push_back((Father){wc, k, dis}); p.push_back((Son){age[x], dis}); for (int i = h[x], t; i != -1; i = e[i].ne) if (e[i].ver != fa) get_dis(e[i].ver, x, dis + e[i].w, wc, k, p); } void calc(int x) { if (del[x]) return; get_wc(x, -1, get_si(x, -1), x); del[x] = true; for (int i = h[x], y, k = 0; i != -1; i = e[i].ne) { y = e[i].ver; if (del[y]) continue; VS &p = s[x][k]; p.push_back((Son){-1, 0}), p.push_back((Son){A + 1, 0}); get_dis(y, -1, e[i].w, x, k, p); sort(p.begin(), p.end()); for (int i = 1; i < p.size(); ++i) p[i].dis += p[i - 1].dis; ++k; } for (int i = h[x]; i != -1; i = e[i].ne) calc(e[i].ver); } LL ask(int x, int l, int r) { LL res = 0; for (Father &i : f[x]) { int g = age[i.x]; if (g >= l && g <= r) res += i.dis; for (int j = 0; j < 3; ++j) { if (j == i.id) continue; VS &p = s[i.x][j]; if (p.empty()) continue; int a = lower_bound(p.begin(), p.end(), (Son){l, -1}) - p.begin(); int b = lower_bound(p.begin(), p.end(), (Son){r + 1, -1}) - p.begin(); res += i.dis * (b - a) + p[b - 1].dis - p[a - 1].dis; } } for (int i = 0; i < 3; ++i) { VS &p = s[x][i]; if (p.empty()) continue; int a = lower_bound(p.begin(), p.end(), (Son){l, -1}) - p.begin(); int b = lower_bound(p.begin(), p.end(), (Son){r + 1, -1}) - p.begin(); res += p[b - 1].dis - p[a - 1].dis; } return res; } int main() { scanf("%d%d%d", &n, &m, &A); for (int i = 1; i <= n; ++i) h[i] = -1; idx = 0; for (int i = 1; i <= n; ++i) scanf("%d", &age[i]); for (int i = 1, u, v, w; i < n; ++i) { scanf("%d%d%d", &u, &v, &w); add(u, v, w), add(v, u, w); } calc(1); LL ans = 0; int u, a, b, l, r; while (m--) { scanf("%d%d%d", &u, &a, &b); l = (a + ans) % A, r = (b + ans) % A; if (l > r) swap(l, r); ans = ask(u, l, r); printf("%lld\n", ans); } return 0; }
|