이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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*240
#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++;
assert(l&&r);
L[p] = l, R[p] = r;
A[p] = (l?A[l]:0) + (r?A[r]:0); B[p] = (l?B[l]:0) + (r?B[r]:0);
return p;
}
inline int persist3(int l, int r)
{
int p = ALLOC++;
L[p] = l, R[p] = r;
A[p] = (l?A[l]:0) + (r?A[r]:0); B[p] = (l?B[l]:0) + (r?B[r]:0);
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 persist3(upd(L[v], l, (l+r)/2, w, W), R[v]);
return persist3(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... |