Language: C++

#include<bits/stdc++.h>
using namespace std;
#define MAX 150005
typedef long long ll;

int n,q,a,b,c;

vector<int> G[MAX];
int cd[MAX],mv[MAX],pr[MAX],de[MAX];

vector<int> H;
int hl[MAX],hr[MAX];

// calc hl[] hr[] mv[]
void dfsB(int pos,int prev){
  hl[pos]=H.size();
  H.push_back(pos);
  if(cd[pos]!=-1){
    mv[cd[pos]]=mv[pos];
    dfsB(cd[pos],pos);
  }
  for(int i=0;i<(int)G[pos].size();i++){
    int to=G[pos][i];
    if(to==prev||to==cd[pos])continue;
    dfsB(to,pos);
  }
  hr[pos]=H.size();
}

// calc cd[] pr[] de[] , init mv[]
int dfsA(int pos,int prev,int depth){
  int m=-1,maxm,res=1;
  pr[pos]=prev;
  de[pos]=depth;
  mv[pos]=pos;
  for(int i=0;i<(int)G[pos].size();i++){
    int to=G[pos][i];
    if(to==pr[pos])continue;
    int k=dfsA(to,pos,depth+1);
    res+=k;
    if(m==-1||maxm<k)m=to,maxm=k;
  }
  cd[pos]=m;
  return res;
}


void init(){
  H.clear();
  for(int i=0;i<n;i++){
    G[i].clear();
  }
}


char ch(int x){ return 'A'+x; }

struct segtree{
  int n;
  vector<ll> A,B;
  void init(int N){
    A.clear(),B.clear();    
    for(n=1;n<N;n*=2);
    A.resize(n+n),B.resize(n+n);
  }
  void add(int a,int b,int x,int k,int l,int r){
    if(b<=l || r<=a)return;
    if(a<=l && r<=b){A[k]+=x;return;}
    B[k]+=(min(b,r)-max(a,l))*x;
    int m=(l+r)/2;
    add(a,b,x,k*2+1,l,m);
    add(a,b,x,k*2+2,m,r);
  }
  void add(int a,int b,int x){add(a,b,x,0,0,n);}
  ll sum(int a,int b,int k,int l,int r){
    if(b<=l || r<=a)return 0;
    if(a<=l && r<=b)return A[k]*(r-l)+B[k];
    int m=(l+r)/2;
    return sum(a,b,k*2+1,l,m)+sum(a,b,k*2+2,m,r)+A[k]*(min(b,r)-max(a,l));
  }
  ll sum(int a,int b){return sum(a,b,0,0,n);}
};

segtree t;

