#line 1 "library/Template/template.hpp" #include using namespace std; #define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define ALL(v) (v).begin(),(v).end() using ll=long long int; const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; templateinline bool chmax(T& a,T b){if(ainline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} #line 2 "library/Utility/fastio.hpp" #include class FastIO{ static constexpr int L=1<<16; char rdbuf[L]; int rdLeft=0,rdRight=0; inline void reload(){ int len=rdRight-rdLeft; memmove(rdbuf,rdbuf+rdLeft,len); rdLeft=0,rdRight=len; rdRight+=fread(rdbuf+len,1,L-len,stdin); } inline bool skip(){ for(;;){ while(rdLeft!=rdRight and rdbuf[rdLeft]<=' ')rdLeft++; if(rdLeft==rdRight){ reload(); if(rdLeft==rdRight)return false; } else break; } return true; } template::value,int> =0>inline bool _read(T& x){ if(!skip())return false; if(rdLeft+20>=rdRight)reload(); bool neg=false; if(rdbuf[rdLeft]=='-'){ neg=true; rdLeft++; } x=0; while(rdbuf[rdLeft]>='0' and rdLeft::value,int> =0>inline bool _read(T& x){ if(!skip())return false; if(rdLeft+20>=rdRight)reload(); bool neg=false; if(rdbuf[rdLeft]=='-'){ neg=true; rdLeft++; } x=0; while(rdbuf[rdLeft]>='0' and rdbuf[rdLeft]<='9' and rdLeft='0' and rdbuf[rdLeft]<='9' and rdLeft=rdRight)reload(); x=rdbuf[rdLeft++]; return true; } inline bool _read(string& x){ if(!skip())return false; for(;;){ int pos=rdLeft; while(pos' ')pos++; x.append(rdbuf+rdLeft,pos-rdLeft); if(rdLeft==pos)break; rdLeft=pos; if(rdLeft==rdRight)reload(); else break; } return true; } templateinline bool _read(vector& v){ for(auto& x:v){ if(!_read(x))return false; } return true; } char wtbuf[L],tmp[50]; int wtRight=0; inline void flush(){ fwrite(wtbuf,1,wtRight,stdout); wtRight=0; } inline void _write(const char& x){ if(wtRight>L-32)flush(); wtbuf[wtRight++]=x; } inline void _write(const string& x){ for(auto& c:x)_write(c); } template::value,int> =0>inline void _write(T x){ if(wtRight>L-32)flush(); if(x==0){ _write('0'); return; } else if(x<0){ _write('-'); if (__builtin_expect(x == std::numeric_limits::min(), 0)) { switch (sizeof(x)) { case 2: _write("32768"); return; case 4: _write("2147483648"); return; case 8: _write("9223372036854775808"); return; } } x=-x; } int pos=0; while(x!=0){ tmp[pos++]=char((x%10)|48); x/=10; } rep(i,0,pos)wtbuf[wtRight+i]=tmp[pos-1-i]; wtRight+=pos; } templateinline void _write(const vector& v){ rep(i,0,v.size()){ if(i)_write(' '); _write(v[i]); } } public: FastIO(){} ~FastIO(){flush();} inline void read(){} template inline void read(Head& head,Tail&... tail){ assert(_read(head)); read(tail...); } templateinline void write(){if(ln)_write('\n');} template inline void write(const Head& head,const Tail&... tail){ if(space)_write(' '); _write(head); write(tail...); } }; /** * @brief Fast IO */ #line 3 "sol.cpp" #line 2 "library/Graph/centroid.hpp" class CentroidDecomposition{ void get(int v,int p){ sz[v]=1; for(auto& to:g[v])if(to!=p and !used[to]){ get(to,v); sz[v]+=sz[to]; } } int dfs(int v,int p,int rt){ for(auto& to:g[v])if(to!=p and !used[to]){ if(sz[to]>(sz[rt]>>1))return dfs(to,v,rt); } return v; } public: int n; vector> g; vector sz,used; CentroidDecomposition(int n_):n(n_),g(n),sz(n),used(n){} void add_edge(int u,int v){ g[u].push_back(v); g[v].push_back(u); } int find(int rt){ get(rt,-1); int res=dfs(rt,-1,rt); used[res]=1; return res; } }; /** * @brief Centroid Decomposition */ #line 2 "library/Graph/hld.hpp" struct HLD{ using P=pair; vector> g; vector sz,in,out,rev,hs,par,dist; void dfs(int v,int p){ par[v]=p; sz[v]=1; dist[v]=dist[p]+1; if(!g[v].empty() and g[v][0]==p)swap(g[v][0],g[v].back()); for(auto& to:g[v])if(to!=p){ dfs(to,v); sz[v]+=sz[to]; if(sz[g[v][0]]in[v])swap(u,v); if(hs[u]==hs[v])return u; } } vector

