Submission #1269054

#TimeUsernameProblemLanguageResultExecution timeMemory
1269054Bui_Quoc_CuongTourism (JOI23_tourism)C++20
0 / 100
5095 ms8264 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; const int B = 500; int n, m, q; vector <int> g[maxn]; int c[maxn]; int par[maxn][18], h[maxn], tin[maxn], timedfs, arr[maxn]; void dfs(int u){ tin[u] = ++ timedfs; arr[tin[u]] = u; for(int &v : g[u]) if(v != par[u][0]){ par[v][0] = u; h[v] = h[u] + 1; for(int j = 1; (1 << j) <= n; j++){ par[v][j] = par[par[v][j - 1]][j - 1]; } dfs(v); } } int LCA(int u, int v){ if(h[u] < h[v]) swap(u, v); int z = __lg(h[u]); for(int s = z; s >= 0; s--) if(h[u] - h[v] >= (1 << s)) u = par[u][s]; if(u == v) return u; for(int s = z; s >= 0; s--) if(par[u][s] != par[v][s]) u = par[u][s], v = par[v][s]; return par[u][0]; } int dist(int u, int v){ return h[u] + h[v] - 2 * h[LCA(u, v)]; } namespace sub1{ void solve(){ while(q--){ int L, R; cin >> L >> R; vector <int> tour; for(int i = L; i <= R; i++){ tour.push_back(c[i]); } sort(tour.begin(), tour.end(), [&](int u, int v){ return tin[u] < tin[v]; }); tour.resize(unique(tour.begin(), tour.end()) - tour.begin()); int ans = 0; for(int i = 1; i < (int)tour.size(); i++){ ans+= dist(tour[i - 1], tour[i]); } ans+= dist(tour.back(), tour[0]); cout << ans / 2 + 1 << "\n"; } } } namespace sub2{ int ans[2]; set <int> Col[2]; struct Queries{ int L, R, id; bool operator <(const Queries &v)const{ if(L / B != v.L / B) return L < v.L; return R < v.R; } } Q[maxn]; int res[maxn]; void add(int u, int c){ auto it = Col[c].lower_bound(tin[u]); if(Col[c].empty()){ Col[c].insert(tin[u]); return; } if(it == Col[c].begin()){ int v2 = arr[*it]; int vk = arr[*Col[c].rbegin()]; ans[c]-= dist(v2, vk); ans[c]+= dist(u, vk); ans[c]+= dist(u, v2); } else if(it == Col[c].end()){ int vk = arr[*prev(it)]; int v1 = arr[*Col[c].begin()]; ans[c]-= dist(v1, vk); ans[c]+= dist(u, vk); ans[c]+= dist(u, v1); } else{ auto itR = it; auto itL = itR; itL--; int uL = arr[*itL], uR = arr[*itR]; ans[c]-= (h[uL] + h[uR] - 2 * h[LCA(uL, uR)]); ans[c]+= (h[uL] + h[u] - 2 * h[LCA(u, uL)]); ans[c]+= (h[uR] + h[u] - 2 * h[LCA(u, uR)]); } Col[c].insert(tin[u]); } void del(int u, int c){ auto it = Col[c].lower_bound(tin[u]); if(it == Col[c].begin()){ int v2 = arr[*next(it)]; int vk = arr[*Col[c].rbegin()]; ans[c]-= (h[u] + h[v2] - 2 * h[LCA(u, v2)]); ans[c]-= (h[u] + h[vk] - 2 * h[LCA(u, vk)]); ans[c]+= (h[v2] + h[vk] - 2 * h[LCA(v2, vk)]); } else if(next(it) == Col[c].end()){ int vk = arr[*prev(it)]; int v1 = arr[*Col[c].begin()]; ans[c]-= (h[u] + h[vk] - 2 * h[LCA(u, vk)]); ans[c]-= (h[u] + h[v1] - 2 * h[LCA(u, v1)]); ans[c]+= (h[v1] + h[vk] - 2 * h[LCA(v1, vk)]); } else{ auto itR = it, itL = it; itR++; itL--; int uL = arr[*itL], uR = arr[*itR]; ans[c]+= (h[uL] + h[uR] - 2 * h[LCA(uL, uR)]); ans[c]-= (h[uL] + h[u] - 2 * h[LCA(u, uL)]); ans[c]-= (h[uR] + h[u] - 2 * h[LCA(u, uR)]); } Col[c].erase(tin[u]); } int l = 1, r = 0; void MO(int id){ while(l > Q[id].L){ add(c[--l], 0); } while(l < Q[id].L){ del(c[l++], 0); } while(r > Q[id].R){ del(c[r--], 0); } while(r < Q[id].R){ add(c[++r], 0); } res[Q[id].id] = ans[0] / 2 + 1; } void solve(){ for(int i = 1; i <= q; i++){ int l, r; cin >> l >> r; Q[i] = {l, r, i}; } sort(Q + 1, Q + 1 + q); for(int i = 1; i <= q; i++) MO(i); for(int i = 1; i <= q; i++){ cout << res[i] << "\n"; } } } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); #define koa "kieuoanh" if(fopen(koa".inp", "r")){ freopen(koa".inp", "r", stdin); freopen(koa".out", "w", stdout); } cin >> n >> m >> q; for(int i = 1; i < n; i++){ int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for(int i = 1; i <= m; i++){ cin >> c[i]; } dfs(1); sub2::solve(); return 0; }

Compilation message (stderr)

tourism.cpp: In function 'int32_t main()':
tourism.cpp:152:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  152 |         freopen(koa".inp", "r", stdin); freopen(koa".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:152:48: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  152 |         freopen(koa".inp", "r", stdin); freopen(koa".out", "w", stdout);
      |                                         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#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...