Submission #1127975

#TimeUsernameProblemLanguageResultExecution timeMemory
1127975bunhadasouSynchronization (JOI13_synchronization)C++20
100 / 100
219 ms21932 KiB
#include <bits/stdc++.h> #define fi first #define se second #define mp make_pair #define PB push_back #define EB emplace_back #define ll long long #define bit(n,i) ((n>>i)&1) #define sz(x) (int)x.size() #define all(x) x.begin(),x.end() #define TASK "cf" using namespace std; struct fenwick{ vector<int>ft; int lim; void reset(int _n){ lim=_n+5; ft.assign(_n+10,0); } void upd(int pos,int val){ for(;pos<=lim;pos+=pos&-pos) ft[pos]+=val; } int get(int pos){ int res=0; for (;pos>0;pos-=pos&-pos) res+=ft[pos]; return res; } void updating(int l,int r,int val){ upd(l,val);upd(r+1,-val); } }; const int LOG=17; const int maxn=1e5+5; const int max_edge=2e5+5; vector<int>adj[maxn]; bool active[max_edge]; int in[maxn],out[maxn],par[maxn][LOG],val[maxn],last[maxn]; int time_dfs; pair<int,int>edge[max_edge]; int n,m,q; fenwick ft; int get_anc(int u){ int origin=u; for (int i=LOG-1;i>=0;i--) { if (par[u][i] &&ft.get(in[par[u][i]])==ft.get(in[origin])) u=par[u][i]; } return u; } void dfs(int u,int p){ in[u]=++time_dfs; for (auto v:adj[u]) { if (v==p) continue; par[v][0]=u; for (int i=1;i<LOG;i++) par[v][i]=par[par[v][i-1]][i-1]; dfs(v,u); } out[u]=time_dfs; } int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); // freopen(TASK".inp","r",stdin); // freopen(TASK".out","w",stdout); cin>>n>>m>>q; ft.reset(n); for (int i=1;i<n;i++){ int u,v; cin>>u>>v; edge[i].fi=u;edge[i].se=v; adj[u].PB(v);adj[v].PB(u); } dfs(1,-1); // for (int i=1;i<=n;i++) cout<<in[i]<<" "<<out[i]<<"\n"; for (int i=1;i<=n;i++) { ft.updating(in[i],out[i],-1); val[i]=1; } get_anc(1); for (int i=1;i<=m;i++) { int idx; cin>>idx; int u=edge[idx].fi,v=edge[idx].se; if (v==par[u][0]) swap(u,v); if (active[idx]){ // cout<<get_anc(u)<<" "<<val[get_anc(u)]<<"\n"; val[v]=last[v]=val[get_anc(u)]; ft.updating(in[v],out[v],-1); } else { val[get_anc(u)]+=val[v]-last[v]; ft.updating(in[v],out[v],1); } active[idx]=!active[idx]; } while(q--){ int u; cin>>u; cout<<val[get_anc(u)]<<"\n"; } cerr<<"Time running "<<1000*clock()/CLOCKS_PER_SEC<<" ms\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...