P3241 [HNOI2015]开店

在线询问树上$\sum_{i=1}^n dis(x,i),a[i]\in[l,r]$。
$degree\leq 3$

显然$ans=query(x,r)-query(x,l-1)$

查询一颗$u$子树上是,$dis(u,x)\times Num[u][r]+sum[u][r]-u关于x那颗子树$。

由于点的度不大,暴力计算$3$颗子树的贡献。

  • 统计$sum[u][r]$,可以排好序,计算前缀和即可。
  • 需要关系$num[u]+=(a[u]<=r)$
  • 开始的时候需要计算$x$子树的贡献
代码
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, ll>
#define mk make_pair
const int N = 4e5 + 10;
const int mod = 1e9 + 7;

int read()
{
int 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 << 1) + (x << 3) + c - '0', c = getchar();
return x * f;
}

vector<pii> g[N];
int vis[N];
int st[N][20], dfn[N], dep[N], tot, lg2[N];
ll dw[N];
void lcadfs(int x, int fa, int d)
{
st[++tot][0] = x;
dep[x] = d;
dfn[x] = tot;
for (pii now : g[x])
{
int to = now.first;
if (to == fa)
continue;
dw[to] = dw[x] + now.second;
lcadfs(to, x, d + 1);
st[++tot][0] = x;
}
}
int lower(int u, int v)
{
return (dep[u] < dep[v]) ? u : v;
}

void Lca_init()
{
lcadfs(1, 0, 1);
for (int i = 2; i <= tot; i++)
lg2[i] = lg2[i >> 1] + 1;
for (int l = 1; (1 << l) <= tot; l++)
{
for (int i = 1; i + (1 << l) - 1 <= tot; i++)
st[i][l] = lower(st[i][l - 1], st[i + (1 << (l - 1))][l - 1]);
}
}

int lca(int u, int v)
{
u = dfn[u];
v = dfn[v];
if (u > v)
swap(u, v);
int i = lg2[v - u + 1], w = (1 << i);
return lower(st[u][i], st[v - w + 1][i]);
}
ll dis(int u, int v)
{
return dw[u] + dw[v] - 2 * dw[lca(u, v)];
}
int siz[N], f[N];
int father[N], a[N];
int cnt[N], np[N];
int rt, gcnt;
vector<pii> fw[N][3];
vector<ll> sum[N][3];
void GetRoot(int x, int fa)
{
siz[x] = 1;
f[x] = 0;
for (pii now : g[x])
{
int to = now.first;
if (to == fa || vis[to])
continue;
GetRoot(to, x);
siz[x] += siz[to];
f[x] = max(f[x], siz[to]);
}
f[x] = max(f[x], gcnt - siz[x]);
if (f[x] < f[rt])
rt = x;
}

void dfs1(int x, int fa, int rt)
{
fw[rt][cnt[rt]].push_back(mk(a[x], dis(rt, x)));
for (pii now : g[x])
{
int to = now.first;
if (to == fa || vis[to])
continue;
dfs1(to, x, rt);
}
}
void solve(int x)
{

vis[x] = 1;

dfs1(x, 0, father[x]);
np[x] = cnt[father[x]];
cnt[father[x]]++;
for (pii now : g[x])
{
int to = now.first;
if (vis[to])
continue;
gcnt = siz[to];
rt = 0;

GetRoot(to, x);
father[rt] = x;
solve(rt);
}
}
void Prepare(int n)
{
for (int i = 0; i <= n; i++)
if (cnt[i])
{
for (int j = 0; j < cnt[i]; j++)
{

sort(fw[i][j].begin(), fw[i][j].end());

sum[i][j].push_back(fw[i][j][0].second);
for (int t = 1; t < fw[i][j].size(); t++)
sum[i][j].push_back(fw[i][j][t].second + sum[i][j][t - 1]);
}
}
}
pii calc(int x, int u, int r)
{

pii res = mk(a[x] <= r, 0);

for (int i = 0; i < cnt[x]; i++)
{
if (i == u)
continue;

int it = upper_bound(fw[x][i].begin(), fw[x][i].end(), mk(r + 1, 0ll)) - fw[x][i].begin();
it--;
it = min((int)(fw[x][i].size() - 1), it);
res.first += it + 1;
if (it >= 0)
res.second += sum[x][i][it];
}
return res;
}
ll query(int u, int r)
{
if (r < 0)
return 0;
int now = u;

pii o = calc(now, -1, r);
ll res = o.second;

while (father[now])
{

o = calc(father[now], np[now], r);

res += o.first * dis(father[now], u);
res += o.second;
now = father[now];
}
return res;
}
int main()
{
int n = read(), q = read(), A = read();
for (int i = 1; i <= n; i++)
a[i] = read();
for (int i = 1; i < n; i++)
{
int u = read(), v = read(), w = read();
g[u].push_back(pii(v, w));
g[v].push_back(mk(u, w));
}
Lca_init();
rt = 0;
f[0] = 1e9;
gcnt = n;
GetRoot(1, 0);

solve(rt);
Prepare(n);
ll ans = 0;
for (int i = 1; i <= q; i++)
{
int u = read(), a = read(), b = read();
a = (a + ans) % A;
b = (b + ans) % A;
if (a > b)
swap(a, b);
printf("%lld\n", (ans = (query(u, b) - query(u, a - 1))));
}
}