Language: C++

#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include<bits/stdc++.h>
#define F first
#define S second
#define vec vector
#define ms multiset
#define pb push_back
#define pll pair<ll,ll>
#define pdd pair<ld, ld>
#define pq priority_queue
#define umap unordered_map
#define uset unordered_set
#define pii pair<ll, ll>
#define int ll
#define pnn pair<Node*, Node*>
#define uid uniform_int_distribution
#define FILE ifstream in("board.in");ofstream out("board.out");
#define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);
using namespace std;
typedef string str;
typedef long long ll;
typedef long double ld;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); //uid<int> u1(5, 10); u1(rnd);

struct Node{
    Node *l=0, *r=0;
    pii p = {0,0};

    Node(Node *a, Node *b, pii c){
        l = a; r = b;
        p = c;
    }

    Node(Node *a, Node *b){
        l = a; r = b;
        p = {a->p.F+b->p.F, a->p.S+b->p.S};
    }

    Node(Node *n){
        l = n->l; r = n->r;
        p = n->p;
    }
};

Node *add(int l, int r, int pos, Node *n, int val){
    Node *cp = n ? new Node(n) : new Node(0,0,{0,0});
    if(l==r){
        cp->p.F++;
        cp->p.S+=val;
        return cp;
    }
    int md = (l+r)/2;
    if(pos<=md) cp->l = add(l,md,pos,cp->l,val);
    else cp->r = add(md+1,r,pos,cp->r,val);
    return cp;
}

pii get(int l, int r, int pos, Node *n){
    if(!n) return {0,0};
    if(l==r) return n->p;
    int md = (l+r)/2;
    if(pos<=md) return get(l,md,pos,n->l);
    else return get(md+1,r,pos,n->r);
}

int a, tim=0, ndo=-1, SZ = 1e5+5;
vec<vec<int>> l;
vec<int> sz, dep, tin, tout, numdo, posindo, eul, p, lf, rg, cl, dl, prs, firindo;
vec<Node*> m;

int init1(int v, int pr, int d){
    sz[v] = 1;
    prs[v] = pr==-1 ? 0 : prs[pr]+dl[v];
    p[v] = pr;
    dep[v] = d;
    for(int h : l[v]){
        if(h!=pr) sz[v] += init1(h, v, d+1);
    }
    sort(l[v].begin(), l[v].end(), [](int i1, int i2){return sz[i1]>sz[i2];});
    return sz[v];
}

void init2(int v, int pr, int pdo){
    eul.pb(v);
    if(pdo==0) ndo++, lf.pb(eul.size()-1);
    numdo[v] = ndo;
    tin[v] = tim++;
    posindo[v] = pdo;
    int cnt = 0;
    for(int h : l[v]){
        if(h==pr) continue;
        init2(h, v, (cnt==0 ? pdo+1 : 0));
        cnt++;
    }
    if(rg.size()<lf.size()) rg.pb(eul.size()-1);
    tout[v] = tim-1;
}

int check(int v, int pt){
    return tin[v]<=tin[pt] && tin[pt]<=tout[v];
}

int sm=0;
pii go(int x, int y, int c){
    int cnt=0, d=0, sx = x, sy = y;
    for(; !check(firindo[numdo[x]], y);){
        pii pp = get(0, SZ, c, m[lf[numdo[x]]+posindo[x]]);
        cnt+=pp.F, d+=pp.S;
        x = p[firindo[x]];
    }
    for(; numdo[x]!=numdo[y];){
        pii pp = get(0, SZ, c, m[lf[numdo[y]]+posindo[y]]);
        cnt+=pp.F, d+=pp.S;
        y = p[firindo[y]];
    }
    if(dep[x]>dep[y]) swap(x,y);
    sm = prs[sx] + prs[sy] - 2*prs[x];
    int DO = numdo[x];
    pii p1 = get(0, SZ, c, m[lf[DO]+posindo[y]]);
    pii p2 = get(0, SZ, c, m[lf[DO]+posindo[x]]);
    return {cnt+p1.F-p2.F, d+p1.S-p2.S};
}

int main() {
    fast;
    int z;
    cin>>a>>z;
    p = vec<int>(a);
    sz = vec<int>(a);
    cl = vec<int>(a);
    dl = vec<int>(a);
    dep = vec<int>(a);
    tin = vec<int>(a);
    prs = vec<int>(a);
    tout = vec<int>(a);
    numdo = vec<int>(a);
    posindo = vec<int>(a);
    firindo = vec<int>(a);
    l = vec<vec<int>>(a, vec<int>());
    for(int q=0; q<a-1; q++){
        int x,y,c,d; cin>>x>>y>>c>>d; x--; y--;
        if(x>y) swap(x,y);
        l[x].pb(y);
        l[y].pb(x);
        cl[y] = c;
        dl[y] = d;
    }
    init1(0, -1, 0);
    init2(0, -1, 0);
    ndo++;
    m = vec<Node*>(a);
    for(int q=0; q<ndo; q++) {
        m[lf[q]] = new Node(0, 0, {0, 0});
        for(int w=lf[q]; w<=rg[q]; w++) m[w] = add(0, SZ, cl[eul[w]], (w==lf[q] ? m[w] : m[w-1]), dl[eul[w]]), firindo[eul[w]] = eul[lf[q]];
    }
    for(; z; z--){
        int x,y,c,d; cin>>c>>d>>x>>y; x--; y--;
        sm = 0;
        pii p = go(x,y,c);
        cout<<sm-p.S+p.F*d<<"\n";
    }
}

Compiler output:
a.cc:15:13: error: '::main' must return 'int'
   15 | #define int ll
      |             ^~
a.cc:125:1: note: in expansion of macro 'int'
  125 | int main() {
      | ^~~