#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
#define sz(a) (int)a.size()
const int mxN = (int)2e5+10;
const int mxN2 = mxN*320;
const int INF = (int)1e9;
const int mxLg = 18;
using ar = array<ll,2>;
ar seg[mxN2];
vector<ar> edge;
int n, m, q, IND=1;
int jmp[mxLg][mxN];
int lev[mxN], chkpnt[mxN];
vector<int> v[mxN], adj[mxN];
int L[mxN2], R[mxN2], ro[mxN];
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]) 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 : 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 = 1; i < n; 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 num = seg[s][1]+seg[t][1]-2*seg[z][1];
cout << max(-1ll, x-(num-query(y,s,t,z)[1])) << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Runtime error |
23 ms |
19532 KB |
Execution killed with signal 8 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
19 ms |
19500 KB |
Execution killed with signal 8 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9856 KB |
Output is correct |
2 |
Incorrect |
9 ms |
13984 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Runtime error |
23 ms |
19532 KB |
Execution killed with signal 8 |
3 |
Halted |
0 ms |
0 KB |
- |