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;
typedef long long ll;
typedef vector<ll> vii;
typedef pair<ll,ll> pii;
#define F first
#define S second
#define pb push_back
#define all(v) v.begin(),v.end()
#define pow poww
const ll M=1e5+10;
const ll inf=2e18;
const ll mod=998244353;
const ll mmod=1e9+7;
const ll sz=(1<<18);
ll pow(ll x,ll y){
ll res=1;
if(y<0)
return 1;
x%=mod;
while(y>0){
if((y&1))
res*=x,res%=mod;
x*=x,x%=mod;
y=(y>>1);
}
return res;
}
ll n,m,q,tin[M],tout[M],sparse[M][20],val[M],root[M],vroot[M],curtime,t;
vii v[M];
map<ll,vii>vv;
pii edge[M];
struct pst{
ll current_time=0;
}se;
void dfs(ll x=1){
tin[x]=t++;
for(auto it:v[x]){
if(it==sparse[x][0])
continue;
sparse[it][0]=x;
val[it]+=val[x];
dfs(it);
}
tout[x]=t++;
}
void build(){
dfs();
for(int bit=1;bit<=18;bit++){
for(int i=1;i<=n;i++)
sparse[i][bit]=sparse[sparse[i][bit-1]][bit-1];
}
}
ll least_common(ll x,ll y){
if(tin[y]<tin[x]&&tout[y]>tout[x])
return y;
if(tin[x]<tin[y]&&tout[x]>tout[y])
return x;
for(int bit=18;bit>=0;bit--){
if(tin[sparse[x][bit]]<tin[y]&&tout[sparse[x][bit]]>tout[y])
continue;
x=sparse[x][bit];
}
x=sparse[x][0];
return x;
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
ll TC=1;
//cin>>TC;
while(TC--){
cin>>n>>m>>q;
for(int i=0;i<n-1;i++){
ll x,y;
cin>>x>>y;
edge[i+1]={x,y};
v[x].pb(y),v[y].pb(x);
}
ll vi=0;
build();
for(int i=0;i<m;i++){
ll x,y;
cin>>x>>y;
pii p=edge[x];
if(tin[p.F]<tin[p.S])
val[p.S]++,vv[y].pb(p.S);
else
val[p.F]++,vv[y].pb(p.F);
vi=y;
}
build();
/*for(auto it:vv){
for(auto itt:it.S){
se.update(tin[itt],it.F);
se.update(tout[itt],-it.F);
}
root[++curtime]=se.current_time;
vroot[curtime]=it.F;
}*/
while(q--){
ll l=0,r=curtime,mid;
ll s,t,g,y;
cin>>s>>t>>g>>y;
ll lc=least_common(s,t);
if(val[s]+val[t]-val[lc]*2==0){
cout<<g<<'\n';
continue;
}/*
while(l<=r){
mid=((l+r)>>1);
if(se.query(root[mid],s)+se.query(root[mid],t)-se.query(root[mid],lc)-se.query(root[mid],sparse[lc][0])<=y)
l=mid+1;
else
r=mid-1;
}
if(l==0){
g-=val[s]+val[t]-val[lc]-val[sparse[lc][0]]-y/vroot[0];
if(g<0)
g=-1;
cout<<g<<'\n';
continue;
}
ll x=se.qcnt(root[l-1],s)+se.qcnt(root[l-1],t)-se.qcnt(root[l-1],lc)-se.qcnt(root[l-1],sparse[lc][0]);
y-=se.query(root[l-1],s)+se.query(root[l-1],t)-se.query(root[l-1],lc)-se.query(root[l-1],sparse[lc][0]);
g-=val[x]+val[t]-val[lc]-val[sparse[lc][0]]-x-y/vroot[l];
if(g<0)
g=-1;*/
g-=val[s]+val[t]-val[lc]*2;
g+=min(val[s]+val[t]-val[lc]*2,y/vi);
if(g<0)
g=-1;
cout<<g<<'\n';
}
}
return 0;
}
/*
*/
Compilation message (stderr)
currencies.cpp: In function 'int main()':
currencies.cpp:101:10: warning: unused variable 'l' [-Wunused-variable]
101 | ll l=0,r=curtime,mid;
| ^
currencies.cpp:101:14: warning: unused variable 'r' [-Wunused-variable]
101 | ll l=0,r=curtime,mid;
| ^
currencies.cpp:101:24: warning: unused variable 'mid' [-Wunused-variable]
101 | ll l=0,r=curtime,mid;
| ^~~
# | 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... |