이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/extc++.h"
using namespace std;
template <typename T>
void dbgh(const T& t) {
cerr << t << endl;
}
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t << " | ";
dbgh(u...);
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
dbgh(__VA_ARGS__);
#else
#define dbg(...)
#define cerr \
if (false) \
cerr
#endif
#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())
namespace __gnu_pbds {
template <typename T>
using ost =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
}
const int maxn = 1e5 + 5;
int iarr[maxn];
struct Node {
int ind, opt, otr;
vector<int> ch;
bool operator<(const Node& n) const {
return iarr[opt] < iarr[n.opt];
}
} heap[maxn * 2];
int hind = 0;
Node& alloc() {
Node& n = heap[hind];
n.ind = hind;
n.opt = n.otr = -1;
hind++;
return n;
}
pair<int, int> ans;
void dfs(int o, int k, int add) {
Node& n = heap[o];
if (!sz(n.ch)) {
ans = max(ans, pair<int, int> {add, -iarr[n.opt]});
}
for (auto& a : n.ch) {
dfs(a, k, add + (k >= n.otr));
}
}
int GetBestPosition(int n, int m, int k, int* arr, int* arrl, int* arrr) {
for (int i = 0; i < n - 1; i++) {
iarr[arr[i]] = i;
}
iarr[k] = n - 1;
__gnu_pbds::ost<Node> s;
for (int i = 0; i < n - 1; i++) {
Node& n = alloc();
n.opt = arr[i];
s.insert(n);
}
{
Node& n = alloc();
n.opt = k;
s.insert(n);
}
for (int i = 0; i < m; i++) {
int l = arrl[i], r = arrr[i];
Node& n = alloc();
for (int j = 0; j < r - l + 1; j++) {
auto it = s.find_by_order(l);
Node m = *it;
s.erase(it);
n.ch.push_back(m.ind);
n.opt = max(n.opt, m.opt);
if (j == r - l) {
n.otr = max(n.otr, m.otr);
} else {
n.otr = max(n.otr, n.opt);
}
}
s.insert(n);
}
dfs(s.begin()->ind, k, 0);
return -ans.second;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |