-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCF100570F.cpp
154 lines (154 loc) · 3.54 KB
/
CF100570F.cpp
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
#include <algorithm>
#include <cstdio>
#include <vector>
typedef long long Int64;
Int64 read() {
Int64 res = 0, c;
do
c = getchar();
while(c < '0' || c > '9');
while('0' <= c && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
return res;
}
const int size = 100005;
struct Edge {
int to, nxt, w;
} E[size * 2];
int last[size], cnt = 0;
void addEdge(int u, int v, int w) {
++cnt;
E[cnt].to = v, E[cnt].nxt = last[u], E[cnt].w = w;
last[u] = cnt;
}
int siz[size], nrt, cmsiz, tsiz;
bool vis[size];
void getRootImpl(int u, int p) {
siz[u] = 1;
int msiz = 0;
for(int i = last[u]; i; i = E[i].nxt) {
int v = E[i].to;
if(v == p || vis[v])
continue;
getRootImpl(v, u);
siz[u] += siz[v];
msiz = std::max(msiz, siz[v]);
}
msiz = std::max(msiz, tsiz - siz[u]);
if(cmsiz > msiz)
cmsiz = msiz, nrt = u;
}
int getRoot(int u, int usiz) {
cmsiz = 1 << 30, tsiz = usiz;
getRootImpl(u, 0);
return nrt;
}
int S[size], maxs;
void modify(int x, int op) {
while(x <= maxs) {
S[x] += op;
x += x & -x;
}
}
int query(int x) {
int res = 0;
while(x) {
res += S[x];
x -= x & -x;
}
return res;
}
int pcnt;
Int64 dis[size], cdis[size];
int find(Int64 d) {
return std::upper_bound(cdis + 1, cdis + maxs + 1,
d) -
cdis - 1;
}
void DFS(int u, int p, Int64 d) {
dis[++pcnt] = d;
for(int i = last[u]; i; i = E[i].nxt) {
int v = E[i].to;
if(v == p || vis[v])
continue;
DFS(v, u, d + E[i].w);
}
}
std::vector<std::pair<int, Int64> > Q[size];
int ans[size];
void calc(int u, int p, Int64 d) {
for(int i = 0; i < Q[u].size(); ++i)
ans[Q[u][i].first] +=
query(find(Q[u][i].second - d));
for(int i = last[u]; i; i = E[i].nxt) {
int v = E[i].to;
if(v == p || vis[v])
continue;
calc(v, u, d + E[i].w);
}
}
void solve(int u) {
pcnt = 0;
DFS(u, 0, 0);
std::sort(dis + 1, dis + pcnt + 1);
maxs = std::unique_copy(dis + 1, dis + pcnt + 1,
cdis + 1) -
(cdis + 1);
for(int i = 1; i <= maxs; ++i)
S[i] = 0;
for(int i = 1; i <= pcnt; ++i)
++S[find(dis[i])];
for(int i = 1; i <= maxs; ++i) {
int j = i + (i & -i);
if(j <= maxs)
S[j] += S[i];
}
for(int i = 0; i < Q[u].size(); ++i)
ans[Q[u][i].first] +=
query(find(Q[u][i].second));
for(int i = last[u]; i; i = E[i].nxt) {
int v = E[i].to;
if(vis[v])
continue;
pcnt = 0;
DFS(v, u, E[i].w);
for(int j = 1; j <= pcnt; ++j)
modify(find(dis[j]), -1);
calc(v, u, E[i].w);
for(int j = 1; j <= pcnt; ++j)
modify(find(dis[j]), 1);
}
}
void divide(int u) {
vis[u] = true;
solve(u);
for(int i = last[u]; i; i = E[i].nxt) {
int v = E[i].to;
if(!vis[v]) {
int crt = getRoot(v, siz[v]);
divide(crt);
}
}
}
int main() {
int n = read();
int q = read();
for(int i = 1; i < n; ++i) {
int u = read();
int v = read();
int w = read();
addEdge(u, v, w);
addEdge(v, u, w);
}
for(int i = 1; i <= q; ++i) {
int u = read();
Int64 l = read();
Q[u].push_back(std::make_pair(i, l));
}
divide(getRoot(1, n));
for(int i = 1; i <= q; ++i)
printf("%d\n", ans[i]);
return 0;
}