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;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
#define pb push_back
#define f first
#define s second
namespace debug {
const int DEBUG = true;
template<class T1, class T2>
void pr(const pair<T1, T2> &x);
template<class T, size_t SZ>
void pr(const array<T, SZ> &x);
template<class T>
void pr(const vector<T> &x);
template<class T>
void pr(const set<T> &x);
template<class T1, class T2>
void pr(const map<T1, T2> &x);
template<class T>
void pr(const T &x) { if (DEBUG) cout << x; }
template<class T, class... Ts>
void pr(const T &first, const Ts &... rest) { pr(first), pr(rest...); }
template<class T1, class T2>
void pr(const pair<T1, T2> &x) { pr("{", x.f, ", ", x.s, "}"); }
template<class T>
void prIn(const T &x) {
pr("{");
bool fst = 1;
for (auto &a : x) {
pr(fst ? "" : ", ", a), fst = 0;
}
pr("}");
}
template<class T, size_t SZ>
void pr(const array<T, SZ> &x) { prIn(x); }
template<class T>
void pr(const vector<T> &x) { prIn(x); }
template<class T>
void pr(const set<T> &x) { prIn(x); }
template<class T1, class T2>
void pr(const map<T1, T2> &x) { prIn(x); }
void ps() { pr("\n"), cout << flush; }
template<class Arg, class... Args>
void ps(const Arg &first, const Args &... rest) {
pr(first, " ");
ps(rest...);
}
}
using namespace debug;
bool success = true;
int standardNx;
namespace PSegTr {
static const int MAXN = 500000, NODES = 40 * MAXN;
// different definition of lz to nicely implement persistence
// lz is only for children, node which it is on already contains updated value
int lC[NODES], rC[NODES], tr[NODES];
int nx = 0;
int cpy(int o) {
int n = nx++;
lC[n] = lC[o], rC[n] = rC[o], tr[n] = tr[o];
return n;
}
// q does not create any new nodes
int q(int i, int l, int r, int s, int e) {
if (e < l || r < s) {
return 0;
}
if (s <= l && r <= e) {
return tr[i];
}
int mid = (l + r) / 2;
return q(lC[i], l, mid, s, e) +
q(rC[i], mid + 1, r, s, e);
}
// returns index of updated node
int u(int i, int l, int r, int x, int d) {
i = cpy(i);
if (l == r) {
tr[i] += d;
return i;
}
int mid = (l + r) / 2;
if (x <= mid) {
lC[i] = u(lC[i], l, mid, x, d);
} else {
rC[i] = u(rC[i], mid + 1, r, x, d);
}
tr[i] = tr[lC[i]] + tr[rC[i]];
return i;
}
int bEmpty(int l, int r) {
int i = nx++;
if (l == r) {
tr[i] = 0;
return i;
}
int mid = (l + r) / 2;
lC[i] = bEmpty(l, mid);
rC[i] = bEmpty(mid + 1, r);
tr[i] = tr[lC[i]] + tr[rC[i]];
assert(tr[i] == 0);
return i;
}
int clr() {
memset(lC, 0, sizeof(lC)), memset(rC, 0, sizeof(rC)), memset(tr, 0, sizeof(tr));
nx = 0;
}
int useK(int n, int u, int l, int r, int k) {
// ps("l, r, k:", l, r, k);
if(k == 0){
return u;
}
int rem = tr[n] - tr[u];
if (rem < k) {
success = false;
return u;
}
u = cpy(u);
tr[u] += k;
if (l == r) {
return u;
}
int remL = tr[lC[n]] - tr[lC[u]];
int mid = (l + r) / 2;
if (remL >= k) {
lC[u] = useK(lC[n], lC[u], l, mid, k);
} else {
lC[u] = lC[n]; // all of it is used, interesting how you can copy subtrees with persistence
rC[u] = useK(rC[n], rC[u], mid + 1, r, k - max(0, remL));
}
return u;
}
// cut is exclusive
int remB(int i, int l, int r, int cut) {
i = cpy(i);
if (l == r) {
tr[i] = 0;
return i;
}
int mid = (l + r) / 2;
if (cut <= mid) {
lC[i] = remB(lC[i], l, mid, cut);
} else {
lC[i] = cpy(lC[i]);
lC[i] = 0; // don't need to worry about children as we shouldn't ever descend into it again
rC[i] = remB(rC[i], mid + 1, r, cut);
}
tr[i] = tr[lC[i]] + tr[rC[i]];
return i;
}
vi trees;
void uTrees(int x, int d) {
trees.pb(u(trees.back(), 1, MAXN, x, d));
}
int qTrees(int t, int s, int e) {
return q(trees[t], 1, MAXN, s, e);
}
vector<vi> layers;
int printTr(int x, int l, int r, int d) {
layers[d].pb(tr[x]);
if (l != r) {
int mid = (l + r) / 2;
printTr(lC[x], l, mid, d + 1);
printTr(rC[x], mid + 1, r, d + 1);
}
}
void printTr(int x) {
layers.clear();
layers.resize(10);
printTr(x, 1, MAXN, 0);
for (int i = 0; i < 10; i++) {
ps(layers[i]);
}
}
}
using namespace PSegTr;
int N;
vector<pi> pts;
int init(int iN, int a[], int b[]) {
N = iN;
for (int i = 0; i < N; i++) {
pts.pb({a[i], b[i]});
}
pts.pb({-1, -1});
sort(pts.begin(), pts.end());
clr();
trees.pb(bEmpty(1, MAXN));
for (int i = 1; i <= N; i++) {
uTrees(pts[i].s, +1);
}
// printTr(trees[1]);
standardNx = nx;
}
int can(int M, int k[]) {
success = true;
nx = standardNx; // save space by reusing elements
sort(k, k + M);
// ps(allK);
int used = bEmpty(1, MAXN);
for (int i = 0; i < M; i++) {
int rMostValid = upper_bound(pts.begin(), pts.end(), make_pair(k[i], (int) 1e8)) - pts.begin() - 1;
int options = trees[rMostValid];
// ps(rMostValid);
// ps("before cut");
// printTr(options);
options = k[i] - 1 > 0 ? remB(options, 1, MAXN, k[i] - 1) : options;
// ps("options", i);
// printTr(options);
used = k[i] - 1 > 0 ? remB(used, 1, MAXN, k[i] - 1) : used;
// ps("k[i]:", k[i]);
used = useK(options, used, 1, MAXN, k[i]);
if (success == false) {
// ps("failed on:", i);
return 0;
}
// ps("used");
// printTr(used);
}
return 1;
}
Compilation message (stderr)
teams.cpp: In function 'int PSegTr::clr()':
teams.cpp:142:5: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
teams.cpp: In function 'int PSegTr::printTr(int, int, int, int)':
teams.cpp:212:5: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
teams.cpp: In function 'int init(int, int*, int*)':
teams.cpp:247:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
teams.cpp: In function 'int can(int, int*)':
teams.cpp:258:104: warning: conversion to 'int' from '__gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int> > >::difference_type {aka long int}' may alter its value [-Wconversion]
int rMostValid = upper_bound(pts.begin(), pts.end(), make_pair(k[i], (int) 1e8)) - pts.begin() - 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |