Language: C++

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <functional>
#include <set>
#include <ctime>
#include <random>
#include <chrono>
#include <cassert>
#include <tuple>
#include <utility>
using namespace std;

namespace {
  using Integer = long long; //__int128;
  template<class T, class S> istream& operator >> (istream& is, pair<T,S>& p){return is >> p.first >> p.second;}
  template<class T> istream& operator >> (istream& is, vector<T>& vec){for(T& val: vec) is >> val; return is;}
  template<class T> istream& operator ,  (istream& is, T& val){ return is >> val;}
  template<class T, class S> ostream& operator << (ostream& os, const pair<T,S>& p){return os << p.first << " " << p.second;}
  template<class T> ostream& operator << (ostream& os, const vector<T>& vec){for(size_t i=0; i<vec.size(); i++) os << vec[i] << (i==vec.size()-1?"":" "); return os;}
  template<class T> ostream& operator ,  (ostream& os, const T& val){ return os << " " << val;}

  template<class H> void print(const H& head){ cout << head; }
  template<class H, class ... T> void print(const H& head, const T& ... tail){ cout << head << " "; print(tail...); }
  template<class ... T> void println(const T& ... values){ print(values...); cout << endl; }

  template<class H> void eprint(const H& head){ cerr << head; }
  template<class H, class ... T> void eprint(const H& head, const T& ... tail){ cerr << head << " "; eprint(tail...); }
  template<class ... T> void eprintln(const T& ... values){ eprint(values...); cerr << endl; }

  class range{ Integer start_, end_, step_; public: struct range_iterator{ Integer val, step_; range_iterator(Integer v, Integer step) : val(v), step_(step) {} Integer operator * (){return val;} void operator ++ (){val += step_;} bool operator != (range_iterator& x){return step_ > 0 ? val < x.val : val > x.val;} }; range(Integer len) : start_(0), end_(len), step_(1) {} range(Integer start, Integer end) : start_(start), end_(end), step_(1) {} range(Integer start, Integer end, Integer step) : start_(start), end_(end), step_(step) {} range_iterator begin(){ return range_iterator(start_, step_); } range_iterator   end(){ return range_iterator(  end_, step_); } };

  inline string operator "" _s (const char* str, size_t size){ return move(string(str)); }
  constexpr Integer my_pow(Integer x, Integer k, Integer z=1){return k==0 ? z : k==1 ? z*x : (k&1) ? my_pow(x*x,k>>1,z*x) : my_pow(x*x,k>>1,z);}
  constexpr Integer my_pow_mod(Integer x, Integer k, Integer M, Integer z=1){return k==0 ? z%M : k==1 ? z*x%M : (k&1) ? my_pow_mod(x*x%M,k>>1,M,z*x%M) : my_pow_mod(x*x%M,k>>1,M,z);}
  constexpr unsigned long long operator "" _ten (unsigned long long value){ return my_pow(10,value); }

  inline int k_bit(Integer x, int k){return (x>>k)&1;} //0-indexed

  mt19937 mt(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count());

  template<class T> string join(const vector<T>& v, const string& sep){ stringstream ss; for(size_t i=0; i<v.size(); i++){ if(i>0) ss << sep; ss << v[i]; } return ss.str(); }

  inline string operator * (string s, int k){ string ret; while(k){ if(k&1) ret += s; s += s; k >>= 1; } return ret; }
}
constexpr long long mod = 9_ten + 7;


class TrivialBitVector{
 public:
  vector<int> v;
  vector<int> rank_;
  vector<int> select_0;
  vector<int> select_1;

  TrivialBitVector(vector<int>& v_) : v(v_){
    rank_.resize(v.size()+1, 0);
    for(int i=1; i<=v.size(); i++){
      rank_[i] = rank_[i-1] + v[i-1];
    }

    for(int i=0; i<v.size(); i++){
      (v[i] ? select_1 : select_0).push_back( i );
    }
  }

  int at(int pos){
    assert(0 <= pos && pos < v.size());
    return v[pos];
  }

  //count of q in [0, ub)
  int rank(int q, int ub){
    assert(ub <= v.size());
    int ret = rank_[ub];
    if(q == 0) ret = ub - ret;
    return ret;
  }

  //the position of the (t+1)-th occurrence of q
  int select(int q, int t){
    auto& s = q ? select_1 : select_0;
    if(t >= s.size()) return -1;
    return s[t];
  }
};

// T = unsigned int / unsigned long long
// B = bitvector container type
template<class T, class B>
class WaveletMatrix{
  vector<B> m;
  vector<int> L_size;
  T lg; //left most bit
  T mx;
  const int len;

 public:
  WaveletMatrix(vector<T>& v) : len(v.size()){
    mx = *max_element(v.begin(), v.end());
    lg = 0;
    while((T(1)<<lg+1) <= mx) lg++;

    m.reserve(lg+1);
    L_size.resize(lg+1);

    deque<int> l,r;

    l.resize(len);
    iota(l.begin(), l.end(), 0);

    for(int k=0; k<=lg; k++){
      deque<int> l_, r_;
      vector<int> bits(len);

      L_size[k] = l.size();

      for(int i=0; i<l.size(); i++){
        bits[i] = (v[ l[i] ] >> (lg-k))&1;
        if( (v[ l[i] ] >> (lg-k))&1 ){
          r_.push_back( l[i] );
        }else{
          l_.push_back( l[i] );
        }
      }

      for(int i=0; i<r.size(); i++){
        bits[l.size() + i] = (v[ r[i] ] >> (lg-k))&1;
        if( (v[ r[i] ] >> (lg-k))&1 ){
          r_.push_back( r[i] );
        }else{
          l_.push_back( r[i] );
        }
      }

      swap(l, l_);
      swap(r, r_);
      m.emplace_back(bits);

      /*
      for(int i=0; i<len; i++){
        cerr << bits[i];
      }cerr << endl;
      */
    }
  }

  // [q, q]
  // [lb, ub)
  int count(T q, int lb, int ub){
    return rank(q, ub, lb, 0);
  }

  // [0, q]
  // [lb, ub)
  int count_less_than(T q, int lb, int ub){
    if(ub<=lb) return 0;
    if(q >= mx) return ub-lb;
    return rank_less_than_(q, lb, ub, 0);
  }

  //return t-th occurence position of q
  //if q is not in the list then return -1
  int select(T q, int t){
    if(q >= (T(1)<<lg+1)) return -1;
    return select(q,t, 0,len, 0);
  }


  //return (t+1)-th biggest {value, pos} in [lb,ub)
  pair<int,int> quantile(int lb, int ub, int t, int k=0){
    if(t >= ub-lb) return {-1,-1};

    int val = 0;
    int pos = lb;

    int r = m[k].rank(1, ub);
    int l = m[k].rank(1, lb);

    if(k==lg){
      if(r-l > t){
        pos = m[k].select(1, r-1 - t);
        return {1, pos};
      }else{
        pos = m[k].select(0, ub-1-t-l );
        return {0, pos};
      }
    }
    if( r-l > t ){
      auto tmp = quantile(L_size[k+1] + l, L_size[k+1] + r, t, k+1);
      val = (T(1)<<(lg-k)) | tmp.first;
      pos = m[k].select(1, tmp.second - L_size[k+1]);
    }else{
      auto tmp = quantile(lb-l, ub-r, t-(r-l), k+1);
      val = tmp.first;
      pos = m[k].select(0, tmp.second);
    }
    return {val, pos};
  }

  int quantile_pos(int lb, int ub, int t, int k=0){
    if(t >= ub-lb) return -1;

    int val = 0;
    int pos = lb;

    int r = m[k].rank(1, ub);
    int l = m[k].rank(1, lb);

    if(k==lg){
      if(r-l > t){
        return m[k].select(1, r-1 - t);
      }else{
        return m[k].select(0, ub-1-t-l );
      }
    }
    if( r-l > t ){
      auto tmp = quantile_pos(L_size[k+1] + l, L_size[k+1] + r, t, k+1);
      pos = m[k].select(1, tmp - L_size[k+1]);
    }else{
      auto tmp = quantile_pos(lb-l, ub-r, t-(r-l), k+1);
      pos = m[k].select(0, tmp);
    }
    return pos;
  }

  int quantile_val(int lb, int ub, int t, int k=0){
    if(t >= ub-lb) return -1;
    int val = 0;

    int r = m[k].rank(1, ub);
    int l = m[k].rank(1, lb);

    if(k==lg){
      if(r-l > t){
        return 1;
      }else{
        return 0;
      }
    }
    if( r-l > t ){
      auto tmp = quantile_val(L_size[k+1] + l, L_size[k+1] + r, t, k+1);
      val = (T(1)<<(lg-k)) | tmp;
    }else{
      auto tmp = quantile_val(lb-l, ub-r, t-(r-l), k+1);
      val = tmp;
    }
    return val;
  }

 private:

  //return count of the occurence of q in [0, ub)
  int rank(T q, int ub, int lb=0, int k=0){
    if(q > mx) return 0;
    //ub = min(len, ub);
    int bit = (q>>lg-k) & 1;
    int r = m[k].rank(bit, ub);
    int l = m[k].rank(bit, lb);
    if(k==lg) return r - l;
    if(bit == 0){
      return rank(q, r, l, k+1);
    }else{
      return rank(q, L_size[k+1] + r, L_size[k+1] + l, k+1);
    }
  }

  int rank_less_than_(T q, int lb, int ub, int k=0){
    if(q >= mx) return ub-lb;
    int res = 0;
    int bit = (q>>lg-k) & 1;
    int r = m[k].rank(bit, ub);
    int l = m[k].rank(bit, lb);
    if(bit == 1){
      res += ub-lb - (r-l);
    }
    if(k==lg) return res + r - l;
    if(bit == 0){
      return res + rank_less_than_(q, l, r, k+1);
    }else{
      return res + rank_less_than_(q, L_size[k+1] + l, L_size[k+1] + r, k+1);
    }
  }

  int select(T q, int t, int lb, int ub, int k){
    int bit = (q>>lg-k) & 1;

    int r = m[k].rank(bit, ub);
    int l = m[k].rank(bit, lb);

    if(k==lg){
      return (r-l > t) ? m[k].select(bit, l+t) : -1;
    }else{
      if(bit == 0){
        t = select(q, t, l,r, k+1);
      }else{
        t = select(q, t, L_size[k+1] + l, L_size[k+1] + r, k+1) - L_size[k+1];
      }
      if(t <= -1) return -1;
      
      return m[k].select(bit, t);
    }
  }

};

class HeavyLightDecomposition{
 public:
  struct heavy_set{
    vector<int> element;
    int depth;
    int parent_vertex;
    heavy_set(int v, int d, int par) : element(1,v), depth(d), parent_vertex(par){}
  };

  vector<vector<int>>& G;
  vector<heavy_set> S;
  vector<int> subtree_size;
  vector<int> set_index;
  vector<int> ele_index;

 private:
  int get_subtree_size(int pos, int par){
    int sz = 1;
    for(int ch : G[pos]){
      if(ch == par) continue;
      sz += get_subtree_size(ch, pos);
    }
    return subtree_size[pos] = sz;
  }

  void make_path(int pos, int par, int set_id){
    set_index[pos] = set_id;
    ele_index[pos] = S[set_id].element.size()-1;

    int largest_child = -1;
    int value = 0;

    for(int ch : G[pos]){
      if(ch == par) continue;
      if(value < subtree_size[ch]){
        value = subtree_size[ch];
        largest_child = ch;
      }
    }

    for(int ch : G[pos]){
      if(ch == par) continue;
      if(largest_child == ch){
        S[set_id].element.push_back(ch);
        make_path(ch, pos, set_id);
      }else{
        S.emplace_back( ch, S[set_id].depth+1, pos );
        make_path(ch, pos, S.size()-1);
      }
    }
  }

