# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
105882 | nvmdava | Jousting tournament (IOI12_tournament) | C++17 | 0 ms | 0 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;
#define N 131075
int n;
int r[N];
int lef[N][20];
int logg[N];
void fillsparse(){
for(int i = 1; i <= n; i++){
lef[i][0] = r[i];
for(int j = 1; j < 20; j++){
if(i < (1 << j))
break;
lef[i][j] = max(lef[i][j - 1], lef[i - (1 << (j - 1))][j - 1]);
}
}
logg[1] = 0;
for(int i = 2; i <= 131072; i++)
logg[i] = logg[i / 2] + 1;
}
int getmin(int l, int r){
int sz = logg[r - l + 1];
return max(lef[r][sz], lef[l + (1 << sz) - 1][sz]);
}
struct Fen{
int arr[N];
void update(int id, int val){
for(; id < N; id += id & -id)
arr[id] += val;
};
int sum(int id){
int res = 0;
while(id){
res += arr[id];
id -= id & -id;
}
}
}
Fen fen;
int segTree[2 * N];
#define MX 131072
void update(int id, int l, int r, int L, int R){
if(l == L && r == R){
segTree[id]++;
return;
}
int m = (l + r) >> 1;
if(m < L) update(id << 1 | 1, m + 1, r, L, R);
else if(m >= R) update(id << 1, l, m, L, R);
else {
update(id << 1, l, m, L, m);
update(id << 1 | 1, m + 1, r, m + 1, R);
}
}
int GetBestPosition(int NN, int C, int R, int *K, int *S, int *E) {
n = NN;
for(int i = 1; i < n; i++){
r[i] = S[i - 1];
}
r[n] = -1;
fillsparse();
for(int i = 0; i < C; i++){
int add1, add2;
S[i]++;
E[i]++;
add1 = fen.sum(S[i]);
add2 = fen.sum(E[i]);
fen.upd(S[i], E[i] - S[i]);
S[i] += add1;
E[i] += add2;
if(getmin(S[i], E[i] - 1) < R){
update(1, 1, MX, S[i], E[i]);
}
}
for(int i = 1; i < MX; i++){
segTree[i << 1] += segTree[i];
segTree[i << 1 | 1] += segTree[i];
}
int best = 0;
for(int i = 1; i < n; i++){
if(segTree[MX + i] > segTree[MX + best]){
best = i;
}
}
return best;
}