int main(){
  while(scanf("%d %d",&n,&q)==2){
    t.init(n);
    init();
    for(int i=0;i<n-1;i++){
      scanf("%d %d",&a,&b);
      G[a].push_back(b);
      G[b].push_back(a);
    }

    for(int i=0;i<n;i++)sort(G[i].begin(),G[i].end());

    dfsA(0,-1,0);
    dfsB(0,-1);
    
    
    while(q--){
      scanf("%d %d %d",&a,&b,&c);
      if(a==1){
        t.add(hl[b],hr[b],c);
        t.add(hl[b],hl[b]+1,-c);
      }else{
        ll ans=0;
        while(mv[b]!=mv[c]){
          if(de[mv[b]]<de[mv[c]])swap(b,c);
          ans+=t.sum( hl[mv[b]] , hl[b]+1);
          b=pr[mv[b]];          
        }
        if(hl[b]>hl[c])swap(b,c);
        ans+=t.sum(hl[b],hl[c]+1);
        printf("%lld\n",ans);
      }
    }
  }
  return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define MAX 150005
typedef long long ll;

int n,q,a,b,c;

vector<int> G[MAX];
int cd[MAX],mv[MAX],pr[MAX],de[MAX];

vector<int> H;
int hl[MAX],hr[MAX];

// calc hl[] hr[] mv[]
void dfsB(int pos,int prev){
  hl[pos]=H.size();
  H.push_back(pos);
  if(cd[pos]!=-1){
    mv[cd[pos]]=mv[pos];
    dfsB(cd[pos],pos);
  }
  for(int i=0;i<(int)G[pos].size();i++){
    int to=G[pos][i];
    if(to==prev||to==cd[pos])continue;
    dfsB(to,pos);
  }
  hr[pos]=H.size();
}

// calc cd[] pr[] de[] , init mv[]
int dfsA(int pos,int prev,int depth){
  int m=-1,maxm,res=1;
  pr[pos]=prev;
  de[pos]=depth;
  mv[pos]=pos;
  for(int i=0;i<(int)G[pos].size();i++){
    int to=G[pos][i];
    if(to==pr[pos])continue;
    int k=dfsA(to,pos,depth+1);
    res+=k;
    if(m==-1||maxm<k)m=to,maxm=k;
  }
  cd[pos]=m;
  return res;
}


void init(){
  H.clear();
  for(int i=0;i<n;i++){
    G[i].clear();
  }
}


char ch(int x){ return 'A'+x; }

struct segtree{
  int n;
  vector<ll> A,B;
  void init(int N){
    A.clear(),B.clear();    
    for(n=1;n<N;n*=2);
    A.resize(n+n),B.resize(n+n);
  }
  void add(int a,int b,int x,int k,int l,int r){
    if(b<=l || r<=a)return;
    if(a<=l && r<=b){A[k]+=x;return;}
    B[k]+=(min(b,r)-max(a,l))*x;
    int m=(l+r)/2;
    add(a,b,x,k*2+1,l,m);
    add(a,b,x,k*2+2,m,r);
  }
  void add(int a,int b,int x){add(a,b,x,0,0,n);}
  ll sum(int a,int b,int k,int l,int r){
    if(b<=l || r<=a)return 0;
    if(a<=l && r<=b)return A[k]*(r-l)+B[k];
    int m=(l+r)/2;
    return sum(a,b,k*2+1,l,m)+sum(a,b,k*2+2,m,r)+A[k]*(min(b,r)-max(a,l));
  }
  ll sum(int a,int b){return sum(a,b,0,0,n);}
};

segtree t;

int main(){
  while(scanf("%d %d",&n,&q)==2){
    t.init(n);
    init();
    for(int i=0;i<n-1;i++){
      scanf("%d %d",&a,&b);
      G[a].push_back(b);
      G[b].push_back(a);
    }

    for(int i=0;i<n;i++)sort(G[i].begin(),G[i].end());

    dfsA(0,-1,0);
    dfsB(0,-1);
    
    
    while(q--){
      scanf("%d %d %d",&a,&b,&c);
      if(a==1){
        t.add(hl[b],hr[b],c);
        t.add(hl[b],hl[b]+1,-c);
      }else{
        ll ans=0;
        while(mv[b]!=mv[c]){
          if(de[mv[b]]<de[mv[c]])swap(b,c);
          ans+=t.sum( hl[mv[b]] , hl[b]+1);
          b=pr[mv[b]];          
        }
        if(hl[b]>hl[c])swap(b,c);
        ans+=t.sum(hl[b],hl[c]+1);
        printf("%lld\n",ans);
      }
    }
  }
  return 0;
}

Compiler output:
a.cc:127:5: error: redefinition of 'int n'
  127 | int n,q,a,b,c;
      |     ^
a.cc:6:5: note: 'int n' previously declared here
    6 | int n,q,a,b,c;
      |     ^
a.cc:127:7: error: redefinition of 'int q'
  127 | int n,q,a,b,c;
      |       ^
a.cc:6:7: note: 'int q' previously declared here
    6 | int n,q,a,b,c;
      |       ^
a.cc:127:9: error: redefinition of 'int a'
  127 | int n,q,a,b,c;
      |         ^
a.cc:6:9: note: 'int a' previously declared here
    6 | int n,q,a,b,c;
      |         ^
a.cc:127:11: error: redefinition of 'int b'
  127 | int n,q,a,b,c;
      |           ^
a.cc:6:11: note: 'int b' previously declared here
    6 | int n,q,a,b,c;
      |           ^
a.cc:127:13: error: redefinition of 'int c'
  127 | int n,q,a,b,c;
      |             ^
a.cc:6:13: note: 'int c' previously declared here
    6 | int n,q,a,b,c;
      |             ^
a.cc:129:13: error: redefinition of 'std::vector<int> G [150005]'
  129 | vector<int> G[MAX];
      |             ^
a.cc:8:13: note: 'std::vector<int> G [150005]' previously declared here
    8 | vector<int> G[MAX];
      |             ^
a.cc:130:5: error: redefinition of 'int cd [150005]'
  130 | int cd[MAX],mv[MAX],pr[MAX],de[MAX];
      |     ^~
a.cc:9:5: note: 'int cd [150005]' previously declared here
    9 | int cd[MAX],mv[MAX],pr[MAX],de[MAX];
      |     ^~
a.cc:130:13: error: redefinition of 'int mv [150005]'
  130 | int cd[MAX],mv[MAX],pr[MAX],de[MAX];
      |             ^~
a.cc:9:13: note: 'int mv [150005]' previously declared here
    9 | int cd[MAX],mv[MAX],pr[MAX],de[MAX];
      |             ^~
a.cc:130:21: error: redefinition of 'int pr [150005]'
  130 | int cd[MAX],mv[MAX],pr[MAX],de[MAX];
      |                     ^~
a.cc:9:21: note: 'int pr [150005]' previously declared here
    9 | int cd[MAX],mv[MAX],pr[MAX],de[MAX];
      |                     ^~
a.cc:130:29: error: redefinition of 'int de [150005]'
  130 | int cd[MAX],mv[MAX],pr[MAX],de[MAX];
      |                             ^~
a.cc:9:29: note: 'int de [150005]' previously declared here
    9 | int cd[MAX],mv[MAX],pr[MAX],de[MAX];
      |                             ^~
a.cc:132:13: error: redefinition of 'std::vector<int> H'
  132 | vector<int> H;
      |             ^
a.cc:11:13: note: 'std::vector<int> H' previously declared here
   11 | vector<int> H;
      |             ^
a.cc:133:5: error: redefinition of 'int hl [150005]'
  133 | int hl[MAX],hr[MAX];
      |     ^~
a.cc:12:5: note: 'int hl [150005]' previously declared here
   12 | int hl[MAX],hr[MAX];
      |     ^~
a.cc:133:13: error: redefinition of 'int hr [150005]'
  133 | int hl[MAX],hr[MAX];
      |             ^~
a.cc:12:13: note: 'int hr [150005]' previously declared here
   12 | int hl[MAX],hr[MAX];
      |             ^~
a.cc:136:6: error: redefinition of 'void dfsB(int, int)'
  136 | void dfsB(int pos,int prev){
      |      ^~~~
a.cc:15:6: note: 'void dfsB(int, int)' previously defined here
   15 | void dfsB(int pos,int prev){
      |      ^~~~
a.cc:152:5: error: redefinition of 'int dfsA(int, int, int)'
  152 | int dfsA(int pos,int prev,int depth){
      |     ^~~~
a.cc:31:5: note: 'int dfsA(int, int, int)' previously defined here
   31 | int dfsA(int pos,int prev,int depth){
      |     ^~~~
a.cc:169:6: error: redefinition of 'void init()'
  169 | void init(){
      |      ^~~~
a.cc:48:6: note: 'void init()' previously defined here
   48 | void init(){
      |      ^~~~
a.cc:177:6: error: redefinition of 'char ch(int)'
  177 | char ch(int x){ return 'A'+x; }
      |      ^~
a.cc:56:6: note: 'char ch(int)' previously defined here
   56 | char ch(int x){ return 'A'+x; }
      |      ^~
a.cc:179:8: error: redefinition of 'struct segtree'
  179 | struct segtree{
      |        ^~~~~~~
a.cc:58:8: note: previous definition of 'struct segtree'
   58 | struct segtree{
      |        ^~~~~~~
a.cc:205:9: error: redefinition of 'segtree t'
  205 | segtree t;
      |         ^
a.cc:84:9: note: 'segtree t' previously declared here
   84 | segtree t;
      |         ^
a.cc:207:5: error: redefinition of 'int main()'
  207 | int main(){
      |     ^~~~
a.cc:86:5: note: 'int main()' previously defined here
   86 | int main(){
      |     ^~~~