# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
684580 | stevancv | Jousting tournament (IOI12_tournament) | C++14 | 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>
#include "tournament.h"
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
const int inf = 1e9;
int l[N], r[N], ans[N];
struct Bit {
int bit[N];
void Add(int x, int y) {
x += 1;
for (; x < N; x += x & (-x)) bit[x] += y;
}
int Get(int x) {
x += 1;
int ans = 0;
for (; x > 0; x -= x & (-x)) ans += bit[x];
return ans;
}
}f;
vector<int> b;
bool Has(int x, int y) {
auto it = lower_bound(b.begin(), b.end(), x);
if (it == b.end()) return false;
return *it < y;
}
int GetBestPosition(int n, int q, int k, int *p, int *s, int *e) {
for (int i = 0; i < n - 1; i++) if (p[i] > k) b.push_back(i);
for (int i = 0; i < n; i++) l[i] = r[i] = i;
for (int i = 0; i < q; i++) {
int idx = s[i] + f.Get(s[i]); int idy = e[i] + f.Get(e[i]);
r[idx] = r[idy];
f.Add(l[idx] + 1, e[i] - s[i]);
if (!Has(l[idx], r[idy])) {
ans[l[idx]]++;
ans[r[idy] + 1]--;
}
}
int pos = 0;
for (int i = 0; i < n; i++) {
if (i > 0) ans[i] += ans[i - 1];
if (ans[i] > ans[pos]) pos = i;
}
return pos;
}