This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ar = array<ll,2>;
#define pb push_back
const int mxN = (int)1e5+10;
const int mxN2 = mxN*240;
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=1, 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));
}
ll query(ll x, int p1, int p2, int p3, int l=1, int r=INF){
if(l==r) return min(seg[p1][1]+seg[p2][1]-2*seg[p3][1], x/l);
int mid = (l+r)/2;
ll 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) return query(x,L[p1],L[p2],L[p3],l,mid);
return query(x-ltot,R[p1],R[p2],R[p3],mid+1,r)+lntot;
}
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);
}
int 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++){
ll 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))) << "\n";
}
}
Compilation message (stderr)
currencies.cpp: In function 'void dfs(int, int)':
currencies.cpp:55:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
55 | if(u==p) continue; ro[u] = ro[s];
| ^~
currencies.cpp:55:28: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
55 | if(u==p) continue; ro[u] = ro[s];
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |