Language: C++

#include<bits/stdc++.h>
 
using namespace std;
typedef long long int64;
vector< vector< int > > graph;
 
struct SegmentTree
{
  vector< int64 > data, add;
  int sz;
 
  SegmentTree(int n)
  {
    sz = 1;
    while(sz < n) sz <<= 1;
    data.assign(2 * sz - 1, 0);
    add.assign(2 * sz - 1, 0);
  }
 
  inline void Push(int k)
  {
    add[2 * k + 1] += add[k] / 2;
    add[2 * k + 2] += add[k] / 2;
    data[k] += add[k];
    add[k] = 0;
  }
 
  inline void Merge(int k)
  {
    data[k] = data[2 * k + 1] + data[2 * k + 2] + add[2 * k + 1] + add[2 * k + 2];
  }
 
  inline void RangeAdd(int a, int b, int x, int k, int l, int r)
  {
    if(a >= r || b <= l) return;
    if(a <= l && r <= b) {
      add[k] += 1LL * x * (r - l);
      return;
    }
    Push(k);
    RangeAdd(a, b, x, 2 * k + 1, l, (l + r) >> 1);
    RangeAdd(a, b, x, 2 * k + 2, (l + r) >> 1, r);
    Merge(k);
  }
 
  inline void RangeAdd(int a, int b, int x)
  {
    RangeAdd(a, b, x, 0, 0, sz);
  }
 
  inline int64 RangeSum(int a, int b, int k, int l, int r)
  {
    if(a >= r || b <= l) return (0);
    if(a <= l && r <= b) return (data[k] + add[k]);
    Push(k);
    int64 LL = RangeSum(a, b, 2 * k + 1, l, (l + r) >> 1);
    int64 RR = RangeSum(a, b, 2 * k + 2, (l + r) >> 1, r);
    return (LL + RR);
  }
 
  inline int64 RangeSum(int a, int b)
  {
    return (RangeSum(a, b, 0, 0, sz));
  }
 
  inline int size()
  {
    return (sz);
  }
};
 
struct CentroidPathDecomposition
{
  struct Centroid
  {
    int ParIndex, ParDepth, Deep;
    vector< int > node;
 
    inline int size()
    {
      return (node.size());
    }
 
    inline int &operator[](int k)
    {
      return (node[k]);
    }
 
    inline pair< int, int > Up()
    {
      return (make_pair(ParIndex, ParDepth));
    }
  };
 
  vector< int > SubTreeSize, NextPath;
  vector< int > TreeIndex, TreeDepth;
  vector< Centroid > Centroids;
 
  void BuildSubTreeSize()
  {
    stack< pair< int, int > > s;
    s.push({0, -1});
    while(!s.empty()) {
      auto p = s.top();
      s.pop();
      if(~SubTreeSize[p.first]) {
        NextPath[p.first] = -1;
        for(auto &to : graph[p.first]) {
          if(p.second == to) continue;
          SubTreeSize[p.first] += SubTreeSize[to];
          if(NextPath[p.first] == -1 || SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) {
            NextPath[p.first] = to;
          }
        }
      } else {
        s.push(p);
        SubTreeSize[p.first] = 1;
        for(auto &to : graph[p.first]) {
          if(p.second != to) s.push({to, p.first});
        }
      }
    }
  }
 
  void BuildPath()
  {
    stack< pair< int, int > > s;
    Centroids.push_back((Centroid) {-1, -1, 0});
    s.push({0, -1});
    TreeIndex[0] = 0;
    while(!s.empty()) {
      auto p = s.top();
      s.pop();
      TreeDepth[p.first] = Centroids[TreeIndex[p.first]].size();
      for(auto &to : graph[p.first]) {
        if(p.second != to) {
          if(to == NextPath[p.first]) { // Centroid-Path
            TreeIndex[to] = TreeIndex[p.first];
          } else {                  // Not Centroid-Path
            TreeIndex[to] = Centroids.size();
            Centroids.push_back((Centroid) {TreeIndex[p.first], TreeDepth[p.first], Centroids[TreeIndex[p.first]].Deep + 1});
          }
          s.push({to, p.first});
        }
      }
      Centroids[TreeIndex[p.first]].node.push_back(p.first);
    }
  }
 
  void AddEdge(int x, int y)
  {
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
 
  void Build()
  {
    BuildSubTreeSize();
    BuildPath();
  }
 
  inline int size()
  {
    return (Centroids.size());
  }
 
  inline pair< int, int > Information(int idx)
  {
    return (make_pair(TreeIndex[idx], TreeDepth[idx]));
  }
 
  inline Centroid &operator[](int k)
  {
    return (Centroids[k]);
  }
 
  inline int LCA(int a, int b)
  {
    int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
    tie(TreeIdxA, TreeDepthA) = Information(a);
    tie(TreeIdxB, TreeDepthB) = Information(b);
    while(TreeIdxA != TreeIdxB) {
      if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
        tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
      } else {
        tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
      }
    }
    if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB);
    return (Centroids[TreeIdxA][TreeDepthA]);
  }
 
  inline virtual void query(int a, int b, const function< void(int, int, int) > &f)
  {
    int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
    tie(TreeIdxA, TreeDepthA) = Information(a);
    tie(TreeIdxB, TreeDepthB) = Information(b);
    while(TreeIdxA != TreeIdxB) {
      if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
        f(TreeIdxA, 0, TreeDepthA + 1);
        tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
      } else {
        f(TreeIdxB, 0, TreeDepthB + 1);
        tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
      }
    }
    if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB);
    f(TreeIdxA, TreeDepthA + 1, TreeDepthB + 1);
  }
 
 
  CentroidPathDecomposition(int SZ)
  {
    graph.resize(SZ);
    SubTreeSize.assign(SZ, -1);
    NextPath.resize(SZ);
    TreeIndex.resize(SZ);
    TreeDepth.resize(SZ);
  }
};
 
CentroidPathDecomposition tree(150000);
vector< SegmentTree > segs;
 
void addChild(int k, int c)
{
  int TreeIdx, TreeDepth;
  tie(TreeIdx, TreeDepth) = tree.Information(k);
  segs[TreeIdx].RangeAdd(TreeDepth, segs[TreeIdx].size(), c);
}
 
int64 getDist(int k)
{
  int64 ret = 0, sub = 0;
  int TreeIdx, TreeDepth;
  tie(TreeIdx, TreeDepth) = tree.Information(k);
  while(TreeIdx > 0) {
    sub += TreeDepth + 1;
    ret += segs[TreeIdx].RangeSum(0, TreeDepth);
    tie(TreeIdx, TreeDepth) = tree[TreeIdx].Up();
    ret += segs[TreeIdx].RangeSum(TreeDepth, TreeDepth + 1) * sub;
  }
  ret += segs[TreeIdx].RangeSum(0, TreeDepth);
  return (ret);
}
 
 
int main()
{
  int N, Q;
  scanf("%d %d", &N, &Q);
 
  for(int i = 0; i < N - 1; i++) {
    int A, B;
    scanf("%d %d", &A, &B);
    tree.AddEdge(A, B);
  }
  tree.Build();
  for(int i = 0; i < tree.size(); i++) {
    segs.push_back(SegmentTree(tree[i].size()));
  }
  while(Q--) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    if(a == 0) {
      int64 sum = 0, sz = 0;
      tree.query(b, c, [&](int x, int y, int z)
      {
        sum += segs[x].RangeSum(z, z + 1) * sz;
        sum += segs[x].RangeSum(y, z);
        sz += y;
      });
      tree.query(0, tree.LCA(b, c), [&](int x, int y, int z)
      {
        sum += segs[x].RangeSum(z, z + 1) * sz;
      });
 
      printf("%lld\n", sum);
    } else {
      addChild(b, c);
    }
  }
}


Compiler output:
a.cc:2:1: error: extended character   is not valid in an identifier
    2 |  
      | ^
a.cc:6:1: error: extended character   is not valid in an identifier
    6 |  
      | ^
a.cc:9:1: error: extended character   is not valid in an identifier
    9 |   vector< int64 > data, add;
      | ^
a.cc:9:1: error: extended character   is not valid in an identifier
a.cc:10:1: error: extended character   is not valid in an identifier
   10 |   int sz;
      | ^
a.cc:10:1: error: extended character   is not valid in an identifier
a.cc:11:1: error: extended character   is not valid in an identifier
   11 |  
      | ^
a.cc:12:1: error: extended character   is not valid in an identifier
   12 |   SegmentTree(int n)
      | ^
a.cc:12:1: error: extended character   is not valid in an identifier
a.cc:13:1: error: extended character   is not valid in an identifier
   13 |   {
      | ^
a.cc:13:1: error: extended character   is not valid in an identifier
a.cc:14:1: error: extended character   is not valid in an identifier
   14 |     sz = 1;
      | ^
a.cc:14:1: error: extended character   is not valid in an identifier
a.cc:14:1: error: extended character   is not valid in an identifier
a.cc:14:1: error: extended character   is not valid in an identifier
a.cc:15:1: error: extended character   is not valid in an identifier
   15 |     while(sz < n) sz <<= 1;
      | ^
a.cc:15:1: error: extended character   is not valid in an identifier
a.cc:15:1: error: extended character   is not valid in an identifier
a.cc:15:1: error: extended character   is not valid in an identifier
a.cc:16:1: error: extended character   is not valid in an identifier
   16 |     data.assign(2 * sz - 1, 0);
      | ^
a.cc:16:1: error: extended character   is not valid in an identifier
a.cc:16:1: error: extended character   is not valid in an identifier
a.cc:16:1: error: extended character   is not valid in an identifier
a.cc:17:1: error: extended character   is not valid in an identifier
   17 |     add.assign(2 * sz - 1, 0);
      | ^
a.cc:17:1: error: extended character   is not valid in an identifier
a.cc:17:1: error: extended character   is not valid in an identifier
a.cc:17:1: error: extended character   is not valid in an identifier
a.cc:18:1: error: extended character   is not valid in an identifier
   18 |   }
      | ^
a.cc:18:1: error: extended character   is not valid in an identifier
a.cc:19:1: error: extended character   is not valid in an identifier
   19 |  
      | ^
a.cc:20:1: error: extended character   is not valid in an identifier
   20 |   inline void Push(int k)
      | ^
a.cc:20:1: error: extended character   is not valid in an identifier
a.cc:21:1: error: extended character   is not valid in an identifier
   21 |   {
      | ^
a.cc:21:1: error: extended character   is not valid in an identifier
a.cc:22:1: error: extended character   is not valid in an identifier
   22 |     add[2 * k + 1] += add[k] / 2;
      | ^
a.cc:22:1: error: extended character   is not valid in an identifier
a.cc:22:1: error: extended character   is not valid in an identifier
a.cc:22:1: error: extended character   is not valid in an identifier
a.cc:23:1: error: extended character   is not valid in an identifier
   23 |     add[2 * k + 2] += add[k] / 2;
      | ^
a.cc:23:1: error: extended character   is not valid in an identifier
a.cc:23:1: error: extended character   is not valid in an identifier
a.cc:23:1: error: extended character   is not valid in an identifier
a.cc:24:1: error: extended character   is not valid in an identifier
   24 |     data[k] += add[k];
      | ^
a.cc:24:1: error: extended character   is not valid in an identifier
a.cc:24:1: error: extended character   is not valid in an identifier
a.cc:24:1: error: extended character   is not valid in an identifier
a.cc:25:1: error: extended character   is not valid in an identifier
   25 |     add[k] = 0;
      | ^
a.cc:25:1: error: extended character   is not valid in an identifier
a.cc:25:1: error: extended character   is not valid in an identifier
a.cc:25:1: error: extended character   is not valid in an identifier
a.cc:26:1: error: extended character   is not valid in an identifier
   26 |   }
      | ^
a.cc:26:1: error: extended character   is not valid in an identifier
a.cc:27:1: error: extended character   is not valid in an identifier
   27 |  
      | ^
a.cc:28:1: error: extended character   is not valid in an identifier
   28 |   inline void Merge(int k)
      | ^
a.cc:28:1: error: extended character   is not valid in an identifier
a.cc:29:1: error: extended character   is not valid in an identifier
   29 |   {
      | ^
a.cc:29:1: error: extended character   is not valid in an identifier
a.cc:30:1: error: extended character   is not valid in an identifier
   30 |     data[k] = data[2 * k + 1] + data[2 * k + 2] + add[2 * k + 1] + add[2 * k + 2];
      | ^
a.cc:30:1: error: extended character   is not valid in an identifier
a.cc:30:1: error: extended character   is not valid in an identifier
a.cc:30:1: error: extended character   is not valid in an identifier
a.cc:31:1: error: extended character   is not valid in an identifier
   31 |   }
      | ^
a.cc:31:1: error: extended character   is not valid in an identifier
a.cc:32:1: error: extended character   is not valid in an identifier
   32 |  
      | ^
a.cc:33:1: error: extended character   is not valid in an identifier
   33 |   inline void RangeAdd(int a, int b, int x, int k, int l, int r)
      | ^
