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;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,f,g) for (int i=f;i<=g;i++)
#define for2(i,f,g) for (int i=f;i>=g;i--)
#define for3(i,f,g,h) for(int i=f;i<=g;i+=h)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
const int maxn=1e5+9;
int n,m,q;
struct edg1{
int u,v,w;
}cp[maxn];
struct edg{
int v,w;
};
vector<edg>a[maxn];
struct checkpoint{
int p;
long long c;
bool operator < (const checkpoint &pp){
return c<pp.c;
}
}b[maxn];
int in[maxn];
int out[maxn];
int dep[maxn];
int rm[maxn][19];
int dis[maxn];
int tme=0;
void dfs(int u,int par){
in[u]=++tme;
for (auto v:a[u]){
if (v.v==par)continue;
rm[v.v][0]=u;
dis[v.v]=dis[u]+1;
for1(i,1,18){
rm[v.v][i]=rm[rm[v.v][i-1]][i-1];
}
dep[v.v]=dep[u]+v.w;
//cerr<<u<<" "<<v.v<<" "<<v.w<<'\n';
dfs(v.v,u);
}
out[u]=tme;
}
int lca(int u,int v){
if (dis[u]<dis[v])swap(u,v);
int k=dis[u]-dis[v];
for1(i,0,18){
if (k>>i&1)u=rm[u][i];
}
if (u==v)return u;
for2(i,18,0){
if (!rm[u][i]||!rm[v][i])continue;
if (rm[u][i]!=rm[v][i]){
u=rm[u][i];
v=rm[v][i];
}
}
return rm[u][0];
}
pli bit[maxn];
void add(int u,long long val){
for (;u<=n;u+=(u-(u&(u-1)))){
bit[u].fi+=val;
if (val>0)bit[u].se++;
else bit[u].se--;
}
}
pli get(int u){
pli sum;
for (;u>=1;u-=(u-(u&(u-1)))){
sum.fi+=bit[u].fi;
sum.se+=bit[u].se;
}
return sum;
}
int s[maxn];
int t[maxn];
long long x[maxn];
long long y[maxn];
int par[maxn];
struct cnp{
long long l,r,num,ans,cost;
cnp(){
l=1,r=1e9,num=0,ans=0,cost=0;
}
}d[maxn];
struct query{
long long mid;
int id;
bool operator < (const query &p){
return mid<p.mid;
}
};
vector<int>ver[maxn];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("usaco.INP","r",stdin);
//freopen("usaco.OUT","w",stdout);
cin>>n>>m>>q;
map<int,int>nenso;
vector<int>nen;
nen.pb(0);
for1(i,1,n-1){
int u,v;
cin>>u>>v;
cp[i].u=u,cp[i].v=v;
cp[i].w=0;
}
for1(i,1,m){
cin>>b[i].p>>b[i].c;
nen.pb(b[i].c);
cp[b[i].p].w++;
//cerr<<b[i].p<<'\n';
}
sort(all(nen));
for1(i,1,sz(nen)-1){
if (nen[i]>nen[i-1]){
nenso[nen[i]]=nenso[nen[i-1]]+1;
}
}
for1(i,1,m){
ver[nenso[b[i].c]].pb(i);
}
//cout<<cp[2].u<<" "<<cp[2].v<<'\n';
for1(i,1,n-1){
a[cp[i].u].pb({cp[i].v,cp[i].w});
a[cp[i].v].pb({cp[i].u,cp[i].w});
}
dfs(1,0);
for1(i,1,n-1){
if (in[cp[i].u]>in[cp[i].v])swap(cp[i].u,cp[i].v);
}
for1(i,1,q){
cin>>s[i]>>t[i]>>x[i]>>y[i];
par[i]=lca(s[i],t[i]);
d[i].l=1;
d[i].r=sz(nen)-1;
}
sort(b+1,b+1+m);
while (true){
vector<query>pr;
for1(i,1,q){
if (d[i].l<=d[i].r){
pr.pb({(d[i].l+d[i].r)/2,i});
}
}
if (pr.empty())break;
sort(all(pr));
int nw=0;
for1(i,1,n)bit[i].fi=bit[i].se=0;
for (auto v:pr){
for1(i,nw+1,v.mid){
int id=b[i].p;
add(in[cp[id].v],b[i].c);
add(out[cp[id].v]+1,-b[i].c);
}
nw=v.mid;
pli t1=get(in[s[v.id]]);
pli t2=get(in[t[v.id]]);
pli t3=get(in[par[v.id]]);
pli temp;
temp.fi=t1.fi+t2.fi-2*t3.fi;
temp.se=t1.se+t2.se-2*t3.se;
//cout<<d[v.id].l<<" "<<d[v.id].r<<" "<<v.mid<<" "<<temp.fi<<" "<<temp.se<<'\n';
if (temp.fi<=y[v.id]){
d[v.id].l=v.mid+1;
d[v.id].num=temp.se;
d[v.id].ans=nen[v.mid];
d[v.id].cost=temp.fi;
}
else {
d[v.id].r=v.mid-1;
}
}
}
for1(i,1,q){
long long num=dep[s[i]]+dep[t[i]]-2*dep[par[i]]-d[i].num;
if (num>x[i])cout<<-1<<'\n';
else cout<<x[i]-num<<'\n';
}
}
# | 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... |