Submission #67519

#TimeUsernameProblemLanguageResultExecution timeMemory
67519realitySynchronization (JOI13_synchronization)C++17
100 / 100
1407 ms96360 KiB
#include "bits/stdc++.h" using namespace std; #define fi first #define se second #define ll long long #define dbg(v) cerr<<#v<<" = "<<v<<'\n' #define vi vector<int> #define vl vector <ll> #define pii pair<int,int> #define vii vector < pii > #define mp make_pair #define db long double #define pb push_back #define all(s) s.begin(),s.end() template < class P , class Q > ostream& operator<<(ostream& stream, pair < P , Q > v){ stream << "(" << v.fi << ',' << v.se << ")"; return stream;} template < class T > ostream& operator<<(ostream& stream, const vector<T> v){ stream << "[ "; for (int i=0; i<(int)v.size(); i++) stream << v[i] << " "; stream << "]"; return stream;} template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;} template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;} const int LG = 20; const int N = 1e6; int e[LG][N]; vi g[N]; int s[N]; int bg[N]; int ed[N]; int ans[N]; int lst[N]; int Time = 0; void dfs(int node = 1,int prev = 1) { bg[node] = ++Time; e[0][node] = prev; for (auto it : g[node]) if (it != prev) dfs(it,node); ed[node] = Time + 1; } int t[N]; int n; void upd(int i,int h) { for (;i <= n;i += i&(-i)) t[i] += h; } int que(int i) { int ans = 0; for (;i;i -= i&(-i)) ans += t[i]; return ans; } int get(int v) { const int G = que(bg[v]); for (int k = 19;k >= 0;--k) if (que(bg[e[k][v]]) == G) v = e[k][v]; return v; } vii E; int main(void) { int m,q; cin>>n>>m>>q; for (int i = 1;i < n;++i) { int u,v; cin>>u>>v; g[u].pb(v); g[v].pb(u); E.pb(mp(u,v)); } dfs(); for (int k = 1;k < LG;++k) for (int i = 1;i <= n;++i) e[k][i] = e[k - 1][e[k - 1][i]]; for (int i = 0;i + 1 < n;++i) if (bg[E[i].fi] > bg[E[i].se]) swap(E[i].fi,E[i].se); for (int i = 1;i <= n;++i) upd(bg[i],1),upd(ed[i],-1),ans[i] = 1; while (m --) { int tp; cin>>tp; --tp; int u = E[tp].fi; int v = E[tp].se; int g = get(u); if (lst[tp] == -1) { upd(bg[v],1); upd(ed[v],-1); lst[tp] = ans[v] = ans[g]; } else { upd(bg[v],-1); upd(ed[v],1); ans[g] += ans[v] - lst[tp]; lst[tp] = -1; } } while (q --) { int node; cin>>node; cout << ans[get(node)] << '\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...