#include <bits/stdc++.h>
//#include "includeall.h"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define all(x) x.begin(), x.end()
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define FOR(i, x, n) for (ll i =x; i<=n; ++i)
#define RFOR(i, x, n) for (ll i =x; i>=n; --i)
#pragma GCC optimize("O3","unroll-loops")
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<pi, ll> pii;
typedef pair<pi, pi> piii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll n, m, q;
ll a[100005], b[100005], p[100005], c[100005], dep[100005], twok[100005][25], from[100005], to[100005], ca[100005], x[100005], y[100005], in[100005], out[100005], lab = 0, until[100005], ans[100005];
vector<ll> adj[100005];
vector<ll> fenwick(100005,0);
ll ps(ll i)
{
if (i==0) return 0;
ll sum=0;
while (i>0)
{
sum+=fenwick[i];
i-=i&(-i);
}
return sum;
}
void update(ll i, ll v)
{
if (i==0) return;
while (i<=100005)
{
fenwick[i]+=v;
i+=i&(-i);
}
}
void rangeupdate(ll l, ll r, ll v)
{
update(l,v);
update(r+1,-v);
}
void dfs(ll x = 1, ll p = -1)
{
in[x] = ++lab;
for (ll k=1; k<20; ++k)
{
if (twok[x][k-1]==-1) twok[x][k] = -1;
else twok[x][k] = twok[twok[x][k-1]][k-1];
}
for (ll u: adj[x]) if( u!=p)
{
dep[u] = dep[x] + 1;
twok[u][0] = x;
dfs(u, x);
}
out[x] = lab;
}
ll lca(ll a, ll b)
{
if (a==b) return a;
if (dep[a] < dep[b]) swap(a,b);
for (ll k=0; k<20; ++k) if ((dep[a] - dep[b]) & (1LL << k)) a = twok[a][k];
if (a==b) return a;
for (ll k=19; k>=0; --k)
{
if (twok[a][k] == twok[b][k]) continue;
a = twok[a][k], b = twok[b][k];
}
return twok[a][0];
}
bool avg(pii a, pii b)
{
return a.f.f + a.f.s < b.f.f + b.f.s;
}
int main()
{
cin >> n >> m >> q;
for (ll i=1; i<=n-1;++i)
{
cin >> a[i] >> b[i];
adj[a[i]].pb(b[i]);
adj[b[i]].pb(a[i]);
}
for (ll i=0; i<m; ++i) cin >> p[i] >> c[i];
dep[1] = 0;
twok[1][0] = -1;
dfs();
for (ll i=1; i<=n-1; ++i) if (dep[a[i]] < dep[b[i]]) swap(a[i], b[i]);
vector<pii> que;
for (ll i=0; i<q; ++i)
{
cin >> from[i] >> to[i] >> x[i] >> y[i];
ca[i] = lca(from[i], to[i]);
que.pb(mp(mp(-1, m-1), i));
}
vector<pi> upd;
for (ll i=0; i<m; ++i) upd.pb(mp(c[i], a[p[i]]));
sort(all(upd));
while (que.size())
{
ll idx = 0;
sort(all(que), avg);
vector<pii> newque;
for (ll i=0; i<que.size(); ++i)
{
//show(i);
ll lo = que[i].f.f, hi = que[i].f.s;
ll mid = (lo + hi + 1)/2;
while (idx < m && idx <= mid)
{
rangeupdate(in[upd[idx].s], out[upd[idx].s], upd[idx].f);
++idx;
}
ll res = ps(in[from[que[i].s]]) + ps(in[to[que[i].s]]) - 2*ps(in[ca[que[i].s]]);
//show3(res, mid, que[i].s);
if (res <= y[que[i].s]) lo = mid;
else hi = mid-1;
if (lo==hi) until[que[i].s] = lo;
else newque.pb(mp(mp(lo, hi), que[i].s));
}
for (ll i=0; i<idx; ++i) rangeupdate(in[upd[i].s], out[upd[i].s], -upd[i].f);
swap(newque, que);
}
for (ll i=0; i<m; ++i) rangeupdate(in[upd[i].s], out[upd[i].s], 1);
for (ll i=0; i<q; ++i) ans[i] = ps(in[from[i]]) + ps(in[to[i]]) - 2*ps(in[ca[i]]);
for (ll i=0; i<m; ++i) rangeupdate(in[upd[i].s], out[upd[i].s], -1);
vector<pi> que2;
for (ll i=0; i<q; ++i) que2.pb(mp(until[i], i));
sort(all(que2));
ll idx = 0;
for (ll i=0; i<q; ++i)
{
while (idx < m && idx <= que2[i].f)
{
rangeupdate(in[upd[idx].s], out[upd[idx].s], 1);
idx++;
}
ans[que2[i].s] -= ps(in[from[que2[i].s]]) + ps(in[to[que2[i].s]]) - 2*ps(in[ca[que2[i].s]]);
}
for (ll i=0; i<q; ++i) cout << max(-1LL, x[i] - ans[i]) << endl;
return 0;
}
# | 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... |