  void init(int root){
    subtree_size.resize(G.size(), 0);
    get_subtree_size(root,root);

    set_index.resize(G.size(), 0);
    ele_index.resize(G.size(), 0);

    S.emplace_back( root,0,root );

    make_path( root, root, 0 );

    subtree_size.clear();
  }

 public:
  HeavyLightDecomposition(vector<vector<int>>& G_, int root = 0) : G(G_){
    init(root);
  }

  //set_index, element_index
  //S[set_index].element[element_index] == v
  pair<int,int> get_position(int v){
    return {set_index[v], ele_index[v]};
  }
};


int LCA(HeavyLightDecomposition& T, int u, int v){
  auto pu = T.get_position(u);
  auto pv = T.get_position(v);
  if(pu.first == pv.first){
    return pu.second < pv.second ? u : v;
  }

  if(T.S[pu.first].depth > T.S[pv.first].depth){
    swap(pu, pv);
    swap(u,v);
  }

  while(T.S[pu.first].depth != T.S[pv.first].depth){
    v = T.S[pv.first].parent_vertex;
    pv = T.get_position( v );
  }

  while(pu.first != pv.first){
    u = T.S[pu.first].parent_vertex;
    v = T.S[pv.first].parent_vertex;
    pu = T.get_position(u);
    pv = T.get_position(v);
    if(T.S[pv.first].depth == 0) break;
    if(T.S[pv.first].depth == 0) break;
  }

  if(pu.first == pv.first){
    return pu.second < pv.second ? u : v;
  }else{
    abort();
  }
  return -1;
}


//segment tree
//using std::function as comparing function
template<class Value = int>
class SegmentTree{
  int n;
  vector<Value> V;
  Value DEFAULT_VALUE;

  //evaluation function
  static Value evaluate(Value a, Value b){
    return a+b;
  }

  
  //return evaluated value in [a,b)
  //T[at] covers [l,r)
  Value RangeEvaluation(int a, int b, int at, int l, int r){
    //out of range
    if(r <= a || b <= l) return DEFAULT_VALUE;
    //covered
    if(a <= l && r <= b) return V[at];

    //partially covered
    else{
      Value val_left = RangeEvaluation(a,b, at*2+1, l, (l+r)/2);
      Value val_right = RangeEvaluation(a,b, at*2+2, (l+r)/2, r);
      return evaluate(val_left, val_right);
    }
  }

 public:
  SegmentTree(){}

  init(int size, Value DEFAULT = 0){
    DEFAULT_VALUE = DEFAULT;
    n=1;
    while(n<size) n <<= 1;
    V = vector<Value>(2*n - 1, DEFAULT_VALUE);
  }

  void update(int at, Value new_val){
    at += n-1;
    V[at] = new_val;
    while(at>0){
      at = (at-1)/2;
      V[at] = evaluate(V[at*2 + 1], V[at*2 + 2]);
    }
  }


  //return evaluated value in [l,r)
  Value RangeEvaluation(int l, int r){
    if(l>=r) return DEFAULT_VALUE;
    if(l>=n) return DEFAULT_VALUE;
    return RangeEvaluation(l,r, 0, 0, n);
  }
};


void Euler_tour(vector<vector<int>>& G, int pos, int last, vector<int>& res, vector<int>& et_fst){
  et_fst[pos] = res.size();
  res.push_back(pos);
  for(int nx : G[pos]){
    if(nx == last) continue;
    Euler_tour(G, nx, pos, res, et_fst);
  }
  res.push_back(pos);
}