a.cc:33:1: error: extended character   is not valid in an identifier
a.cc:34:1: error: extended character   is not valid in an identifier
   34 |   {
      | ^
a.cc:34:1: error: extended character   is not valid in an identifier
a.cc:35:1: error: extended character   is not valid in an identifier
   35 |     if(a >= r || b <= l) return;
      | ^
a.cc:35:1: error: extended character   is not valid in an identifier
a.cc:35:1: error: extended character   is not valid in an identifier
a.cc:35:1: error: extended character   is not valid in an identifier
a.cc:36:1: error: extended character   is not valid in an identifier
   36 |     if(a <= l && r <= b) {
      | ^
a.cc:36:1: error: extended character   is not valid in an identifier
a.cc:36:1: error: extended character   is not valid in an identifier
a.cc:36:1: error: extended character   is not valid in an identifier
a.cc:37:1: error: extended character   is not valid in an identifier
   37 |       add[k] += 1LL * x * (r - l);
      | ^
a.cc:37:1: error: extended character   is not valid in an identifier
a.cc:37:1: error: extended character   is not valid in an identifier
a.cc:37:1: error: extended character   is not valid in an identifier
a.cc:37:1: error: extended character   is not valid in an identifier
a.cc:37:1: error: extended character   is not valid in an identifier
a.cc:38:1: error: extended character   is not valid in an identifier
   38 |       return;
      | ^
a.cc:38:1: error: extended character   is not valid in an identifier
a.cc:38:1: error: extended character   is not valid in an identifier
a.cc:38:1: error: extended character   is not valid in an identifier
a.cc:38:1: error: extended character   is not valid in an identifier
a.cc:38:1: error: extended character   is not valid in an identifier
a.cc:39:1: error: extended character   is not valid in an identifier
   39 |     }
      | ^
a.cc:39:1: error: extended character   is not valid in an identifier
a.cc:39:1: error: extended character   is not valid in an identifier
a.cc:39:1: error: extended character   is not valid in an identifier
a.cc:40:1: error: extended character   is not valid in an identifier
   40 |     Push(k);
      | ^
a.cc:40:1: error: extended character   is not valid in an identifier
a.cc:40:1: error: extended character   is not valid in an identifier
a.cc:40:1: error: extended character   is not valid in an identifier
a.cc:41:1: error: extended character   is not valid in an identifier
   41 |     RangeAdd(a, b, x, 2 * k + 1, l, (l + r) >> 1);
      | ^
a.cc:41:1: error: extended character   is not valid in an identifier
a.cc:41:1: error: extended character   is not valid in an identifier
a.cc:41:1: error: extended character   is not valid in an identifier
a.cc:42:1: error: extended character   is not valid in an identifier
   42 |     RangeAdd(a, b, x, 2 * k + 2, (l + r) >> 1, r);
      | ^
a.cc:42:1: error: extended character   is not valid in an identifier
a.cc:42:1: error: extended character   is not valid in an identifier
a.cc:42:1: error: extended character   is not valid in an identifier
a.cc:43:1: error: extended character   is not valid in an identifier
   43 |     Merge(k);
      | ^
a.cc:43:1: error: extended character   is not valid in an identifier
a.cc:43:1: error: extended character   is not valid in an identifier
a.cc:43:1: error: extended character   is not valid in an identifier
a.cc:44:1: error: extended character   is not valid in an identifier
   44 |   }
      | ^
a.cc:44:1: error: extended character   is not valid in an identifier
a.cc:45:1: error: extended character   is not valid in an identifier
   45 |  
      | ^
a.cc:46:1: error: extended character   is not valid in an identifier
   46 |   inline void RangeAdd(int a, int b, int x)
      | ^
a.cc:46:1: error: extended character   is not valid in an identifier
a.cc:47:1: error: extended character   is not valid in an identifier
   47 |   {
      | ^
a.cc:47:1: error: extended character   is not valid in an identifier
a.cc:48:1: error: extended character   is not valid in an identifier
   48 |     RangeAdd(a, b, x, 0, 0, sz);
      | ^
a.cc:48:1: error: extended character   is not valid in an identifier
a.cc:48:1: error: extended character   is not valid in an identifier
a.cc:48:1: error: extended character   is not valid in an identifier
a.cc:49:1: error: extended character   is not valid in an identifier
   49 |   }
      | ^
a.cc:49:1: error: extended character   is not valid in an identifier
a.cc:50:1: error: extended charact