Language: C++

#include<bits/stdc++.h>
using namespace std;
using Int = long long;

struct HLDecomposition {
  Int n,pos;
  struct edge{
    Int to,cost;
    edge(){}
    edge(Int to,Int cost):to(to),cost(cost){}
  };
  vector<vector<edge> > G;
  vector<Int> vid, head, sub, hvy, par, dep, inv, type;
  
  HLDecomposition(){}
  HLDecomposition(Int sz):
    n(sz),pos(0),G(n),
    vid(n,-1),head(n),sub(n,1),hvy(n,-1),
    par(n),dep(n),inv(n),type(n){}
  
  void add_edge(Int u, Int v,Int c) {
    G[u].emplace_back(v,c);
    G[v].emplace_back(u,c);
  }

  void build(vector<Int> rs=vector<Int>(1,0)) {
    Int c=0;
    for(Int r:rs){
      dfs(r);
      bfs(r, c++);
    }
  }
  
  void dfs(Int rt) {
    using T = pair<Int,int>;
    stack<T> st;
    par[rt]=-1;
    dep[rt]=0;
    st.emplace(rt,0);
    while(!st.empty()){
      Int v=st.top().first;
      Int &i=st.top().second;
      if(i<(Int)G[v].size()){
	Int u=G[v][i].to,c=G[v][i].cost;
	i++;
	if(u==par[v]) continue;
	par[u]=v;
	dep[u]=dep[v]+c;
	st.emplace(u,0);
      }else{
	st.pop();
	Int res=0;
	for(edge e:G[v]){
	  Int u=e.to;
	  if(u==par[v]) continue;
	  sub[v]+=sub[u];
	  if(res<sub[u]) res=sub[u],hvy[v]=u;
	}
      }
    }
  }

  void bfs(Int r,Int c) {
    Int &k=pos;
    queue<Int> q({r});
    while(!q.empty()){
      Int h=q.front();q.pop();
      for(Int i=h;i!=-1;i=hvy[i]) {
	type[i]=c;
	vid[i]=k++;
	inv[vid[i]]=i;
	head[i]=h;
	for(edge e:G[i]){
	  Int j=e.to;
	  if(j!=par[i]&&j!=hvy[i]) q.push(j);
	}
      }
    }
  }
  
  // for_each(vertex)
  // [l,r] <- attention!!
  void for_each(Int u, Int v, const function<void(Int, Int)>& f) {
    while(1){
      if(vid[u]>vid[v]) swap(u,v);
      f(max(vid[head[v]],vid[u]),vid[v]);
      if(head[u]!=head[v]) v=par[head[v]];
      else break;
    }
  }
  
  // for_each(edge)
  // [l,r] <- attention!!
  void for_each_edge(Int u, Int v, const function<void(Int, Int)>& f) {
    while(1){
      if(vid[u]>vid[v]) swap(u,v);
      if(head[u]!=head[v]){
	f(vid[head[v]],vid[v]);
        v=par[head[v]];
      } else{
	if(u!=v) f(vid[u]+1,vid[v]);
	break;
      }
    }
  }

  Int lca(Int u,Int v){
    while(1){
      if(vid[u]>vid[v]) swap(u,v);
      if(head[u]==head[v]) return u;
      v=par[head[v]];
    }
  }

  Int distance(Int u,Int v){
    return dep[u]+dep[v]-2*dep[lca(u,v)];
  }
};


