#include <bits/stdc++.h>
#define int long long
//#warning("nu uita de clear la aint")
using namespace std;
using i64=long long;
using ll=long long;
#define UNUSED -1
#define all(x) x.begin(),x.end()
#define pb push_back
const int mod=1e9+7,inf=1e9+1;
const i64 INF=1e18;
mt19937 rng(69);
const int N=70'000;
struct edge
{
int to,cost;
};
int n,k;
vector<pair<int,int>>sk;
i64 sol[N],h[N];
int subtree[N],carry[N],comp[N];
vector<edge>adj[N];
bitset<N>viz;
map<int, vector<pair<int, int>>> mp;
struct tree {
int sum = 0;
tree *left_child = nullptr, *right_child = nullptr;
void extend(ll left, ll right) {
if (!left_child && left + 1 < right) {
left_child = new tree();
right_child = new tree();
}
}
void add(ll kk, ll x, ll left = 0, ll right = (n + 1) * k)
{
sum += x;
if (left + 1 < right) {
if (kk < (left + right) / 2)
{
if (!left_child) left_child = new tree();
left_child->add(kk, x, left, (left + right) / 2);
}
else
{
if (!right_child) right_child = new tree();
right_child->add(kk, x, (left + right) / 2, right);
}
}
}
int get_sum(ll lq, ll rq, ll left = 0, ll right = (n + 1) * k) {
if (lq <= left && right <= rq)
return sum;
if (max(left, lq) >= min(right, rq))
return 0;
ll answer = 0;
if (left_child)
answer += left_child->get_sum(lq, rq, left, (left + right) / 2);
if (right_child)
answer += right_child->get_sum(lq, rq, (left + right) / 2, right);
return answer;
}
};
tree* aint=nullptr;
void recalc_dfs(int nod,int tt)
{
subtree[nod]=1;
for(auto &v:adj[nod])
{
int c=v.to;
if(c!=tt && !viz[c])
{
recalc_dfs(c,nod);
subtree[nod]+=subtree[c];
}
}
}
int get_centroid(int nod,int tt,int goal)
{
for(auto &v:adj[nod])
{
int c=v.to;
if(c!=tt && !viz[c] && subtree[c]>=goal)
{
return get_centroid(c,nod,goal);
}
}
return nod;
}
//sa fac functie recurstiva de insert
void go_up(int nod,int tt)
{
if(sk.size()<=1)
{
comp[nod]=nod;
}
else
{
comp[nod]=comp[tt];
}
sk.push_back({h[nod],nod});
carry[nod]=subtree[nod]=1;
for(auto &v:adj[nod])
{
int c=v.to;
if(c!=tt && !viz[c])
{
h[c]=h[nod]+v.cost;
go_up(c,nod);
subtree[nod]+=subtree[c];
}
}
if(h[nod]>k)
{
//next_stop_up
auto it=lower_bound(all(sk) , make_pair(h[nod]-k,0LL));
assert(it->first>=h[nod]-k);
assert(prev(it)->first<h[nod]-k);
carry[lower_bound(all(sk) , make_pair(h[nod]-k,0LL))->second]+=carry[nod];
}
else
{
mp[comp[nod]].push_back({k-h[nod],carry[nod]});
}
sk.pop_back();
}
void solve(int nod,int tt,int total)
{
//deocamdata nu opreste nimeni in root
sol[nod]+=1LL*(total-subtree[comp[nod]])*(carry[nod]-1);
for(auto &v:adj[nod])
{
int c=v.to;
if(c!=tt && !viz[c])
{
if(nod==tt)
{
for(auto &v:mp[comp[c]])
{
aint->add(v.first,-v.second);
}
}
i64 coef=aint->get_sum(h[nod],h[c]);
sol[nod]+=coef*subtree[c];
aint->add(h[nod]+k,coef);
solve(c,nod,total);
aint->add(h[nod]+k,-coef);
if(nod==tt)
{
for(auto &v:mp[comp[c]])
{
aint->add(v.first,v.second);
}
}
}
}
}
void decomp(int nod)
{
recalc_dfs(nod,nod);
nod=get_centroid(nod,nod,subtree[nod]/2);
h[nod]=0;
go_up(nod,nod);
aint=new tree;
for(auto &v:mp)
{
for(auto &c:v.second)
{
aint->add(c.first,c.second);
}
}
solve(nod,nod,subtree[nod]);
mp.clear();
viz[nod]=true;
for(auto &c:adj[nod])
{
if(!viz[c.to])
{
decomp(c.to);
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
for(int i=1;i<n;i++)
{
int x,y,z;
cin>>x>>y>>z;
adj[x].pb({y,z});
adj[y].pb({x,z});
}
decomp(0);
for(int i=0;i<n;i++)
{
cout<<sol[i]<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4688 KB |
Output is correct |
2 |
Correct |
1 ms |
4944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4688 KB |
Output is correct |
2 |
Correct |
1 ms |
4944 KB |
Output is correct |
3 |
Correct |
5 ms |
5968 KB |
Output is correct |
4 |
Correct |
5 ms |
5712 KB |
Output is correct |
5 |
Correct |
4 ms |
5968 KB |
Output is correct |
6 |
Correct |
8 ms |
7504 KB |
Output is correct |
7 |
Correct |
4 ms |
5456 KB |
Output is correct |
8 |
Correct |
1 ms |
4688 KB |
Output is correct |
9 |
Correct |
5 ms |
6224 KB |
Output is correct |
10 |
Correct |
5 ms |
6284 KB |
Output is correct |
11 |
Correct |
5 ms |
6224 KB |
Output is correct |
12 |
Correct |
5 ms |
6224 KB |
Output is correct |
13 |
Correct |
5 ms |
6392 KB |
Output is correct |
14 |
Correct |
3 ms |
5456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4688 KB |
Output is correct |
2 |
Correct |
406 ms |
85332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4688 KB |
Output is correct |
2 |
Correct |
1 ms |
4944 KB |
Output is correct |
3 |
Correct |
1 ms |
4688 KB |
Output is correct |
4 |
Correct |
406 ms |
85332 KB |
Output is correct |
5 |
Correct |
2017 ms |
1048456 KB |
Output is correct |
6 |
Correct |
2341 ms |
1031772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4688 KB |
Output is correct |
2 |
Correct |
1 ms |
4944 KB |
Output is correct |
3 |
Correct |
349 ms |
67468 KB |
Output is correct |
4 |
Correct |
515 ms |
104892 KB |
Output is correct |
5 |
Correct |
364 ms |
59684 KB |
Output is correct |
6 |
Correct |
1 ms |
4688 KB |
Output is correct |
7 |
Correct |
406 ms |
76784 KB |
Output is correct |
8 |
Correct |
380 ms |
76872 KB |
Output is correct |
9 |
Correct |
382 ms |
76872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4688 KB |
Output is correct |
2 |
Correct |
1 ms |
4944 KB |
Output is correct |
3 |
Correct |
349 ms |
67468 KB |
Output is correct |
4 |
Correct |
515 ms |
104892 KB |
Output is correct |
5 |
Correct |
364 ms |
59684 KB |
Output is correct |
6 |
Correct |
1 ms |
4688 KB |
Output is correct |
7 |
Correct |
406 ms |
76784 KB |
Output is correct |
8 |
Correct |
380 ms |
76872 KB |
Output is correct |
9 |
Correct |
382 ms |
76872 KB |
Output is correct |
10 |
Correct |
242 ms |
64064 KB |
Output is correct |
11 |
Correct |
302 ms |
62540 KB |
Output is correct |
12 |
Correct |
251 ms |
58724 KB |
Output is correct |
13 |
Correct |
281 ms |
67508 KB |
Output is correct |
14 |
Correct |
257 ms |
67400 KB |
Output is correct |
15 |
Correct |
261 ms |
67344 KB |
Output is correct |
16 |
Correct |
137 ms |
50616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4688 KB |
Output is correct |
2 |
Correct |
1 ms |
4944 KB |
Output is correct |
3 |
Correct |
5 ms |
5968 KB |
Output is correct |
4 |
Correct |
5 ms |
5712 KB |
Output is correct |
5 |
Correct |
4 ms |
5968 KB |
Output is correct |
6 |
Correct |
8 ms |
7504 KB |
Output is correct |
7 |
Correct |
4 ms |
5456 KB |
Output is correct |
8 |
Correct |
1 ms |
4688 KB |
Output is correct |
9 |
Correct |
5 ms |
6224 KB |
Output is correct |
10 |
Correct |
5 ms |
6284 KB |
Output is correct |
11 |
Correct |
5 ms |
6224 KB |
Output is correct |
12 |
Correct |
5 ms |
6224 KB |
Output is correct |
13 |
Correct |
5 ms |
6392 KB |
Output is correct |
14 |
Correct |
3 ms |
5456 KB |
Output is correct |
15 |
Correct |
1 ms |
4688 KB |
Output is correct |
16 |
Correct |
406 ms |
85332 KB |
Output is correct |
17 |
Correct |
2017 ms |
1048456 KB |
Output is correct |
18 |
Correct |
2341 ms |
1031772 KB |
Output is correct |
19 |
Correct |
349 ms |
67468 KB |
Output is correct |
20 |
Correct |
515 ms |
104892 KB |
Output is correct |
21 |
Correct |
364 ms |
59684 KB |
Output is correct |
22 |
Correct |
1 ms |
4688 KB |
Output is correct |
23 |
Correct |
406 ms |
76784 KB |
Output is correct |
24 |
Correct |
380 ms |
76872 KB |
Output is correct |
25 |
Correct |
382 ms |
76872 KB |
Output is correct |
26 |
Correct |
242 ms |
64064 KB |
Output is correct |
27 |
Correct |
302 ms |
62540 KB |
Output is correct |
28 |
Correct |
251 ms |
58724 KB |
Output is correct |
29 |
Correct |
281 ms |
67508 KB |
Output is correct |
30 |
Correct |
257 ms |
67400 KB |
Output is correct |
31 |
Correct |
261 ms |
67344 KB |
Output is correct |
32 |
Correct |
137 ms |
50616 KB |
Output is correct |
33 |
Correct |
1916 ms |
991752 KB |
Output is correct |
34 |
Correct |
749 ms |
322900 KB |
Output is correct |
35 |
Correct |
849 ms |
389524 KB |
Output is correct |
36 |
Correct |
863 ms |
383816 KB |
Output is correct |
37 |
Correct |
1163 ms |
470632 KB |
Output is correct |
38 |
Correct |
1098 ms |
476784 KB |
Output is correct |
39 |
Correct |
1142 ms |
479652 KB |
Output is correct |
40 |
Correct |
416 ms |
145808 KB |
Output is correct |