제출 #216218

#제출 시각아이디문제언어결과실행 시간메모리
216218Blagojce동기화 (JOI13_synchronization)C++11
0 / 100
2278 ms28744 KiB
#include <bits/stdc++.h> #define fr(i, n, m) for(int i = (n); i < (m); i ++) #define pb push_back #define st first #define nd second #define pq priority_queue #define all(x) begin(x), end(x) using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; ll const inf = 1e9; ll const mod = 1e9 + 7; ld const eps = 1e-13; int n, m, q; vector<pii> g[100005]; vector<pii> intervals[100005]; int cnt[100005]; bool deleted[1000005]; int sub[100005]; int temp_size; void dfs0(int u, int p){ sub[u] = 1; for(auto e : g[u]){ if(!deleted[e.st] && e.st != p){ dfs0(e.st, u); sub[u] += sub[e.st]; } } } int dfs1(int u, int p){ for(auto e : g[u]){ if(!deleted[e.st] && e.st != p && sub[e.st] > temp_size / 2){ return dfs1(e.st, u); } } return u; } int seg[1000006]; int zet[1000006]; void update(int k, int l, int r, int x, int y, int add, int z){ zet[k] += z; if(r < x || y < l) return; if(x <= l && r <= y){ zet[k] += add; return; } int mid = (l + r) / 2; update(k * 2, l, mid, x, y, add, zet[k]); update(k * 2 + 1, mid + 1, r, x, y, add, zet[k]); zet[k] = 0; seg[k] = max(seg[k * 2] + zet[k * 2], seg[k * 2 + 1] + zet[k * 2 + 1]); } int findpos(int k, int l, int r, int val, bool dir){ if(l == r){ return l; } int mid = (l + r) / 2; zet[k * 2] += zet[k]; zet[k * 2 + 1] += zet[k]; zet[k] = 0; seg[k] = max(seg[k * 2] + zet[k * 2], seg[k * 2 + 1] + zet[k * 2 + 1]); if(dir){ if(seg[k * 2 + 1] + zet[k * 2 + 1] == val){ return findpos(k * 2 + 1, mid + 1, r, val, dir); } else if(seg[k * 2] + zet[k * 2] == val){ return findpos(k * 2, l, mid, val, dir); } } else{ if(seg[k * 2] + zet[k * 2] == val){ return findpos(k * 2, l, mid, val, dir); } else if(seg[k * 2 + 1] + zet[k * 2 + 1] == val){ return findpos(k * 2 + 1, mid + 1, r, val, dir); } } return -1; } int l[100005], r[100005]; vector<int> V[100005]; void dfs2(int u, int p, int c){ for(auto e : g[u]){ if(deleted[e.st] || e.st == p) continue; for(auto i : intervals[e.nd]){ update(1, 0, m, i.st, i.nd, + 1, 0); } l[e.st] = findpos(1, 0, m, c + 1, 0); r[e.st] = findpos(1, 0, m, c + 1, 1); dfs2(e.st, u, c + 1); for(auto i : intervals[e.nd]){ update(1, 0, m, i.st, i.nd, - 1, 0); } } } void dfs3(int u, int p, int comp){ V[comp].pb(u); for(auto e : g[u]){ if(deleted[e.st] || e.st == p) continue; dfs3(e.st, u, comp); } } int BIT[100005]; void sumupdate(int k, int add){ while(k < 100005){ BIT[k] += add; k += k&-k; } } int sumquery(int k){ int s = 0; while(k > 0){ s += BIT[k]; k -= k&-k; } return s; } void decompose(int root){ dfs0(root, root); temp_size = sub[root]; int centroid = dfs1(root, root); dfs2(centroid, centroid, 0); int c = 0; for(auto u : g[centroid]){ if(deleted[u.st]) continue; dfs3(u.st, centroid, c); ++c; } fr(i, 0, c){ for(auto u : V[i]){ if(l[u] == -1) continue; cnt[centroid] ++; cnt[u] ++; sumupdate(l[u], 1); } } fr(i, 0, c){ for(auto u : V[i]){ if(l[u] == -1) continue; sumupdate(l[u], -1); } for(auto u : V[i]){ cnt[u] += sumquery(r[u]); } for(auto u : V[i]){ if(l[u] == -1) continue; sumupdate(l[u], +1); } } fr(i, 0, c){ for(auto u : V[i]){ if(l[u] == -1) continue; sumupdate(l[u], -1); } V[i].clear(); } deleted[centroid] = true; for(auto u : g[centroid]){ if(!deleted[u.st]){ decompose(u.st); } } } void solve(){ cin >> n >> m >> q; fr(i, 0, n - 1){ int u, v; cin >> u >> v; --u, --v; g[u].pb({v, i}); g[v].pb({u, i}); } int _last[n]; memset(_last, -1, sizeof(_last)); fr(i, 0, m){ int _ed; cin >> _ed; --_ed; if(_last[_ed] == -1){ _last[_ed] = i; } else{ intervals[_ed].pb({_last[_ed] + 1, i + 1}); _last[_ed] = -1; } } fr(i, 0, n - 1){ if(_last[i] != -1){ intervals[i].pb({_last[i] + 1, m}); } } decompose(0); while(q --){ int _node; cin >> _node; --_node; cout << cnt[_node] + 1<< endl; } } int main() { solve(); 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...