template <typename T,typename E>
struct SegmentTree{
  typedef function<T(T,T)> F;
  typedef function<T(T,E)> G;
  Int n;
  F f;
  G g;
  T d1;
  E d0;
  vector<T> dat;
  SegmentTree(){};
  SegmentTree(Int n_,F f,G g,T d1,
	      vector<T> v=vector<T>()):
    f(f),g(g),d1(d1){
    init(n_);
    if(n_==(Int)v.size()) build(n_,v);
  }
  void init(Int n_){
    n=1;
    while(n<n_) n*=2;
    dat.clear();
    dat.resize(2*n-1,d1);
  }
  void build(Int n_, vector<T> v){
    for(Int i=0;i<n_;i++) dat[i+n-1]=v[i];
    for(Int i=n-2;i>=0;i--)
      dat[i]=f(dat[i*2+1],dat[i*2+2]);
  }
  void update(Int k,E a){
    k+=n-1;
    dat[k]=g(dat[k],a);
    while(k>0){
      k=(k-1)/2;
      dat[k]=f(dat[k*2+1],dat[k*2+2]);
    }
  }
  inline T query(Int a,Int b){
    T vl=d1,vr=d1;
    for(Int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
      if(l&1) vl=f(vl,dat[(l++)-1]);
      if(r&1) vr=f(dat[(--r)-1],vr);
    }
    return f(vl,vr);
  }
  
};


signed main(){
  Int n,k;
  cin>>n>>k;
  HLDecomposition hld(n);
  for(Int i=0;i<n-1;i++){
    Int a,b,c;
    cin>>a>>b>>c;
    hld.add_edge(a,b,c);
  }
  hld.build();
  vector<Int> w(n,0);
  using T = tuple<Int, Int, Int>;
  auto con=[&](Int a,Int b){
    return hld.par[a]==b||hld.par[b]==a;
  };
  auto mget=[&](Int a,Int b){
    if(!con(a,b)) return 0LL;
    Int res=w[a]+w[b]+abs(hld.dep[a]-hld.dep[b]);
    if(res%k) return res;
    return 0LL;
  };
  auto f=[&](T a,T b){
    if(a>b) swap(a,b);
    Int al,ar,av;
    tie(al,ar,av)=a;
    Int bl,br,bv;
    tie(bl,br,bv)=b;
    if(al<0||ar<0) return b;
    Int cl,cr,cv=av+bv;
    if(con(al,bl)){
      cl=ar;cr=br;
      cv+=mget(al,bl);
    }else if(con(al,br)){
      cl=ar;cr=bl;
      cv+=mget(al,br);
    }else if(con(ar,bl)){
      cl=al;cr=br;
      cv+=mget(ar,bl);
    }else if(con(ar,br)){
      cl=al;cr=bl;
      cv+=mget(ar,br);
    }else{
      cl=cr=cv=-1;
    }
    return T(cl,cr,cv);
  };
  SegmentTree<T,Int> seg(n,
			 f,
			 [&](T a,Int b){
			   return T(b,b,0);
			 },
			 T(-1,-1,0));
  for(Int i=0;i<n;i++) seg.update(i,hld.inv[i]);
  
  Int q;
  cin>>q;
  while(q--){
    string op;
    cin>>op;
    if(op=="add"){
      Int x,d;
      cin>>x>>d;
      w[x]+=d;
      seg.update(hld.vid[x],x);
    }
    if(op=="send"){
      Int s,t;
      cin>>s>>t;
      vector<T> v;
      hld.for_each(s,t,
		   [&](Int l,Int r){
		     T x=seg.query(l,r+1);
		     for(Int i=0;i<(Int)v.size();i++){
		       T y=f(x,v[i]);
		       if(~get<0>(y)){
			 v[i]=y;
			 return;
		       }
		     }
		     v.push_back(x);
		   });
      
      while((Int)v.size()>1){
	T x=v.back();v.pop_back();
	for(Int i=0;i<(Int)v.size();i++){
	  T y=f(x,v[i]);
	  if(~get<0>(y)){
	    v[i]=y;
	    break;
	  }
	}
      }
      cout<<(get<2>(v.front()))<<endl;
    }
  }
  return 0;
}

Compiler output:
a.cc: In member function 'void HLDecomposition::dfs(Int)':
a.cc:42:23: error: cannot bind non-const lvalue reference of type 'Int&' {aka 'long long int&'} to a value of type 'int'
   42 |       Int &i=st.top().second;
      |              ~~~~~~~~~^~~~~~