This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define pii pair<int, int>
#define piii pair<pii, int>
#define vi vector<int>
#define vvi vector<vi>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define f first
#define s second
#define oo 1000000001
#define eb emplace_back
#define pb push_back
#define mpr make_pair
#define size(v) (int)v.size()
#define ln '\n'
#define ull unsigned long long
#define ll long long
#define all(v) v.begin(), v.end()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct segTree {
int N;
vector<int>tree;
segTree(int n) {
N = n;
tree.assign(4 * N, 0);
}
void add(int node, int l, int r, int pos) {
tree[node]++;
if (l < r) {
int mid = (l + r) >> 1;
if (pos <= mid) {
add(node << 1, l, mid, pos);
} else {
add((node << 1) | 1, mid + 1, r, pos);
}
}
}
int getKth(int node, int l, int r, int k) {
if (l == r) {
return l;
} else {
int mid = (l + r) >> 1;
if (mid - l + 1 - tree[node << 1] >= k) {
return getKth(node << 1, l, mid, k);
} else {
return getKth((node << 1) | 1, mid + 1, r, k - (mid - l + 1 - tree[node << 1]));
}
}
}
};
void dfs(int node, vi &psum, vvi &g, vi &l, vi &r, vpii &best) {
for (int to : g[node]) {
dfs(to, psum, g, l, r, best);
l[node] = min(l[node], l[to]);
r[node] = max(r[node], r[to]);
if (best[to].f > best[node].f) {
best[node] = best[to];
}
}
if (size(g[node]) == 0) { /// leaf node
l[node] = r[node] = node;
best[node] = mpr(0, node);
} else if (psum[r[node] - 1] - (l[node] > 0 ? psum[l[node] - 1] : 0) == 0) { /// late knight has highest rank, +1 win
best[node].f++;
}
}
int GetBestPosition(int N, int C, int R, int *K, int *S, int *E) {
vvi g(2 * N);
int nextNode = N - 1;
vector<int>root(N);
for (int i = 0; i < N; ++i) {
root[i] = i;
}
segTree tree(N);
for (int i = 0; i < C; ++i) {
int node = tree.getKth(1, 0, N - 1, S[i] + 1);
g[++nextNode].eb(root[node]);
root[node] = nextNode;
for (int j = S[i] + 2; j <= E[i] + 1; ++j) {
node = tree.getKth(1, 0, N - 1, S[i] + 2);
tree.add(1, 0, N - 1, node);
g[nextNode].eb(root[node]);
}
}
vector<int>psum(N - 1, 0);
for (int i = 0; i < N - 1; ++i) {
if (K[i] > R) {
psum[i] = 1;
}
}
for (int i = 1; i < N - 1; ++i) {
psum[i] += psum[i - 1];
}
vector<int>l(2 * N, oo), r(2 * N, 0);
vpii best(2 * N, mpr(-1, -1));
dfs(nextNode, psum, g, l, r, best);
return best[nextNode].s;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |