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
|
#include <bitset> #include <cstdio> #include <vector> #include <utility> #include <cassert> #include <algorithm>
using namespace std;
const auto U = [](auto x) { return [x](auto ...y) { return x(x, y...); }; };
class fenwick { public: long long st[400005]; void vadd(int pos, long long val, int n) { pos++; n++; do { st[pos] += val; } while((pos += pos & -pos) <= n); } long long qsum(int pos) { pos++; long long s = 0; do { s += st[pos]; } while(pos -= pos & -pos); return s; } } fw;
int t[400005]; vector<int> web[400005]; bitset<400005> vis; int dep[400005]; int dcnt1[400005], dcnt2[400005]; long long ans[400005];
int main() { int n, d; scanf("%d%d", &n, &d); for(int i=1;i<=n;i++) { scanf("%d", t + i); t[i] = min(t[i], n); } for(int i=1;i<n;i++) { int u, v; scanf("%d%d", &u, &v); web[u].push_back(v); web[v].push_back(u); } auto cent = [](int x) { int s = U([&](auto &&self, int u, int fa) -> int { int res = 1; for(int v : web[u]) if(v != fa && !vis[v]) res += self(self, v, u); return res; })(x, -1); return -U([&](auto &&self, int u, int fa) -> int { int sum = 1, maxn = 0; for(int v : web[u]) if(v != fa && !vis[v]) { int res = self(self, v, u); if(res < 0) return res; maxn = max(maxn, res); sum += res; } maxn = max(maxn, s - sum); if(maxn <= s / 2) return -u; else return sum; })(x, -1); }; U([&](auto &&self, int u) -> void { u = cent(u); dep[u] = 0; U([&](auto &&sel, int x, int fa) -> void { for(int y : web[x]) if(y != fa && !vis[y]) { dep[y] = dep[x] + 1; sel(sel, y, x); } })(u, -1); auto vcalc = [&](int x) {
vector<pair<int, int>> vins; U([&](auto &&sel, int a, int fa, int minn) -> void { if(dep[a] > d) return; minn = min(minn, t[a] - dep[a]); vins.push_back({dep[a], minn}); for(int b : web[a]) { if(b != fa && !vis[b]) sel(sel, b, a, minn); } })(x, -1, t[u] + dep[u]); class qry { public: int id, dmax, vmax, fctr; }; vector<qry> vq; int bfc = (x == u ? 1 : -1); U([&](auto &&sel, int a, int fa, int minn) -> void { if(dep[a] > d) return; minn = min(minn, t[a] + dep[a]); if(minn <= dep[a]) { vq.push_back({a, d - dep[a], 0x3f3f3f3f, bfc}); } else vq.push_back({a, d - dep[a], dep[a], bfc}); for(int b : web[a]) { if(b != fa && !vis[b]) sel(sel, b, a, minn); } })(x, -1, t[u] + dep[u]); sort(vq.begin(), vq.end(), [](const auto &x, const auto &y) { return x.dmax < y.dmax; }); sort(vins.begin(), vins.end(), [](const auto &x, const auto &y) { return x.first < y.first; }); int cur = 0; for(const auto &[id, dmax, vmax, fctr] : vq) { while(cur < vins.size() && vins[cur].first <= dmax) fw.vadd(max(0, vins[cur++].second), 1, n + 5); ans[id] += fctr * (vmax < 0 ? 0 : fw.qsum(min(n + 1, vmax))); } while(cur > 0) fw.vadd(max(0, vins[--cur].second), -1, n + 5); }; vcalc(u); vis[u] = true; for(int v : web[u]) { if(!vis[v]) vcalc(v); } for(int v : web[u]) { if(!vis[v]) self(self, v); } })(1); for(int i=1;i<=n;i++) printf("%lld\n", ans[i]); return 0; }
|