# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
168857 | Lawliet | Jousting tournament (IOI12_tournament) | C++17 | 182 ms | 30328 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int LOG = 20;
const int MAXN = 200010;
class FenwickTree
{
public:
void update(int i, int v)
{
for( ; i < MAXN ; i += i & -i )
BIT[ i ] += v;
}
int query(int i)
{
int ans = 0;
for( ; i > 0 ; i -= i & -i )
ans += BIT[ i ];
return ans;
}
FenwickTree() { memset( BIT , 0 , sizeof(BIT) ); }
private:
int BIT[MAXN];
};
int n;
int cntNode;
int mx[MAXN];
int prof[MAXN];
int dp[LOG][MAXN];
int virtualNode[MAXN];
vector< int > adj[MAXN];
FenwickTree BIT;
void DFS(int cur)
{
for(int k = 1 ; k < LOG ; k++)
dp[k][cur] = dp[k - 1][ dp[k - 1][cur] ];
for(int i = 0 ; i < adj[cur].size() ; i++)
{
int viz = adj[cur][i];
prof[ viz ] = prof[ cur ] + 1;
DFS( viz );
}
}
int getKthKnight(int k)
{
int l = 1;
int r = n;
while( l < r )
{
int m = ( l + r )/2;
if( BIT.query( m ) >= k ) r = m;
else l = m + 1;
}
return r;
}
int LCA(int U, int V)
{
if( prof[U] < prof[V] ) swap( U , V );
int d = prof[U] - prof[V];
for(int k = 0 ; k < LOG ; k++)
if( d & (1 << k) ) U = dp[k][U];
if( U == V ) return U;
for(int k = LOG - 1 ; k >= 0 ; k--)
{
if( dp[k][U] != dp[k][V] )
{
U = dp[k][U];
V = dp[k][V];
}
}
return dp[0][U];
}
int GetBestPosition(int N, int C, int R, int *K, int *S, int *E)
{
n = N;
cntNode = n;
for(int i = 1 ; i <= n ; i++)
{
virtualNode[i] = i;
BIT.update( i , 1 );
}
for(int i = 0 ; i < C ; i++)
{
cntNode++;
E[i]++; S[i]++;
int sz = E[i] - S[i] + 1;
for(int j = 0 ; j < sz ; j++)
{
int cur = getKthKnight( S[i] );
dp[0][ virtualNode[cur] ] = cntNode;
adj[ cntNode ].push_back( virtualNode[cur] );
if( j != sz - 1 ) BIT.update( cur , -1 );
}
int last = getKthKnight( S[i] );
virtualNode[ last ] = cntNode;
}
DFS( cntNode );
int last = 0;
for(int i = 1 ; i <= n ; i++)
{
int l = LCA( i , last );
mx[i] = prof[i] - prof[l] - 1;
if( last == 0 ) mx[i] = prof[i];
if( K[i - 1] > R ) last = i;
}
last = 0;
int ind;
int ans = -1;
for(int i = n ; i > 0 ; i--)
{
int l = LCA( i , last );
int val = prof[i] - prof[l] - 1;
if( last == 0 ) val = prof[i];
mx[i] = min( mx[i] , val );
if( ans <= mx[i] )
{
ind = i - 1;
ans = mx[i];
}
if( i >= 2 && K[i - 2] > R ) last = i;
}
return ind;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |