满足 n<=150000,Q<=200000。对于所有数据,满足 A<=10^9
动态树分治,先把分治过程中的值记录下来,询问的时候把之前记录下来的值统计一下即可。
第一发用了两个map,结果68s险过,改成vector加二分快了整整一半。
#include<map>
#include<vector>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 3e5 + 10;
const int INF = 0x7FFFFFFF;
int N, Q, A, age[maxn], x, y, z, l, r;
struct Tree
{
struct point
{
int x, y;
point(int x = 0, int y = 0) :x(x), y(y) {}
bool operator < (const point &a)const
{
return x < a.x;
}
};
int ft[maxn], nt[maxn], u[maxn], v[maxn], sz;
int mx[maxn], ct[maxn], vis[maxn];
int pre[maxn], ch[maxn][3];
vector<point> t[maxn][3], dis[maxn];
vector<LL> s[maxn][3];
void clear(int n)
{
mx[sz = 0] = INF;
for (int i = 1; i <= n; i++)
{
ft[i] = -1; vis[i] = 0;
dis[i].clear();
for (int j = 0; j < 3; j++)
{
t[i][j].clear();
s[i][j].clear();
}
}
}
void AddEdge(int x, int y, int z)
{
u[sz] = y; v[sz] = z; nt[sz] = ft[x]; ft[x] = sz++;
u[sz] = x; v[sz] = z; nt[sz] = ft[y]; ft[y] = sz++;
}
int dfs(int x, int fa, int sum)
{
int y = mx[x] = (ct[x] = 1) - 1;
for (int i = ft[x]; i != -1; i = nt[i])
{
if (u[i] == fa || vis[u[i]]) continue;
int z = dfs(u[i], x, sum);
ct[x] += ct[u[i]];
mx[x] = max(mx[x], ct[u[i]]);
y = mx[y] < mx[z] ? y : z;
}
mx[x] = max(mx[x], sum - ct[x]);
return mx[x] < mx[y] ? x : y;
}
void find(int x, int fa, int rt, int id, int len)
{
dis[rt].push_back(point(x, len));
t[rt][id].push_back(point(age[x], len));
for (int i = ft[x]; i != -1; i = nt[i])
{
if (vis[u[i]] || u[i] == fa) continue;
find(u[i], x, rt, id, len + v[i]);
}
}
int build(int x, int sum, int fa)
{
int y = dfs(x, -1, sum);
pre[y] = fa; vis[y] = 1;
int id = 0; dis[y].push_back(point(y, 0));
for (int i = ft[y]; i != -1; i = nt[i],id++)
{
if (vis[u[i]]) continue;
find(u[i], y, y, id, v[i]);
sort(t[y][id].begin(), t[y][id].end());
for (LL j = 0, k = 0; j < t[y][id].size(); j++)
{
k += t[y][id][j].y;
s[y][id].push_back(k);
}
ch[y][id] = build(u[i], ct[u[i]] > ct[y] ? sum - ct[y] : ct[u[i]], y);
}
sort(dis[y].begin(), dis[y].end());
return y;
}
LL get(int rt, int x, int l, int r, int from)
{
if (x == -1) return 0;
LL ans = 0, sum = 0;
for (int i = 0; i < 3; i++)
{
if (i == from || t[x][i].empty()) continue;
int L = lower_bound(t[x][i].begin(), t[x][i].end(), point(l, 0)) - t[x][i].begin();
int R = upper_bound(t[x][i].begin(), t[x][i].end(), point(r, 0)) - t[x][i].begin();
ans += (R ? s[x][i][R - 1] : 0) - (L ? s[x][i][L - 1] : 0);
sum += R - L;
}
int d = dis[x][lower_bound(dis[x].begin(), dis[x].end(), point(rt, 0)) - dis[x].begin()].y;
ans = ans + (sum + (age[x] >= l&&age[x] <= r))*d;
for (int i = 0; i < 3; i++) if (pre[x] != -1 && ch[pre[x]][i] == x) from = i;
return ans + get(rt, pre[x], l, r, from);
}
}solve;
int main()
{
while (scanf("%d%d%d", &N, &Q, &A) != EOF)
{
solve.clear(N);
for (int i = 1; i <= N; i++) scanf("%d", &age[i]);
for (int i = 1; i < N; i++)
{
scanf("%d%d%d", &x, &y, &z);
solve.AddEdge(x, y, z);
}
solve.build(1, N, -1);
LL get = 0;
while (Q--)
{
scanf("%d%d%d", &x, &y, &z);
l = min((get + y) % A, (get + z) % A);
r = max((get + y) % A, (get + z) % A);
get = solve.get(x, x, l, r, -1);
printf("%lld\n", get);
}
}
return 0;
}