이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
#define sz(x) int(x.size())
#define f first
#define s second
#define mp make_pair
using ll = long long;
template<class T> using V = vector<T>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using QRY = array<ll, 4>;
using vpi = V<pi>;
using vpl = V<pl>;
using vi = V<int>;
using vl = V<ll>;
const int LG = 18;
template<class T> struct BIT {
int N; V<T> A; void init(int n) { N = n; A = V<T>(N, 0); };
void upd(int p, T x) { for(++p;p<=N;p+=p&-p) A[p-1] += x; }
T sum(int l, int r) { return sum(r + 1) - sum(l); }
T sum(int r) { T s = 0; for(;r;r-=r&-r) s += A[r-1]; return s; }
};
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, M, Q; cin >> N >> M >> Q;
V<vpi> adj(N);
for(int i = 0; i < N-1; i++) {
int u, v; cin >> u >> v; --u, --v;
adj[u].pb(mp(v, i));
adj[v].pb(mp(u, i));
}
vpi C(M);
for(auto& x : C) { cin >> x.s >> x.f; --x.s; }
sort(begin(C), end(C));
C.pb(mp(0, -1));
V<QRY> qry(Q);
for(auto& x : qry) {
cin >> x[0] >> x[1] >> x[2] >> x[3];
--x[0], --x[1];
}
V<vi> up(N, vi(LG)); vi st(N), en(N), dep(N), EI(N-1); int t = 0;
function<void(int, int)> gen = [&](int u, int p) {
st[u] = t++;
up[u][0] = p; for(int i = 1; i < LG; i++) up[u][i] = up[up[u][i-1]][i-1];
for(auto& e : adj[u]) {
auto [v, i] = e;
if (v != p) {
EI[i] = v; // v is the child of edge i
dep[v] = dep[u] + 1;
gen(v, u);
}
}
en[u] = t - 1;
};
dep[0] = 0; gen(0, 0);
auto jmp = [&](int u, int d) {
for(int i = 0; i < LG; i++) if ((d >> i) & 1) u = up[u][i];
return u;
};
auto lca = [&](int a, int b) {
if (dep[a] < dep[b]) swap(a, b);
a = jmp(a, dep[a] - dep[b]);
if (a == b) return a;
for(int i = LG - 1; i >= 0; --i) {
if (up[a][i] != up[b][i]) {
a = up[a][i], b = up[b][i];
}
}
return up[a][0];
};
BIT<int> B, S; BIT<ll> P; B.init(N + 1);
for(int i = 0; i < M; i++) {
int c, e; tie(c, e) = C[i];
int u = EI[e];
B.upd(st[u], 1); B.upd(en[u] + 1, -1);
}
// Parallel Binsearch
// Largest i value that is true (LST TRUE)
vi lo(Q, -1), hi(Q, M); vpl ans(Q, mp(-1, -1));
for(int r = 0; r < LG; r++) {
vi mid(Q); for(int i = 0; i < Q; i++) {
if (lo[i] >= hi[i]) mid[i] = -1;
else mid[i] = (lo[i] + hi[i] + 1) / 2;
}
bool done = 1;
V<vi> E(M+1); for(int i = 0; i < Q; i++) {
if (mid[i] != -1) {
E[mid[i]].pb(i);
done = 0;
}
}
if (done) break;
S.init(N + 1); P.init(N + 1);
for(int i = 0; i <= M; i++) {
for(auto& idx : E[i]) {
int u = qry[idx][0], v = qry[idx][1], l = lca(u, v);
ll s = S.sum(st[l], st[u]) + S.sum(st[l], st[v]) - 2 * S.sum(st[l], st[l]); // silver on path
ll p = P.sum(st[l], st[u]) + P.sum(st[l], st[v]) - 2 * P.sum(st[l], st[l]); // checkpoints on path
ll d = B.sum(st[l], st[u]) + B.sum(st[l], st[v]) - 2 * B.sum(st[l], st[l]); // checkpoints on path (all checkpoints)
ll g = d - p;
if (s <= qry[idx][3]) {
lo[idx] = mid[idx];
ans[idx] = mp(s, g);
} else hi[idx] = mid[idx] - 1;
}
if (i != M) {
int c, e; tie(c, e) = C[i];
int u = EI[e];
S.upd(st[u], c); S.upd(en[u] + 1, -c);
P.upd(st[u], 1); P.upd(en[u] + 1, -1);
}
}
}
// cout << endl << endl;
for(int i = 0; i < Q; i++) {
auto [a, b, g, s] = qry[i];
auto [ns, ng] = ans[i];
// cout << i << " ------> " << ns << " " << ng << endl;
if (ns <= s && ng <= g) cout << g - ng << nl;
else cout << -1 << nl;
}
exit(0-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... |