int main(){
  int n, q;
  cin >> n,q;

  vector<vector<int>> edge(n-1, vector<int>(4));
  for(int i=0; i<n-1; i++){
    cin >> edge[i];
    edge[i][0]--;
    edge[i][1]--;
  }

  vector<int> color(n+n-1, n);
  vector<int> weight(n+n-1, 0);
  vector<vector<int>> g(n+n-1);
  for(int i=0; i<n-1; i++){
    int u = edge[i][0];
    int v = edge[i][1];
    int c = edge[i][2];
    int d = edge[i][3];
    int ev = n + i;
    color[ev] = c;
    weight[ev] = d;

    g[u].push_back(ev);
    g[ev].push_back(u);

    g[v].push_back(ev);
    g[ev].push_back(v);
  }

  vector<int> et;
  vector<int> et_fst(g.size());
  Euler_tour(g, 0, -1, et, et_fst);

  vector<vector<int>> col_tour(n+1);
  vector<int> col_et_idx(et.size());
  for(int i=0; i<et.size(); i++){
    int v = et[i];
    int c = color[v];
    col_tour[c].push_back(i);
    col_et_idx[i] = col_tour[c].size()-1;
  }

  SegmentTree<int> seg_all;
  seg_all.init((et.size()) + 1);
  vector<SegmentTree<int>> seg(n+1);
  vector<SegmentTree<int>> seg_cnt(n+1);
  seg.reserve(n+1);
  seg_cnt.reserve(n+1);
  for(int i=0; i<=n; i++){
    auto& ct = col_tour[i];
    seg[i].init(ct.size() + 2);
    seg_cnt[i].init(ct.size() + 2);
  }
  for(int i=0; i<et.size(); i++){
    int v = et[i];
    int c = color[v];
    int ci = col_et_idx[i];
    int w = weight[v];
    if(et_fst[v] == i){
      seg[c].update(ci, w);
      seg_cnt[c].update(ci, 1);
      seg_all.update(i, w);
    }else{
      seg[c].update(ci, -w);
      seg_cnt[c].update(ci, -1);
      seg_all.update(i, -w);
    }
  }

  HeavyLightDecomposition hl(g, 0);

  for(int i=0; i<q; i++){
    int x,y,u,v;
    cin >> x,y,u,v;
    u--;
    v--;

    int p = LCA(hl, u, v);

    int sum = 0;
    int sum2 = 0;
    int cnt = 0;

    if(p != u){
      int l = et_fst[p];
      int r = et_fst[u];
      sum += seg_all.RangeEvaluation(l, r+1);
      auto& seg_col = seg[x];
      auto& cv = col_tour[x];
      int cl = lower_bound(cv.begin(), cv.end(), l) - begin(cv);
      int cr = lower_bound(cv.begin(), cv.end(), r) - begin(cv);
      sum2 += seg_col.RangeEvaluation(cl, cr);
      cnt += seg_cnt[x].RangeEvaluation(cl, cr);
    }
    if(p != v){
      int l = et_fst[p];
      int r = et_fst[v];
      sum += seg_all.RangeEvaluation(l, r+1);
      auto& seg_col = seg[x];
      auto& cv = col_tour[x];
      int cl = lower_bound(cv.begin(), cv.end(), l) - begin(cv);
      int cr = lower_bound(cv.begin(), cv.end(), r) - begin(cv);
      sum2 += seg_col.RangeEvaluation(cl, cr);
      cnt += seg_cnt[x].RangeEvaluation(cl, cr);
    }
    {
      int ans = sum - sum2 + cnt * y;
      println(ans);
    }
  }


  return 0;
}


Compiler output:
a.cc:460:3: error: ISO C++ forbids declaration of 'init' with no type [-fpermissive]
  460 |   init(int size, Value DEFAULT = 0){
      |   ^~~~
a.cc: In member function 'int SegmentTree<Value>::init(int, Value)':
a.cc:465:3: warning: no return statement in function returning non-void [-Wreturn-type]
  465 |   }
      |   ^