Submission #805216

#TimeUsernameProblemLanguageResultExecution timeMemory
805216aymanrsJousting tournament (IOI12_tournament)C++14
0 / 100
46 ms19820 KiB
#include <bits/stdc++.h> using namespace std; int f(int i, int r[]){ if(r[i] == i) return i; return r[i] = f(r[i], r); } void add(int i, int BIT[], int n){ while(i <= n){ BIT[i]++; i += i&-i; } } void sub(int i, int BIT[], int n){ while(i <= n){ BIT[i]--; i += i&-i; } } int que(int i, int BIT[]){ int ans = 0; while(i){ ans += BIT[i]; i -= i&-i; } return ans; } const int L = 18; struct node { vector<node*> l; node* anc[L]; node* root = NULL; int d; }; node* roo; void dfs(node* n, node* p, int d){ n->root = roo; n->d = d; n->anc[0] = p; for(int i = 1;i < L;i++) n->anc[i] = n->anc[i-1]->anc[i-1]; for(node* c : n->l) dfs(c, n, d+1); } int lca(node* u, node* v){ if(u->root != v->root) return u->d; if(u->d < v->d) swap(u, v); int d = u->d - v->d; for(int i = 0;i < L;i++) if(d&(1<<i)) u = u->anc[i]; if(u == v) return d-1; for(int i = L-1;i >= 0;i--) { if(u->anc[i] != v->anc[i]){ d += 1<<i; u = u->anc[i]; v = v->anc[i]; } } return d; } int fs(int s, int BIT[], int N){ s++; int l = 1, r = N, m, b = N; while(l <= r){ m = l+r>>1; if(que(m, BIT) >= s){ b = m; r = m-1; } else l = m+1; } return b-1; } int GetBestPosition(int N, int C, int R, int *K, int *S, int *E) { int r[N+1], BIT[N+1] = {0}, a[N], pr[N], nx[N]; vector<node> g(N); r[N] = N; int c = -1; for(int i = 0;i < N;i++){ r[i] = a[i] = i; add(i+1, BIT, N); } for(int i = 0;i < N-1;i++){ if(K[i] > R) c = i; pr[i] = c; } c = -1; for(int i = N-2;i >= 0;i--){ if(K[i] > R) c = i; nx[i] = c; } for(int i = 0;i < C;i++){ g.emplace_back(); node* nw = &g[g.size()-1]; int s = fs(S[i], BIT, N), e = fs(E[i], BIT, N), cur = f(s+1, r); while(cur <= e){ nw->l.push_back(&g[a[cur]]); sub(cur, BIT, N); cur = r[cur] = f(cur+1, r); } nw->l.emplace_back(&g[a[s]]); a[s] = g.size()-1; } for(int i = g.size()-1;i >= 0;i--){ if(!g[i].root) { roo = &g[i]; dfs(&g[i], &g[i], 0); } } int mx = -1, mc = -1; for(int i = 0;i < N;i++){ int t = g[i].d; if(i < N-1 && nx[i] != -1) t = min(t, lca(&g[i], &g[nx[i]+1])); if(i && pr[i-1] != -1) t = min(t, lca(&g[i], &g[pr[i-1]])); if(t > mx){ mx = t; mc = i; } } return mc; }

Compilation message (stderr)

tournament.cpp: In function 'int fs(int, int*, int)':
tournament.cpp:61:10: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   61 |     m = l+r>>1;
      |         ~^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...