This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define sz(v) ((int)((v).size()))
template<typename T>
void chmax(T &x, const T &v) { if (x < v) x = v; }
template<typename T>
void chmin(T &x, const T &v) { if (x > v) x = v; }
using pii = pair<int, int>;
using vi = vector<int>;
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
const int LG = 17;
static_assert((1<<LG) > 100'000);
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int nbNode, nbSpot, nbReq;
cin >> nbNode >> nbSpot >> nbReq;
vector<vi> adj(nbNode);
rep(iEdge, 0, nbNode-1) {
int u, v; cin >> u >> v;
--u, --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<vi> anc(LG, vi(nbNode, -1));
vector<int> tin(nbNode), tout(nbNode), prof(nbNode);
{
int lastTime = 0;
auto Dfs = [&] (auto dfs, int node, int parent) -> void {
anc[0][node] = parent;
rep(lvl, 0, LG-1) {
if (anc[lvl][node] == -1) break;
anc[lvl+1][node] = anc[lvl][anc[lvl][node]];
}
tin[node] = lastTime++;
for (int child : adj[node]) if (child != parent) {
prof[child] = prof[node]+1;
dfs(dfs, child, node);
}
tout[node] = lastTime++;
};
Dfs(Dfs, 0, -1);
}
auto ancestor = [&] (int ance, int child) {
return tin[ance] <= tin[child] && tout[child] <= tout[ance];
};
auto lca = [&] (int u, int v) {
if (prof[u] > prof[v]) swap(u, v);
int firstJmp = prof[v] - prof[u];
rep(lvl, 0, LG) if ((1<<lvl) & firstJmp) {
v = anc[lvl][v];
}
assert(prof[u] == prof[v]);
if (u == v) return u;
for (int lvl = LG-1; lvl >= 0; --lvl) {
int pu = anc[lvl][u], pv = anc[lvl][v];
if (pu != -1 && pv != -1 && pu != pv) {
u = pu, v = pv;
}
}
assert(u != v);
u = anc[0][u], v = anc[0][v];
assert(u == v);
return u;
};
vector<int> spots(nbSpot);
rep(iSpot, 0, nbSpot) {
cin >> spots[iSpot];
--spots[iSpot];
}
rep(iReq, 0, nbReq) {
int L, R;
cin >> L >> R;
--L, --R;
int ans = 0;
vector<int> itv(begin(spots)+L, begin(spots)+R+1);
sort(all(itv), [&] (int i, int j) { return tin[i] < tin[j]; });
rep(i, 0, R-L) { // attention!
itv.push_back(lca(itv[i], itv[i+1]));
}
sort(all(itv), [&] (int i, int j) { return tin[i] < tin[j]; });
int szItv = sz(itv);
auto Virt = [&] (auto virt, int iCur) -> int {
int iLook = iCur+1;
while (iLook < szItv && ancestor(itv[iCur], itv[iLook])) {
ans += prof[itv[iLook]] - prof[itv[iCur]];
iLook = virt(virt, iLook);
}
return iLook;
};
assert(Virt(Virt, 0) == szItv);
cout << ans+1 << '\n';
}
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |