Submission #49902

#TimeUsernameProblemLanguageResultExecution timeMemory
49902tmwilliamlin168Synchronization (JOI13_synchronization)C++14
100 / 100
390 ms20908 KiB
#include <bits/stdc++.h> using namespace std; const int mxN=1e5; int n, m, q, x[mxN-1], y[mxN-1], st[mxN], en[mxN], dt, ft[mxN+1], v[mxN], lc[mxN-1], anc[mxN][17]; vector<int> adj[mxN]; inline void upd(int i, int x) { for(++i; i<=n; i+=i&-i) ft[i]+=x; } inline int qry(int i) { int r=0; for(++i; i; i-=i&-i) r+=ft[i]; return r; } void dfs(int u, int p) { st[u]=dt++; anc[u][0]=p; for(int i=1; i<17; ++i) anc[u][i]=anc[u][i-1]==-1?-1:anc[anc[u][i-1]][i-1]; for(int v : adj[u]) if(v!=p) dfs(v, u); en[u]=dt; } inline int find(int x) { int a=qry(st[x]), b=x; for(int i=16; i>=0; --i) if(anc[b][i]!=-1&&qry(st[anc[b][i]])==a) b=anc[b][i]; return b; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> q; for(int i=0; i<n-1; ++i) { cin >> x[i] >> y[i], --x[i], --y[i]; adj[x[i]].push_back(y[i]); adj[y[i]].push_back(x[i]); } dfs(0, -1); for(int i=0; i<n; ++i) { v[i]=1; upd(st[i], 1); upd(en[i], -1); } for(int i=0; i<n-1; ++i) if(anc[x[i]][0]==y[i]) swap(x[i], y[i]); while(m--) { int dj; cin >> dj, --dj; int x2=find(x[dj]); if(lc[dj]==-1) { upd(st[y[dj]], 1); upd(en[y[dj]], -1); lc[dj]=v[y[dj]]=v[x2]; } else { upd(st[y[dj]], -1); upd(en[y[dj]], 1); v[x2]+=v[y[dj]]-lc[dj]; lc[dj]=-1; } } while(q--) { int ck; cin >> ck, --ck; cout << v[find(ck)] << "\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...