이 제출은 이전 버전의 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 ALL(x) x.begin(), x.end()
struct persist
{
int a; ll b;
persist *l, *r;
persist(int a0, ll b0) : a(a0), b(b0), l(nullptr), r(nullptr) {}
persist(persist *l0, persist *r0) : a((l0?l0->a:0) + (r0?r0->a:0)), b((l0?l0->b:0) + (r0?r0->b:0)), l(l0), r(r0) {}
~persist() {if (l) delete l; if (r) delete r;}
};
persist *build(int l, int r)
{
if (l == r) return new persist(0, 0);
return new persist(build(l, (l+r)/2), build((l+r)/2+1, r));
}
persist *upd(persist *v, int l, int r, ll w, int W)
{
if (l == r) return new persist(v->a + 1, v->b + w);
if (W <= (l+r)/2) return new persist(upd(v->l, l, (l+r)/2, w, W), v->r);
return new persist(v->l, upd(v->r, (l+r)/2+1, r, w, W));
}
vector<ll> silver_cord;
ll qry(persist *u, persist *v, persist *a, int l, int r, ll k)
{
if (l == r)
return min(k, u->b + v->b - 2*a->b) / silver_cord[l];
ll left = u->l->b + v->l->b - a->l->b * 2;
if (k < left) return qry(u->l, v->l, a->l, l, (l+r)/2, k);
return (u->l->a + v->l->a - a->l->a * 2) + qry(u->r, v->r, a->r, (l+r)/2+1, r, k - left);
}
inline ll qry_count(persist *u, persist *v, persist *a) { return 1ll * u->a + v->a - a->a * 2ll; }
int n, m, q, P[17][N], dep[N], ne;
vector<int> mark[N];
vector<pair<int, int>> g[N];
persist *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(ALL(silver_cord), w) - silver_cord.begin());
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), silver_cord.push_back(c);
sort(ALL(silver_cord)); silver_cord.erase(unique(ALL(silver_cord)), silver_cord.end()); ne = silver_cord.size() - 1;
root[1] = build(0, ne); dfs(1, 1, 1);
for (ll s, t, gold, silver; q--; )
{
cin >> s >> t >> gold >> silver;
persist *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... |