#include <cstring>
#include <vector>
#include <map>
#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[M], 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;
A[p] = A[l] + A[r]; B[p] = B[l] + B[r];
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, int 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, n, m, q, P[N], J[N], D[N], root[N];
vector<int> mark[N];
vector<pair<int, int>> g[N];
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 find(int u, int d)
{
while (D[P[u]] >= d) u = D[J[u]] >= d ? J[u] : P[u];
return u;
}
inline int lca(int u, int v)
{
if (D[u] < D[v]) swap(v, u);
u = find(u, D[v]);
while (u != v)
if (J[u] == J[v]) u = J[u], v = J[v];
else u = P[u], v = P[v];
return u;
}
void dfs(int u, int p, int d)
{
D[u] = d; P[u] = p; if (D[p] - D[J[p]] == D[J[p]] - D[J[J[p]]]) J[u] = J[J[p]]; else J[u] = p;
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;
J[1] = 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 = A[ps] + A[pt] - 2*A[pa];
cout << ((all - gone > gold) ? -1 : gold - (all - gone)) << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14936 KB |
Output is correct |
2 |
Execution timed out |
5025 ms |
10840 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5031 ms |
10844 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5040 ms |
14940 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14936 KB |
Output is correct |
2 |
Execution timed out |
5025 ms |
10840 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |