Submission #1030773

#TimeUsernameProblemLanguageResultExecution timeMemory
1030773NeroZeinTourism (JOI23_tourism)C++17
24 / 100
948 ms24660 KiB
#include "bits/stdc++.h" using namespace std; #ifdef Nero #include "Deb.h" #else #define debug(...) #endif const int N = 1e5 + 5; int c[N]; int bit[N]; int ans[N]; vector<int> g[N]; set<array<int, 3>> se; int idd, id[N], top[N]; int sz[N], pr[N], dep[N]; vector<pair<int, int>> qs[N]; void dfs(int v, int p) { sz[v] = 1; for (int u : g[v]) { if (u == p) { continue; } pr[u] = v; dep[u] = dep[v] + 1; dfs(u, v); sz[v] += sz[u]; } } void dfs_hld(int v, int p, int tp) { top[v] = tp; id[v] = ++idd; int heavy = 0; for (int u : g[v]) { if (u != p && sz[u] > sz[heavy]) { heavy = u; } } if (heavy) { dfs_hld(heavy, v, tp); } for (int u : g[v]) { if (u != p && u != heavy) { dfs_hld(u, v, u); } } } void upd(int i, int v) { while (i < N) { bit[i] += v; i += i & -i; } } int qry(int i) { int ret = 0; while (i) { ret += bit[i]; i -= i & -i; } return ret; } int qry(int l, int r) { return qry(r) - qry(l - 1); } void ins(int l, int r, int val) { vector<array<int, 3>> pp, psh; psh.push_back({l, r, val}); auto itr = prev(se.upper_bound({l, INT_MAX, INT_MAX})); if ((*itr) [1] >= l) { pp.push_back(*itr); if ((*itr)[0] < l) { psh.push_back({(*itr)[0], l - 1, (*itr)[2]}); } if ((*itr)[1] > r) { psh.push_back({r + 1, (*itr)[1], (*itr)[2]}); } } itr++; while (itr != se.end() && (*itr)[0] <= r) { pp.push_back(*itr); if ((*itr)[1] > r) { psh.push_back({r + 1, (*itr)[1], (*itr)[2]}); } itr++; } for (auto [xl, xr, xv] : pp) { if (xv > 0) { upd(xv, -(xr - xl + 1)); } se.erase({xl, xr, xv}); } for (auto [xl, xr, xv] : psh) { if (xv > 0) { upd(xv, xr - xl + 1); } se.insert({xl, xr, xv}); } } void upd(int u, int v, int val) { while (top[u] != top[v]) { if (dep[top[u]] < dep[top[v]]) { swap(u, v); } ins(id[top[u]], id[u], val); u = pr[top[u]]; } if (dep[u] < dep[v]) { swap(u, v); } ins(id[v], id[u], val); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m, q; cin >> n >> m >> q; for(int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } dfs(1, 1); dfs_hld(1, 1, 1); se.insert({1, n, 0}); for (int i = 1; i <= m; ++i) { cin >> c[i]; } for (int i = 0; i < q; ++i) { int l, r; cin >> l >> r; qs[r].push_back({l, i}); } upd(c[1], c[1], 1); for (int r = 2; r <= m; ++r) { upd(c[r - 1], c[r], r - 1); upd(c[r], c[r], r); for (auto [ql, qi] : qs[r]) { ans[qi] = qry(ql, r); } } for (int i = 0; i < q; ++i) { cout << ans[i] << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...