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 <cstdio>
#include <cstring>
#include <cassert>
#include <string>
#include <deque>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
using ll = long long;
using ld = long double;
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false)
#define N 100005
#define M N*40
#define ALL(x) x.begin(), x.end()
int A[M], L[M], R[N], ALLOC = 1;
ll B[M];
inline int persist1(int a, ll b)
{
int p = ALLOC++;
A[p] = a, B[p] = b;;
return p;
}
inline int persist2(int l, int r)
{
int p = ALLOC++;
L[p] = l, R[p] = r;
assert(L[p]&&R[p]);
A[p] = A[L[p]] + A[R[p]]; B[p] = B[L[p]] + B[R[p]];
return p;
}
int build(int l, int r)
{
if (l == r) return persist1(0, 0);
return persist2(build(l, (l+r)/2), build((l+r)/2+1, r));
}
int upd(int v, int l, int r, ll w, int W)
{
if (l == r) return persist1(A[v] + 1, B[v] + w);
if (W <= (l+r)/2) return persist2(upd(L[v], l, (l+r)/2, w, W), R[v]);
return persist2(L[v], upd(R[v], (l+r)/2+1, r, w, W));
}
int cord[N], ne;
ll qry(int u, int v, int a, int l, int r, ll k)
{
if (l == r)
return min(k, B[u] + B[v] - B[a]*2) / cord[l];
ll left = B[L[u]] + B[L[v]] - B[L[a]]*2;
if (k < left) return qry(L[u], L[v], L[a], l, (l+r)/2, k);
return (A[L[u]]+ A[L[v]]- 2*A[L[a]]) + qry(R[u], R[v], R[a], (l+r)/2+1, r, k - left);
}
inline int qry_count(int u, int v, int a) { return A[u] + A[v] - 2*A[a]; }
int n, m, q, P[17][N], dep[N];
vector<int> mark[N];
vector<pair<int, int>> g[N];
int root[N];
inline int lca(int u, int v)
{
if (dep[u] < dep[v]) swap(v, u);
int dt = dep[u] - dep[v];
for (int j = 17; j--;) if (dt & (1 << j)) u = P[j][u];
if (u == v) return u;
for (int j = 17; j--;) if (P[j][u] != P[j][v]) u = P[j][u], v = P[j][v];
return P[0][u];
}
void dfs(int u, int p, int d)
{
dep[u] = d; P[0][u] = p; for (int j = 1; j < 17; ++j) P[j][u] = P[j-1][P[j-1][u]];
for (auto [v, i] : g[u])
{
if (v == p) continue;
root[v] = root[u];
for (auto w : mark[i]) root[v] = upd(root[v], 0, ne, w, lower_bound(cord, cord+ne, w) - cord);
dfs(v, u, d+1);
}
}
int main()
{
ShinLena;
cin >> n >> m >> q;
for (int u, v, i = 1; i < n; ++i) cin >> u >> v, g[u].emplace_back(v, i), g[v].emplace_back(u, i);
for (int p, c; m--; ) cin >> p >> c, mark[p].push_back(c), cord[ne++] = c;
sort(cord, cord+ne), ne = unique(cord, cord+ne) - cord - 1;
root[1] = build(0, ne); dfs(1, 1, 1);
for (ll s, t, gold, silver; q--; )
{
cin >> s >> t >> gold >> silver;
int ps = root[s], pt = root[t], pa = root[lca(s, t)];
int gone = qry(ps, pt, pa, 0, ne, silver), all = qry_count(ps, pt, pa);
cout << ((all - gone > gold) ? -1 : gold - (all - gone)) << '\n';
}
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... |