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/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;
}
int dfs(int o, int k) {
Node& n = heap[o];
dbg(n.otr);
int ans = 0;
for (auto& a : n.ch) {
ans = max(ans, dfs(a, k));
}
return ans + (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);
}
return dfs(s.begin()->ind, k) - 1;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |