// author : black_trees#include<cmath>#include<queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define endl '\n'#define int long longusingnamespacestd;// using i64 = long long;constintsi=2e5+10;constintinf=0x3f3f3f3f3f3f3f3fll;intn,m,V;inttot=0,head[si];structEdge{intver,Next,w;}e[si<<1];inlinevoidadd(intu,intv,intw){e[tot]=(Edge){v,head[u],w},head[u]=tot++;}classSegment_Tree{private:intls[si<<2],rs[si<<2],val[si<<2];intnode(){cot++,ls[cot]=rs[cot]=val[cot]=0;returncot;}voidpushup(intp){val[p]=val[ls[p]]+val[rs[p]];}public:intrt,cot;voidmodify(int&p,intl,intr,intx,intv){if(!p)p=node();if(l==r)returnval[p]+=v,void();intmid=(l+r)>>1;if(x<=mid)modify(ls[p],l,mid,x,v);elsemodify(rs[p],mid+1,r,x,v);pushup(p);}intquery(intp,intl,intr,intql,intqr){if(!p)return0;if(ql<=l&&r<=qr)returnval[p];intmid=(l+r)>>1,ret=0;if(ql<=mid)ret+=query(ls[p],l,mid,ql,qr);if(qr>mid)ret+=query(rs[p],mid+1,r,ql,qr);returnret;}}tr;intrt=0,maxv[si],sum;intsiz[si],dis[si],d[si],cnt=0;boolvis[si];std::queue<int>rec;voidcalcsiz(intu,intfa){siz[u]=1,maxv[u]=0;for(inti=head[u];~i;i=e[i].Next){intv=e[i].ver;if(v==fa||vis[v])continue;calcsiz(v,u),maxv[u]=max(maxv[u],siz[v]),siz[u]+=siz[v];}maxv[u]=max(maxv[u],sum-siz[u]);if(maxv[rt]>maxv[u])rt=u;}voidcalcdis(intu,intfa){d[++cnt]=dis[u];for(inti=head[u];~i;i=e[i].Next){intv=e[i].ver,w=e[i].w;if(v==fa||vis[v])continue;dis[v]=dis[u]+w,calcdis(v,u);}}intans=0;voiddfs(intu,intfa){vis[u]=true;tr.modify(tr.rt,1,V,1,1),rec.push(1);for(inti=head[u];~i;i=e[i].Next){intv=e[i].ver,w=e[i].w;if(v==fa||vis[v])continue;dis[v]=w,cnt=0,calcdis(v,u);for(intj=1;j<=cnt;++j){if(m>=d[j])ans+=tr.query(tr.rt,1,V,max(0ll,1-d[j])+1,max(0ll,m-d[j])+1);// 因为 w >= 0 所以先整体右移一下。}for(intj=1;j<=cnt;++j){tr.modify(tr.rt,1,V,d[j]+1,1),rec.push(d[j]+1);}}while(!rec.empty())tr.modify(tr.rt,1,V,rec.front(),-1),rec.pop();for(inti=head[u];~i;i=e[i].Next){intv=e[i].ver;if(v==fa||vis[v])continue;rt=0,sum=siz[v],maxv[rt]=inf;calcsiz(v,u),calcsiz(rt,-1);dfs(rt,u);}}signedmain(){cin.tie(0)->sync_with_stdio(false);cin.exceptions(cin.failbit|cin.badbit);tr.cot=tr.rt=0;memset(head,-1,sizeofhead);cin>>n,V=(int)2e7;for(inti=1;i<n;++i){intu,v,w;cin>>u>>v>>w;add(u,v,w),add(v,u,w);}cin>>m;rt=0,sum=n,maxv[rt]=inf;calcsiz(1,-1),calcsiz(rt,-1),dfs(rt,-1);cout<<ans<<endl;return0;}
// author : black_trees#include<cmath>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define endl '\n'#define int long long usingnamespacestd;usingi64=longlong;constintsi=1e5+10;constintinf=0x3f3f3f3f3f3f3f3fll;intn,L,R;inttot=0,head[si];structEdge{intver,Next,w;}e[si<<1];inlinevoidadd(intu,intv,intw){e[tot]=(Edge){v,head[u],w},head[u]=tot++;}classFenwick{private:intt[si*2],V;intlowbit(intx){returnx&-x;}public:voidinit(intx){for(inti=0;i<=x;++i)t[i]=0;V=x;}voidadd(intx,intv){while(x<=V)t[x]+=v,x+=lowbit(x);}voidsub(intx,intv){while(x<=V)t[x]-=v,x+=lowbit(x);}intque(intx){intret=0;while(x>0)ret+=t[x],x-=lowbit(x);returnret;}}tr;boolvis[si];intsum,rt,siz[si],maxv[si];voidcalcsiz(intu,intfa){siz[u]=1,maxv[u]=0;for(inti=head[u];~i;i=e[i].Next){intv=e[i].ver;if(v==fa||vis[v])continue;calcsiz(v,u),siz[u]+=siz[v];maxv[u]=max(maxv[u],siz[v]);}maxv[u]=max(maxv[u],sum-siz[u]);if(maxv[u]<maxv[rt])rt=u;}structNode{intval,dep;booloperator<(constNode&rhs)const{returnval<rhs.val;}}q[si];intcnt;voidcalcpath(intu,intfa,intdep,intvalue){q[++cnt]=(Node){value,dep};for(inti=head[u];~i;i=e[i].Next){intv=e[i].ver,w=e[i].w;if(v==fa||vis[v])continue;calcpath(v,u,dep+1,max(value,w));}}// do not change the original infomation if copies can works, or you have to know what you are doing!intsolve(intu,intfa,intedge){intret=0;cnt=0;if(edge==0)calcpath(u,fa,0,edge);elsecalcpath(u,fa,1,edge);// notice here!!sort(q+1,q+1+cnt);for(inti=1;i<=cnt;++i){auto[val,dep]=q[i];ret+=val*(tr.que(R-dep+1)-tr.que(L-dep));tr.add(dep+1,1);}for(inti=1;i<=cnt;++i)tr.sub(q[i].dep+1,1);returnret;}intans=0;voiddfs(intu,intfa){vis[u]=true;for(inti=head[u];~i;i=e[i].Next){intv=e[i].ver,w=e[i].w;if(v==fa||vis[v])continue;ans-=solve(v,u,w);}ans+=solve(u,fa,0);for(inti=head[u];~i;i=e[i].Next){intv=e[i].ver;if(v==fa||vis[v])continue;rt=0,maxv[rt]=inf,sum=siz[v];calcsiz(v,u),calcsiz(rt,0),dfs(rt,u);}}signedmain(){cin.tie(0)->sync_with_stdio(false);cin.exceptions(cin.failbit|cin.badbit);memset(head,-1,sizeofhead);cin>>n>>L>>R,tr.init(n+10);for(inti=1;i<n;++i){intu,v,w;cin>>u>>v>>w;add(u,v,w),add(v,u,w);}rt=0,maxv[rt]=inf,sum=n;calcsiz(1,0),calcsiz(rt,0),dfs(rt,0);cout<<ans*2<<endl;return0;}
// author : black_trees#include<cmath>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define endl '\n'#define int long longusingnamespacestd;usingi64=longlong;usingldb=longdouble;constldbeps=1e-6;constintsi=2e5+10;constldbinfdb=1e18+1;constintinf=0x3f3f3f3f3f3f3f3fll;intn,_t;inttot=0,head[si];structEdge{intver,Next,w;}e[si<<1];inlinevoidadd(intu,intv,intw){e[tot]=(Edge){v,head[u],w},head[u]=tot++;}boolvis[si];intpa[si],s[si],p[si],q[si],lim[si];intdep[si],siz[si],rt=0,sum=0,maxv[si];voidcalcsiz(intu,intfa){siz[u]=1,maxv[u]=0;for(inti=head[u];~i;i=e[i].Next){intv=e[i].ver;if(v==fa||vis[v])continue;calcsiz(v,u),siz[u]+=siz[v];maxv[u]=max(maxv[u],siz[v]);}maxv[u]=max(maxv[u],sum-siz[u]);if(maxv[u]<maxv[rt])rt=u;}structNode{intid,val;booloperator<(constNode&rhs)const{returnval<rhs.val;}}t[si];intcnt=0,udep;voidbuild(intu,intfa){if(lim[u]>=dep[u]-udep)t[++cnt]=(Node){u,lim[u]-dep[u]+udep};for(inti=head[u];~i;i=e[i].Next){intv=e[i].ver;if(v==fa||vis[v])continue;build(v,u);}}intQ[si],cur=0;intdp[si];ldbcalc(inti,intj){return(ldb)(dp[j]-dp[i])/(ldb)(dep[j]-dep[i]);}intfind(ldbslope){intl=1,r=cur;while(l<r){intmid=(l+r)>>1;if(calc(Q[mid-1],Q[mid])<slope-eps)r=mid;elsel=mid+1;}returnQ[l];}voidsolve(intu,inttop){intnw=pa[u],dis=s[u];while(dis<=lim[u]&&nw!=pa[top]){// top 似乎也需要被包含进去。dp[u]=min(dp[u],dp[nw]+dis*p[u]+q[u]);dis+=s[nw],nw=pa[nw];}cnt=0,udep=dep[u];for(inti=head[u];~i;i=e[i].Next){intv=e[i].ver;if(vis[v])continue;// pa[u] 已经 vis 过了。build(v,u);}sort(t+1,t+1+cnt);nw=u,dis=0,cur=0;for(inti=1;i<=cnt;++i){auto[v,val]=t[i];while(dis<=val&&nw!=pa[top]){while(cur>1&&calc(Q[cur-1],Q[cur])<calc(Q[cur-1],nw)+eps)--cur;Q[++cur]=nw;dis+=s[nw],nw=pa[nw];}if(cur>0){intopt=find(-p[v]);dp[v]=min(dp[v],dep[v]*p[v]+q[v]+dp[opt]-dep[opt]*p[v]);}}}voiddfs(intu){// 因为 vis 的缘故其实没有必要在点分的时候存 Father 了,除非统计信息要用inttop=u;while(!vis[top])top=pa[top];vis[u]=true;if(!vis[pa[u]]){calcsiz(pa[u],0);rt=0,sum=siz[pa[u]],maxv[rt]=0;calcsiz(pa[u],0),dfs(pa[u]);// 算上面以 rt 为根的 siz 是没有必要的。}solve(u,top);for(inti=head[u];~i;i=e[i].Next){intv=e[i].ver;if(vis[v])continue;calcsiz(v,0);rt=0,sum=siz[v],maxv[rt]=0;calcsiz(v,0),dfs(v);}}signedmain(){cin.tie(0)->sync_with_stdio(false);cin.exceptions(cin.failbit|cin.badbit);memset(head,-1,sizeofhead);cin>>n>>_t;pa[1]=0,s[1]=dep[1]=0,p[1]=q[1]=0,lim[1]=inf;for(inti=2;i<=n;++i){dp[i]=inf;cin>>pa[i]>>s[i];dep[i]=dep[pa[i]]+s[i];cin>>p[i]>>q[i]>>lim[i];add(i,pa[i],s[i]),add(pa[i],i,s[i]);}rt=0,sum=n,maxv[rt]=inf;dp[1]=0,vis[0]=true,calcsiz(1,0),dfs(rt);for(inti=2;i<=n;++i)cout<<dp[i]<<endl;return0;}