This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
///https://www.instagram.com/_modwwe/
#include<bits/stdc++.h>
#define int long long
//#define ll long long
#define down cout<<'\n';
#define NHP ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define modwwe int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".out","w",stdout)
#define pb push_back
#define checktime cerr << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms";
using namespace std;
void phongbeo();
const int inf=1e18;
const int mod2=1e9+7;
const int mod1=998244353;
struct icd
{
int a,b;
};
struct ib
{
int a;
int b;
};
struct ic
{
int a,b,c;
};
struct id
{
int a,b,c,d;
};
struct ie
{
int a,b,c, d,e,f;
};
int n,m,s1,s2,s4,s3,sf,k,r,mid,s5,s6,mx,s7,s8,s9,mx2,res,dem2=0,dem=0,l;
int i,s10,s12;
int el=29;
main()
{
#ifndef ONLINE_JUDGE
// fin(task),fou(task);
#endif
NHP
/// cin>>s1;
// modwwe
phongbeo();
}
ib euler[100001];
int st[17][100001];
int dist[100001];
ib t[400001];
vector<ie> v3[400001];
vector<int> v[100001];
ib b[100001];
ib a[100001];
int c[100001];
int ans[100001];
int depth[100001];
void dfs(int x,int y)
{
st[0][x]=y;
euler[x].a=++dem;
depth[x]=depth[y]+1;
for(auto f:v[x])
{
if(f!=y)
{
dfs(f,x);
}
}
euler[x].b=dem;
}
void dfs3(int x,int y)
{
dist[x]+=dist[y];
for(auto f:v[x])
if(f!=y)
dfs3(f,x);
}
void ff(int x)
{
for(int i=x*2; i<=x*2+1; i++)
t[i].a+=t[x].a,
t[i].b+=t[x].b;
t[x]= {0,0};
}
void upd(int node,int l,int r,int l1,int r1,int c)
{
if(l>r1||r<l1) return;
if(l>=l1&&r<=r1)
{
t[node].a+=c;
if(c<0) t[node].b--;
else t[node].b++;
return;
}
int mid=l+r>>1;
ff(node);
upd(node<<1,l,mid,l1,r1,c);
upd(node<<1|1,mid+1,r,l1,r1,c);
}
int get(int node,int l,int r,int l1,int r1)
{
if(l>r1||r<l1) return 0;
if(l>=l1&&r<=r1) return t[node].a;
ff(node);
int mid=l+r>>1;
return get(node<<1,l,mid,l1,r1)+get(node<<1|1,mid+1,r,l1,r1);
}
int get2(int node,int l,int r,int l1,int r1)
{
if(l>r1||r<l1) return 0;
if(l>=l1&&r<=r1) return t[node].b;
ff(node);
int mid=l+r>>1;
return get2(node<<1,l,mid,l1,r1)+get2(node<<1|1,mid+1,r,l1,r1);
}
void dfs2(int node,int l,int r)
{
if(l>r) return;
int mid=l+r>>1;
for(int i=mid; i<=r; i++)
upd(1,1,n,euler[b[i].a].a,euler[b[i].a].b,b[i].b);
for(auto x:v3[node])
{
s2=get(1,1,n,euler[x.b].a,euler[x.b].a)+get(1,1,n,euler[x.c].a,euler[x.c].a)-get(1,1,n,euler[x.a].a,euler[x.a].a)*2;
s2+=x.d;
if(s2>=0)
{
ans[x.e]=get2(1,1,n,euler[x.b].a,euler[x.b].a)+get2(1,1,n,euler[x.c].a,euler[x.c].a)-get2(1,1,n,euler[x.a].a,euler[x.a].a)*2,
v3[node<<1|1].pb(x);
}
else v3[node<<1].pb(x);
}
if(v3[node<<1].size()!=0)
dfs2(node<<1,l,mid-1);
for(int i=mid; i<=r; i++)
upd(1,1,n,euler[b[i].a].a,euler[b[i].a].b,-b[i].b);
if(v3[node<<1|1].size()!=0)
dfs2(node<<1|1,mid+1,r);
}
int lca(int l,int r)
{
if(depth[l]<depth[r]) swap(l,r);
int ss=depth[l]-depth[r];
for(int j=16; j>=0; --j)
if(bit(ss,j))
l=st[j][l];
if(l==r) return l;
for(int j=16; j>=0; --j)
if(st[j][l]!=st[j][r])
l=st[j][l],r=st[j][r];
return st[0][l];
}
bool cmp(ib x,ib y)
{
return x.b<y.b;
}
void phongbeo()
{
cin>>n>>m>>k;
for(int i=1; i<n; i++)
{
cin>>l>>r;
v[l].pb(r);
v[r].pb(l);
a[i]= {l,r};
}
dfs(1,0);
for(int i=1; i<=m; i++)
{
cin>>l>>r;
s2=a[l].a;
s3=a[l].b;
if(depth[s3]<depth[s2]) swap(s3,s2);
b[i]= {s3,r};
dist[s3]+=r;
}
///again
dfs3(1,0);
sort(b+1,b+1+m,cmp);
for(int i=1; i<17; i++)
for(int j=1; j<=n; j++)
st[i][j]=st[i-1][st[i-1][j]];;
for(int i=1; i<=k; i++)
{
cin>>l>>r>>s2>>s3;
s4=lca(l,r);
s5=dist[l]+dist[r]-dist[s4]*2;
s3=s3-s5;
c[i]=s2;
if(s3<0)
v3[1].pb({s4,l,r,s3,i});
}
dfs2(1,1,m);
for(int i=1; i<=k; i++)
cout<<max(-1ll,c[i]-ans[i]),down
}
Compilation message (stderr)
currencies.cpp:45:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
45 | main()
| ^~~~
currencies.cpp: In function 'void upd(long long int, long long int, long long int, long long int, long long int, long long int)':
currencies.cpp:104:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
104 | int mid=l+r>>1;
| ~^~
currencies.cpp: In function 'long long int get(long long int, long long int, long long int, long long int, long long int)':
currencies.cpp:114:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
114 | int mid=l+r>>1;
| ~^~
currencies.cpp: In function 'long long int get2(long long int, long long int, long long int, long long int, long long int)':
currencies.cpp:122:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
122 | int mid=l+r>>1;
| ~^~
currencies.cpp: In function 'void dfs2(long long int, long long int, long long int)':
currencies.cpp:128:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
128 | int mid=l+r>>1;
| ~^~
# | 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... |