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 152 153 154 155 156 157 158 159 160 161 162
| #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <vector> #include <cctype> #include <bitset>
using namespace std;
typedef long long ll;
const int maxn = 1e+5 + 5e+4 + 5;
struct edge { int to, nxt, v; }e[maxn << 1]; struct anc { ll dis; int pnt, id; anc(ll d_ = 0, int p_ = 0, int i_ = 0): dis(d_), pnt(p_), id(i_){} }; vector<anc> node_anc[maxn]; struct dat { int age; ll sumdis, sumsiz; dat(int w = 0, ll d_ = 0, ll sz = 0): age(w), sumdis(d_), sumsiz(sz){} bool operator < (const dat &rhs) const { return age < rhs.age; } }; vector<dat> ans[maxn][3];
typedef vector<dat>::iterator iter;
int ptr, lnk[maxn], siz[maxn], a[maxn], sum, glbmn, rt; int n, q; ll A, lstans; ll dep[maxn]; bitset<maxn> vis, cut;
inline ll rdll() { register ll x = 0; char c = getchar(); while(!isdigit(c)) c = getchar(); while(isdigit(c)) x = x * 10 + (c ^ 48), c = getchar(); return x; } inline int rd() { register int x = 0; char c = getchar(); while(!isdigit(c)) c = getchar(); while(isdigit(c)) x = x * 10 + (c ^ 48), c = getchar(); return x; } inline void add(int bgn, int end, int val) { e[++ptr] = (edge){end, lnk[bgn], val}; lnk[bgn] = ptr; } void dfs(int x) { vis[x] = 1; siz[x] = 1; for(int p = lnk[x]; p; p = e[p].nxt) { int y = e[p].to; if(!vis[y] && !cut[y]) dfs(y), siz[x] += siz[y]; } vis[x] = 0; } void efs(int x) { vis[x] = 1; int mx = sum - siz[x]; for(int p = lnk[x]; p; p = e[p].nxt) { int y = e[p].to; if(!vis[y] && !cut[y]) efs(y), mx = max(mx, siz[y]); } vis[x] = 0; if(mx < glbmn) glbmn = mx, rt = x; } void ffs(int x, const int &ast, const int &type_) { vis[x] = 1; node_anc[x].push_back(anc(dep[x], ast, type_)); ans[ast][type_].push_back(dat(a[x], dep[x], 1)); for(int p = lnk[x]; p; p = e[p].nxt) { int y = e[p].to; if(!vis[y] && !cut[y]) dep[y] = dep[x] + e[p].v, ffs(y, ast, type_); } vis[x] = 0; } void conquer(int x) { dfs(x); if(siz[x] == 1) { cut[x] = 1; node_anc[x].push_back(anc(0, x, -1)); return; } sum = siz[x]; glbmn = 0x3f3f3f3f; efs(x); cut[rt] = 1; node_anc[rt].push_back(anc(0, rt, -1)); int t = 0; for(int p = lnk[rt]; p; p = e[p].nxt) { int y = e[p].to; if(cut[y]) continue; dep[y] = e[p].v; ffs(y, rt, t); ans[rt][t].push_back(dat(0x3f3f3f3f, 0, 0)); sort(ans[rt][t].begin(), ans[rt][t].end()); for(int j = ans[rt][t].size() - 2; j >= 0; --j) { ans[rt][t][j].sumdis += ans[rt][t][j+1].sumdis; ans[rt][t][j].sumsiz += ans[rt][t][j+1].sumsiz; } t++; } for(int p = lnk[rt]; p; p = e[p].nxt) if(!cut[e[p].to]) conquer(e[p].to); } void query(int l, int r, int u) { lstans = 0; iter L, R; for(int i = node_anc[u].size()-1; i >= 0; --i) { int f = node_anc[u][i].pnt; for(int type_ = 0; type_ < 3; ++type_) { if(type_ == node_anc[u][i].id || ans[f][type_].empty()) continue; L = lower_bound(ans[f][type_].begin(), ans[f][type_].end(), dat(l, 0, 0)); R = upper_bound(ans[f][type_].begin(), ans[f][type_].end(), dat(r, 0, 0)); lstans += (*L).sumdis - (*R).sumdis + node_anc[u][i].dis * (L->sumsiz - R->sumsiz); } if(l <= a[f] && a[f] <= r) lstans += node_anc[u][i].dis; } }
int main() { scanf("%d%d%lld", &n, &q, &A); for(int i = 1; i <= n; ++i) a[i] = rd(); register int x, y, z; for(int i = 1; i < n; ++i) x = rd(), y = rd(), z = rd(), add(x, y, z), add(y, x, z); conquer(1); register int u; register ll l, r; while(q--) { u = rd(); l = rdll(); r = rdll(); l = (l + lstans) % A; r = (r + lstans) % A; if(l > r) swap(l, r); query(l, r, u); printf("%lld\n", lstans); } return 0; }
|