get(int u,int p,bool es=0){ assert(in[p]<=in[u] and out[u]<=out[p]); vector

res; while(hs[u]!=hs[p]){ res.push_back({in[hs[u]],in[u]+1}); u=par[hs[u]]; } res.push_back({in[p]+es,in[u]+1}); return res; } int jump(int u,int v,int k){ if(k<0)return -1; int g=lca(u,v); int d0=dist[u]+dist[v]-dist[g]*2; if(d0=in[to])return rev[in[st]-k]; k-=in[st]-in[to]+1; st=par[to]; } } }; /** * @brief Heavy Light Decomposition */ #line 6 "sol.cpp" struct ContourQuery{ using P=pair; using T=pair; ContourQuery(int _n=0):n(_n),m(_n),cd(_n), hld(_n),tree(_n*3),depth(_n*3), base(_n*3),parent(_n*3,-1),SZ(_n*3),width(_n*3,1),seg(_n*3){} void add_edge(int u,int v){ cd.add_edge(u,v); hld.add_edge(u,v); } vector run(){ hld.run(); root=rec(0); depth[0]=0; dfs(0,-1); rep(v,0,m)if(v!=root){ seg[v]=width[v]; } return seg; } vector

point(int v){ vector

ret; int cur=v; while(cur!=root){ int D=depth[v]+depth[base[cur]]-2*depth[hld.lca(v,base[cur])]; ret.push_back({cur,D}); cur=parent[cur]; } return ret; } vector range(int v,int L,int R){ vector ret; if(L<=0 and 0> tree; vector depth,base,parent,SZ,width,seg; int rec(int rt){ int cen=cd.find(rt); SZ[cen]=1; queue

que; auto cmp=[&](int u,int v){return SZ[u]>SZ[v];}; priority_queue,decltype(cmp)> pq{cmp}; pq.push(cen); depth[cen]=0; base[cen]=cen; for(auto& to:cd.g[cen])if(!cd.used[to]){ int v=rec(to); que.push({to,cen}); depth[to]=1; while(!que.empty()){ auto [cur,par]=que.front(); que.pop(); width[v]=depth[cur]+1; for(auto& nxt:cd.g[cur])if(nxt!=par and !cd.used[nxt]){ depth[nxt]=depth[cur]+1; que.push({nxt,cur}); } } pq.push(v); base[v]=cen; } cd.used[cen]=0; if(pq.size()>1){ for(;;){ int v1=pq.top(); pq.pop(); int v2=pq.top(); pq.pop(); int extra=m++; tree[extra].push_back(v1); tree[extra].push_back(v2); tree[v1].push_back(extra); tree[v2].push_back(extra); SZ[extra]=SZ[v1]+SZ[v2]; parent[v1]=parent[v2]=extra; if(pq.empty()){ return extra; } pq.push(extra); base[extra]=cen; width[extra]=max(width[v1],width[v2]); } } else{ int extra=m++; tree[extra].push_back(cen); tree[cen].push_back(extra); SZ[extra]=1; parent[cen]=extra; return extra; } } void dfs(int v,int p){ for(auto& to:cd.g[v])if(to!=p){ depth[to]=depth[v]+1; dfs(to,v); } } }; #line 2 "library/DataStructure/dualsegtree.hpp" templateclass DualSegmentTree{ int sz,height; vector data; void down(int k){ data[k*2]=f(data[k*2],data[k]); data[k*2+1]=f(data[k*2+1],data[k]); data[k]=m1(); } public: DualSegmentTree(){} DualSegmentTree(int n){ sz=1,height=0; while(sz& v){ for(int i=0;i<(int)v.size();i++)data[i+sz]=v[i]; } void update(int a,int b,M x){ if(a>=b)return; a+=sz,b+=sz; for(int i=height;i;i--){ if(((a>>i)<>i); if(((b>>i)<>i); } for(;a>=1,b>>=1){ if(a&1)data[a]=f(data[a],x),a++; if(b&1)--b,data[b]=f(data[b],x); } } M query(int k){ k+=sz; for(int i=height;i;i--){ if(((k>>i)<>i); } M ret=data[k]; while(k>>=1)ret=f(ret,data[k]); return ret; } }; /** * @brief Dual Segment Tree */ #line 127 "sol.cpp" ll f(ll a,ll b){return a+b;} ll m1(){return 0;} FastIO io; int main(){ int n,q; io.read(n,q); ContourQuery buf(n); rep(_,0,n-1){ int a,b; io.read(a,b); a--; b--; buf.add_edge(a,b); } auto len=buf.run(); vector> seg(len.size()); rep(i,0,len.size())seg[i]=DualSegmentTree(len[i]); while(q--){ int x,y,z; io.read(x,y,z); x--; ll ret=0; for(auto& [i,v]:buf.point(x)){ ret+=seg[i].query(v); } io.write(ret); for(auto& [i,LR]:buf.range(x,0,y+1)){ auto [L,R]=LR; seg[i].update(L,R,z); } } return 0; }