This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// 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 QRY = array<ll, 4>;
using vpi = V<pi>;
using vi = V<int>;
using vl = V<ll>;
const int LG = 18;
struct BIT {
int N; vl A; void init(int n) { N = n; A = vl(N, 0); };
void upd(int p, int x) { for(++p;p<=N;p+=p&-p) A[p-1] += x; }
ll sum(int l, int r) { return sum(r + 1) - sum(l); }
ll sum(int r) { ll 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);
assert(t == N);
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];
};
auto query = [&](BIT &T, int a, int b) {
int l = lca(a, b);
ll PA = T.sum(st[l], st[a]);
ll PB = T.sum(st[l], st[b]);
// cout << l << " " << a << " " << b << endl;
return PA + PB - 2 * T.sum(st[l], st[l]);
};
BIT B; B.init(N + 1);
for(int i = 0; i < M; i++) {
auto [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); vpi 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;
// cout << i << " => " << mid[i] << endl;
}
vpi E; for(int i = 0; i < Q; i++) {
if (mid[i] != -1) E.pb(mp(mid[i], i));
}
sort(begin(E), end(E));
// cout << "E: "<< endl;
// for(auto& x : E) cout << x.f << " " << x.s << endl;
BIT S, P; S.init(N + 1), P.init(N + 1);
for(int i = 0, cur = 0; i <= M; i++) {
// cout << i << " => " << cur << endl;
while(cur < sz(E) && E[cur].f == i) {
// cout << E[cur].s << " " << i << endl;
int idx = E[cur].s;
ll s = query(S, qry[idx][0], qry[idx][1]); // silver on path
ll p = query(P, qry[idx][0], qry[idx][1]); // checkpoints on path
ll d = query(B, qry[idx][0], qry[idx][1]); // checkpoints on path (all checkpoints)
ll g = d - p;
// cout << idx << " -> " << qry[idx][0] << " " << qry[idx][1] << " " << qry[idx][2] << " " << qry[idx][3] << endl;
// cout << s << " " << p << " " << d << " " << g << endl;
// cout << idx << " ---> " << lo[idx] << " " << mid[idx] << " " << hi[idx] << endl;
// cout << endl;
if (s <= qry[idx][3]) {
// cout << s << " <----> " << g << endl;
lo[idx] = mid[idx];
ans[idx] = mp(s, g);
// cout << idx << " ---> " << lo[idx] << " " << hi[idx] << endl;
} else hi[idx] = mid[idx] - 1;
cur++;
}
if (i != M) {
auto [c, e] = C[i];
int u = EI[e];
// cout << e << endl;
// cout << u << " -> " << st[u] << endl;
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... |