The meaning of problems
A given size \ (n-\) point weighted unrooted trees, \ (Q \) a query, asking each node with \ (U \) is no more than \ (K \) node and the right of point
\ (K \ leq 400, q \ leq 5000, n \ leq 10 ^ 6 \)
solution
Inquiry node \ (U \) is no more than \ (K \) points right node, consider the source of these points, one from within its subtree, two chains derived from its ancestors
Suppose we find the root to a node, a node for \ (U \) , which does not exceed the distance within its subtree \ (K \) weights and point \ (f_u [k] \)
Then we solved the first source of these points
Next statistics on their answers to the chain of ancestors: You can use a small inclusion and exclusion, for the node \ (u \) of \ (t \) bit ancestors \ (v \) , its contribution to the answer is \ (f_v [KT] -f_ son_v} {[-KT. 1] \) , where \ (son_v \) refers to the \ (V \) son on this chain
How to consider seeking \ (f \) array
Since only asked not modified, we consider off-line processing, in accordance with (dfs \) \ sort order
The whole pieces of tree abstraction called a two-dimensional plane, where the horizontal axis is \ (DFS \) sequence, the vertical axis represents the depth
Since a point within the sub-tree, the depth is continuous, \ (DFS \) sequence is continuous, the equivalent weight of each query within the query point and a rectangle
We call this rectangle about open borders, what a difference you can find the right point of the rectangle within this value
If you can not understand look at the code should be able to understand
Code
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int N = 2e6 + 10;
struct node { int x, v, id; };
int n, q;
int cnt;
int fa[N], mp[N], dep[N], dfn[N], sz[N];
int cap;
int head[N], to[N << 1], nxt[N << 1];
long long p[N], ans[N];
vector<node> g[N];
struct BIT {
long long c[N];
BIT() { memset(c, 0, sizeof c); }
void insert(int x, long long v) {
for (; x && x <= n; x += x & -x) c[x] += v;
}
long long query(int x) {
x = min(x, n);
long long res = 0;
for (; x; x -= x & -x) res += c[x];
return res;
}
} bit;
template<typename _T> void read(_T& x) {
int c = getchar(); x = 0;
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
}
inline void add(int x, int y) { to[++cap] = y, nxt[cap] = head[x], head[x] = cap; }
void DFS(int x) {
dfn[x] = ++cnt, mp[cnt] = x, sz[x] = 1;
for (int i = head[x]; i; i = nxt[i])
if (to[i] != fa[x])
dep[to[i]] = dep[x] + 1, fa[to[i]] = x, DFS(to[i]), sz[x] += sz[to[i]];
}
int main() {
read(n);
for (int i = 1; i <= n; ++i) read(p[i]);
int u, v;
for (int i = 1; i < n; ++i) {
read(u), read(v);
add(u, v), add(v, u);
}
dep[1] = 1;
DFS(1);
read(q);
int x, k;
for (int i = 1; i <= q; ++i) {
read(x), read(k);
int p = x, lst = 0;
while (p && k >= 0) {
int pos = dfn[p];
g[pos - 1].push_back((node){dep[p] + k, -1, i});
g[pos + sz[p] - 1].push_back((node){dep[p] + k, 1, i});
if ((p ^ x) && k) {
pos = dfn[lst];
g[pos - 1].push_back((node){dep[lst] + k - 1, 1, i});
g[pos + sz[lst] - 1].push_back((node){dep[lst] + k - 1, -1, i});
}
lst = p, k--;
p = fa[p];
}
}
for (int i = 1; i <= n; ++i) {
bit.insert(dep[mp[i]], p[mp[i]]);
for (int j = 0; j < g[i].size(); ++j)
ans[g[i][j].id] += 1LL * g[i][j].v * bit.query(g[i][j].x);
}
for (int i = 1; i <= q; ++i) printf("%lld\n", ans[i]);
return 0;
}