(UPD: 2024-12-04 14:48 UTC) Judge is not working due to Cloudflare incident. (URL) We can do nothing about it, sorry. After the incident is resolved, we will grade all submissions.

Submission #903487

#TimeUsernameProblemLanguageResultExecution timeMemory
903487GrindMachineSynchronization (JOI13_synchronization)C++17
100 / 100
1591 ms30696 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long int ll; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,ll> pll; #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL) #define pb push_back #define endl '\n' #define sz(a) (int)a.size() #define setbits(x) __builtin_popcountll(x) #define ff first #define ss second #define conts continue #define ceil2(x,y) ((x+y-1)/(y)) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define yes cout << "Yes" << endl #define no cout << "No" << endl #define rep(i,n) for(int i = 0; i < n; ++i) #define rep1(i,n) for(int i = 1; i <= n; ++i) #define rev(i,s,e) for(int i = s; i >= e; --i) #define trav(i,a) for(auto &i : a) template<typename T> void amin(T &a, T b) { a = min(a,b); } template<typename T> void amax(T &a, T b) { a = max(a,b); } #ifdef LOCAL #include "debug.h" #else #define debug(x) 42 #endif /* refs: https://github.com/mostafa-saad/MyCompetitiveProgramming/blob/master/Olympiad/JOI/JOIOC-13-synchronization.txt */ const int MOD = 1e9 + 7; const int N = 1e5 + 5; const int inf1 = int(1e9) + 5; const ll inf2 = ll(1e18) + 5; template<typename T> struct fenwick { int siz; vector<T> tree; fenwick() { } fenwick(int n) { siz = n; tree = vector<T>(n + 1); } int lsb(int x) { return x & -x; } void build(vector<T> &a, int n) { for (int i = 1; i <= n; ++i) { int par = i + lsb(i); tree[i] += a[i]; if (par <= siz) { tree[par] += tree[i]; } } } void pupd(int i, T v) { while (i <= siz) { tree[i] += v; i += lsb(i); } } T sum(int i) { T res = 0; while (i) { res += tree[i]; i -= lsb(i); } return res; } T query(int l, int r) { if (l > r) return 0; T res = sum(r) - sum(l - 1); return res; } }; vector<pll> adj[N]; struct lca_algo { // LCA template (for graphs with 1-based indexing) int LOG = 1; vector<int> depth; vector<vector<int>> up; vector<int> tin, tout; vector<int> edge_node; int timer = 1; lca_algo() { } lca_algo(int n) { lca_init(n); } void lca_init(int n) { while ((1 << LOG) < n) LOG++; up = vector<vector<int>>(n + 1, vector<int>(LOG, 1)); depth = vector<int>(n + 1); tin = vector<int>(n + 1); tout = vector<int>(n + 1); edge_node = vector<int>(n + 1); lca_dfs(1, -1); } void lca_dfs(int node, int par) { tin[node] = timer++; for(auto [child,id] : adj[node]) { if (child == par) conts; up[child][0] = node; rep1(j, LOG - 1) { up[child][j] = up[up[child][j - 1]][j - 1]; } depth[child] = depth[node] + 1; edge_node[id] = child; lca_dfs(child, node); } tout[node] = timer-1; } int lift(int u, int k) { rep(j, LOG) { if (k & (1 << j)) { u = up[u][j]; } } return u; } int query(int u, int v) { if (depth[u] < depth[v]) swap(u, v); int k = depth[u] - depth[v]; u = lift(u, k); if (u == v) return u; rev(j, LOG - 1, 0) { if (up[u][j] != up[v][j]) { u = up[u][j]; v = up[v][j]; } } u = up[u][0]; return u; } int get_dis(int u, int v) { int lca = query(u, v); return depth[u] + depth[v] - 2 * depth[lca]; } bool is_ances(int u, int v){ return tin[u] <= tin[v] and tout[u] >= tout[v]; } }; void solve(int test_case) { ll n,m,q; cin >> n >> m >> q; vector<pll> edges(n+5); rep1(i,n-1){ ll u,v; cin >> u >> v; adj[u].pb({v,i}), adj[v].pb({u,i}); edges[i] = {u,v}; } vector<ll> a(n+5); rep1(i,n) a[i] = 1; lca_algo LCA(n); fenwick<ll> fenw(n+5); auto edge_change = [&](ll id, ll val){ ll u = LCA.edge_node[id]; fenw.pupd(LCA.tin[u],val); fenw.pupd(LCA.tout[u]+1,-val); }; auto path_sum = [&](ll u, ll v){ assert(LCA.is_ances(u,v)); return fenw.sum(LCA.tin[v])-fenw.sum(LCA.tin[u]); }; auto find_cc = [&](ll u){ ll l = 0, r = LCA.depth[u]; ll mx_ances = -1; while(l <= r){ ll mid = (l+r) >> 1; ll ances = LCA.lift(u,mid); ll len = LCA.depth[u]-LCA.depth[ances]; ll sum = path_sum(ances,u); if(sum == len){ mx_ances = ances; l = mid+1; } else{ r = mid-1; } } assert(mx_ances != -1); return mx_ances; }; vector<bool> active(n+5); vector<ll> edge_sub(n+5); while(m--){ ll id; cin >> id; auto [u,v] = edges[id]; ll pu = find_cc(u), pv = find_cc(v); if(!active[id]){ active[id] = 1; ll new_val = a[pu]+a[pv]-edge_sub[id]; edge_sub[id] = 0; edge_change(id,1); pu = find_cc(u); a[pu] = new_val; } else{ active[id] = 0; edge_sub[id] = a[pu]; edge_change(id,-1); ll pu_new = find_cc(u), pv_new = find_cc(v); a[pu_new] = a[pv_new] = a[pu]; } } while(q--){ ll u; cin >> u; ll pu = find_cc(u); ll ans = a[pu]; cout << ans << endl; } } int main() { fastio; int t = 1; // cin >> t; rep1(i, t) { solve(i); } 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...