#include <bits/stdc++.h>
using namespace std;
#define int long long
using ll = long long;
using ar = array<ll,2>;
#define pb push_back
#define sz(a) (int)a.size()
const int mxN = (int)1e5+10;
const int mxN2 = mxN*8*30;
const int INF = (int)1e9;
const int mxLg = 17;
ar seg[mxN2];
int n, m, q, IND=1;
int jmp[mxLg][mxN];
vector<int> v[mxN];
vector<ar> adj[mxN];
int L[mxN2], R[mxN2];
int lev[mxN], ro[mxN];
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]) L[p]=++IND;
if(!R[p]) 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));
}
ar 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], (ll)x/l);
return ar({num*l,num});
}
int mid = (l+r)/2; ar 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,w] : adj[s]){
if(u==p) continue; ro[u] = ro[s];
for(auto x : v[w]) ro[u] = upd(x,x,ro[u]);
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;
int p, c, a, b; ro[1]=1;
for(int i = 1; i < n; i++)
cin >> a >> b, adj[a].pb({b,i}), adj[b].pb({a,i});
for(int i = 1; i <= m; i++)
cin >> p >> c, v[p].pb(c);
dfs(1,0);
for(int i = 1; i < mxLg; i++)
for(int j = 1; j <= n; 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 num = seg[s][1]+seg[t][1]-2*seg[z][1];
cout << max(-1ll, x-(num-query(y,s,t,z)[1])) << "\n";
}
}
Compilation message
currencies.cpp: In function 'void dfs(long long int, long long int)':
currencies.cpp:64:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
64 | if(u==p) continue; ro[u] = ro[s];
| ^~
currencies.cpp:64:28: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
64 | if(u==p) continue; ro[u] = ro[s];
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Runtime error |
16 ms |
10080 KB |
Execution killed with signal 8 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
20 ms |
10092 KB |
Execution killed with signal 8 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Runtime error |
25 ms |
19640 KB |
Execution killed with signal 8 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Runtime error |
16 ms |
10080 KB |
Execution killed with signal 8 |
3 |
Halted |
0 ms |
0 KB |
- |