Submission #957476

#TimeUsernameProblemLanguageResultExecution timeMemory
957476Mizo_CompilerSynchronization (JOI13_synchronization)C++17
100 / 100
411 ms60308 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef double ld; #define pb push_back #define sz(x) int32_t(x.size()) #define all(x) x.begin(),x.end() #define F first #define S second const int N = 2e5+1; int n, m, q, p[N][18], cur = 0, par[N], sz[N], idx[N]; vector<int> adj[N], edg[(1 << 19)]; vector<pair<int, int>> ed; bool vis[int(2e5)], qr[N]; ll ans[N], tk[N]; vector<pair<int, int>> vv; int findp(int u) { return (par[u] == u ? u : findp(par[u])); } void merge(int u, int v) { u = findp(u), v = findp(v); if (u == v)return; cur++; if (sz[u] < sz[v])swap(u, v); sz[u] += sz[v]; par[v] = u; vv.pb({u, v}); } void rollback(int tm) { while (cur > tm) { par[vv.back().S] = vv.back().S; sz[vv.back().F] -= sz[vv.back().S]; vv.pop_back(); cur--; } } void dfs(int u) { for (auto &v : adj[u]) { if (v == p[u][0])continue; p[v][0] = u; for (int i = 1; i < 18; i++) { p[v][i] = p[p[v][i-1]][i-1]; } dfs(v); } } void upd(int li, int ri, int x, int l, int r, int id) { if (li >= l && ri <= r) { edg[x].pb(id); return; } if (li > r || ri < l)return; int mid = (li + ri) >> 1; upd(li, mid, x*2+1, l, r, id); upd(mid+1, ri, x*2+2, l, r, id); } int getp(int u) { int x = findp(u); for (int i = 17; i >= 0; i--) { if (findp(p[u][i]) == x) { u = p[u][i]; } } return u; } void sol(int i) { int u = ed[idx[i]].F, v = ed[idx[i]].S; int rt = getp(u); if (qr[i]) { ans[rt] += ans[v] - tk[v]; tk[v] = ans[v]; } else { ans[v] = ans[rt]; tk[v] = ans[v]; } } void walk(int l, int r, int x) { int tmp = cur; for (auto &i : edg[x]) { merge(ed[i].F, ed[i].S); } if (l == r) { sol(l); rollback(tmp); return; } int mid = (l + r) >> 1; walk(l, mid, x*2+1); walk(mid+1, r, x*2+2); rollback(tmp); return; } signed main () { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m >> q; for (int i = 0; i+1 < n; i++) { int u, v; cin >> u >> v; --u, --v; adj[u].pb(v); adj[v].pb(u); ed.pb({u, v}); ans[i] = 1; sz[i] = 1; par[i] = i; } sz[n-1] = ans[n-1] = 1; par[n-1] = n-1; dfs(0); for (int i = 0; i+1 < n; i++) { if (p[ed[i].F][0] == ed[i].S) { swap(ed[i].F, ed[i].S); } } map<int, int> mp; for (int i = 0; i < m; i++) { int x; cin >> x; --x; vis[x] ^= 1; if (vis[x])mp[x] = i; else upd(0, m-1, 0, mp[x], i-1, x), mp.erase(x); qr[i] = vis[x]; idx[i] = x; } for (auto &[x, y] : mp) { upd(0, m-1, 0, y, m-1, x); } walk(0, m-1, 0); for (auto &[x, y] : mp) { merge(ed[x].F, ed[x].S); } while (q--) { int u; cin >> u; --u; cout << ans[getp(u)] << "\n"; } }
#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...