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;
namespace std {
template <class Fun> class y_combinator_result {
Fun fun_;
public:
template <class T>
explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)) {}
template <class... Args> decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template <class Fun> decltype(auto) yc(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
} // namespace std
typedef long long ll;
// #define int ll
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define PB push_back
#define FS first
#define SD second
#define ary(k) array<int, k>
template <class A, class B> void cmx(A &x, B y) { x = max<A>(x, y); }
template <class A, class B> void cmn(A &x, B y) { x = min<A>(x, y); }
typedef pair<int, int> pii;
typedef vector<int> vi;
const int MM = 5e5 + 5;
struct Node {
int l, r, sum;
} seg[MM * 30];
int ti = 0;
int build(int l, int r) {
int u = ti++;
seg[u].sum = 0;
if (l != r) {
int mid = (l + r) / 2;
seg[u].l = build(l, mid);
seg[u].r = build(mid + 1, r);
}
return u;
};
int inc(int u, int l, int r, int x) {
int nu = ti++;
seg[nu] = seg[u];
if (l == r) {
seg[nu].sum++;
return nu;
}
int mid = (l + r) / 2;
if (x <= mid)
seg[nu].l = inc(seg[nu].l, l, mid, x);
else if (x > mid)
seg[nu].r = inc(seg[nu].r, mid + 1, r, x);
seg[nu].sum = seg[seg[nu].l].sum + seg[seg[nu].r].sum;
return nu;
};
int sum(int u, int l, int r, int ql, int qr) {
if (l == ql && r == qr)
return seg[u].sum;
int mid = (l + r) / 2;
if (qr <= mid)
return sum(seg[u].l, l, mid, ql, qr);
else if (ql > mid)
return sum(seg[u].r, mid + 1, r, ql, qr);
else
return sum(seg[u].l, l, mid, ql, mid) +
sum(seg[u].r, mid + 1, r, mid + 1, qr);
};
int n, ver[MM];
void init(int N, int A[], int B[]) {
n = N;
vector<vi> ls(n + 1);
vector<pii> in;
rep(i, 0, n) {
int l = A[i], r = B[i];
ls[r].PB(l);
}
int root = build(1, n);
for (int i = n; i >= 1; i--) {
for (int l : ls[i]) {
root = inc(root, 1, n, l);
}
ver[i] = root;
}
}
int cnt[MM];
int can(int M, int K[]) {
int m = M;
vi vals;
rep(i, 0, m) {
int x = K[i];
vals.PB(x);
cnt[x]++;
}
if (accumulate(all(vals), 0ll) > n) {
return 0;
}
vals.PB(0);
sort(all(vals));
vals.erase(unique(all(vals)), vals.end());
vi good = {0}, goodv = {0};
bool done = 0;
rep(i, 1, sz(vals)) {
int x = vals[i];
int tot = cnt[x] * x;
int cur = 1e9;
rep(k, 0, sz(good)) {
int add = sum(ver[x], 1, n, goodv[k] + 1, x) - tot;
cmn(cur, good[k] + add);
}
if (cur < 0) {
done = 1;
break;
}
while (sz(good) && good.back() >= cur) {
good.pop_back();
goodv.pop_back();
}
good.PB(cur);
goodv.PB(vals[i]);
}
for (int x : vals)
cnt[x] = 0;
return !done;
}
// delta = sumpeople - sumsizes
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |