# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
792821 | vjudge1 | Two Currencies (JOI23_currencies) | C++17 | 6 ms | 14420 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Author : حسن
#include <bits/stdc++.h>
using namespace std;
#define TL ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define rall(s) s.rbegin(),s.rend()
#define all(s) s.begin(),s.end()
#define pb push_back
#define fi first
#define se second
#define ll long long
#define ld long double
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define yes cout<<"yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define no cout<<"no\n"
const int N = 3e5 + 9 , mod = 1e9 + 7;
ll c[N] , us[N] , a[N], b[N] , d[N] , p[N][20] , us1[N] , timer = 0;
vector<int>v[N] , vc[N];
void dfs(int n){
int i;
us[n] = ++timer;
for(i = 1; i <= 20; i++)
p[n][i] = p[p[n][i - 1]][i - 1];
for(auto to : v[n])
if(to != p[n][0])
d[to] = d[n] + 1, c[to] = c[n] + c[to] , p[to][0] = n , dfs(to);
us1[n] = ++timer;
}
bool upper(int x , int y){
return ((us[x] <= us[y] && us1[x] >= us1[y]));
}
vector<int>ans;
ll get_lca(int x , int y){
if(upper(x , y))
return x;
if(upper(y , x))
return y;
for(int i = 20; i >= 0; i--)
if(p[x][i] != 0 && !upper(p[x][i] , y))
x= p[x][i];
return p[x][0];
}
/*
void get(int x , int y){
for(int i = 20; i >= 0; i--){
if(y >= (1 << i)){
for(auto to : vc[x]){
}
x = p[x][i];
y -= (1 << i);
}
}
}
*/
void solve(){
ll q , i , j , m , n , z , s = 0, f, l , r , k , x , y , mn = 1e18 , mx = 0;
cin>>n>>m>>q;
for(i = 1; i < n; i++){
cin>>l>>r;
v[l].pb(r);
v[r].pb(l);
}
for(i = 1; i <= m; i++){
cin>>x>>y;
j = y;
c[x]++;
}
dfs(1);
while(q--){
cin>>x>>y>>z>>f;
k = get_lca(x , y);
if(k == x || k == y){
if(d[x] > d[y])
swap(x, y);
m = c[y] - c[p[x][0]];
}else
m = c[x] - c[k] + c[y] - c[p[k][0]];
k = m;
k -= f / j;
k = max(k ,0ll);
if(k > z)
cout<<-1<<"\n";
else
cout<<z - k<<"\n";
}
}
int main(){
TL;
/*
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
*/
int t = 1;
//cin>>t;
while(t--)
{
solve();
}
}
// Author : حسن
Compilation message (stderr)
# | 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... |