#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define sz(a) (int)a.size()
const int mxN = (int)2e5+10;
const int mxN2 = mxN*400;
const int INF = (int)1e9;
const int mxLg = 21;
int n, m, q, IND=1;
int lev[mxN], chkpnt[mxN];
int jmp[mxLg][mxN];
vector<int> v[mxN], adj[mxN];
vector<pair<int,int>> edge;
int L[mxN2], R[mxN2], ro[mxN];
array<int,2> seg[mxN2];
void ae(int a, int b){ adj[a].pb(b),adj[b].pb(a); }
int newChild(int p, int v){
int p2 = ++IND;
//L[p2] = L[p], R[p2] = R[p];
seg[p2]={seg[p][0]+v,seg[p][1]+1};
return p2;
}
int newPar(int l, int r){
int p = ++IND; L[p] = l, R[p] = r;
seg[p][0] = seg[l][0]+seg[r][0];
seg[p][1] = seg[l][1]+seg[r][1];
return p;
}
int upd(int x, int v, int p, int l=0, int r=INF){
if(!p) return p;
if(l==r) return newChild(p,v);
int mid = (l+r)/2;
if(!L[p] and x<=mid) L[p]=++IND;
if(!R[p] and x>mid) R[p]=++IND;
if(x<=mid) return newPar(upd(x,v,L[p],l,mid),R[p]);
return newPar(L[p],upd(x,v,R[p],mid+1,r));
}
array<int,2> query(int x, int p1, int p2, int p3, int l=0, int r=INF){
if(l==r){
int num = min(seg[p1][1]+seg[p2][1]-2*seg[p3][1], x/l);
return array<int,2>({num*l,num});
}
int mid = (l+r)/2; array<int,2> ans = {0,0};
int ltot = seg[L[p1]][0]+seg[L[p2]][0]-2*seg[L[p3]][0];
int lntot = seg[L[p1]][1]+seg[L[p2]][1]-2*seg[L[p3]][1];
if(ltot>=x) ans = query(x,L[p1],L[p2],L[p3],l,mid);
else{
ans = query(x-ltot,R[p1],R[p2],R[p3],mid+1,r);
ans[0]+=ltot, ans[1]+=lntot;
}
return ans;
}
void dfs(int s, int p){
jmp[0][s]=p; lev[s] = lev[p]+1;
for(auto u : adj[s]){
if(u==p) continue;
if(!chkpnt[u]) ro[u]=ro[s];
else ro[u] = upd(chkpnt[u],chkpnt[u],ro[s]);
dfs(u,s);
}
}
int getPath(int x, int k){
for(int i = mxLg-1; i>=0; i--)
if((k>>i)&1) x=jmp[i][x];
return x;
}
int lca(int a, int b){
if(a==b) return a;
if(jmp[0][a]==jmp[0][b]) return jmp[0][a];
if(lev[a]>lev[b]) swap(a,b);
b = getPath(b,lev[b]-lev[a]);
for(int i = mxLg-1; i>=0; i--)
if(jmp[i][a]!=jmp[i][b] and jmp[i][a])
a=jmp[i][a], b=jmp[i][b];
return lca(a,b);
}
int32_t main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> m >> q; ro[1]=1;
int p, c, tot=n+1, a, b;
for(int i = 0; i < n-1; i++)
cin >> a >> b, edge.pb({a,b});
for(int i = 1; i <= m; i++)
cin >> p >> c, v[p-1].pb(c);
for(int i = 0; i < n-1; i++){
auto [a,b] = edge[i];
if(lev[a]>lev[b]) swap(a,b);
if(sz(v[i])==0) { ae(a,b); continue; }
ae(a,tot);
for(int j = 0; j < sz(v[i]); j++){
chkpnt[tot]=v[i][j];
if(j==sz(v[i])-1) break;
ae(tot,tot+1), tot++;
}
ae(b,tot), tot++;
}
dfs(1,0);
for(int i = 1; i < mxLg; i++)
for(int j = 1; j <= tot; j++)
jmp[i][j] = jmp[i-1][jmp[i-1][j]];
for(int i = 0; i < q; i++){
int s, t, x, y, z; cin >> s >> t >> x >> y;
z = ro[lca(s,t)], s = ro[s], t = ro[t];
int TOT = seg[s][0]+seg[t][0]-2*seg[z][0];
int num = seg[s][1]+seg[t][1]-2*seg[z][1];
auto [tot,ans] = query(y,s,t,z);
cout << max(-1ll, x-(num-ans)) << "\n";
}
}
Compilation message
currencies.cpp: In function 'int32_t main()':
currencies.cpp:115:13: warning: unused variable 'TOT' [-Wunused-variable]
115 | int TOT = seg[s][0]+seg[t][0]-2*seg[z][0];
| ^~~
/usr/bin/ld: failed to convert GOTPCREL relocation; relink with --no-relax
collect2: error: ld returned 1 exit status