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 "teams.h"
#include <bits/stdc++.h>
using namespace std;
const int maxN = 5e5 + 20;
const int maxL = 20;
const int maxM = 2e5 + 20;
vector<int> add[maxN];
int tree[maxN * maxL];
int ch[maxN * maxL][2];
int root[maxN * 4];
int dp[maxM];
int node_cnt;
int N;
int update(int cur_id, int lt, int rt, int pos) {
int new_id = ++node_cnt;
if (lt == rt) {
tree[new_id] = tree[cur_id] + 1;
return new_id;
}
int mt = (lt + rt) / 2;
if (pos <= mt) {
ch[new_id][0] = update(ch[cur_id][0], lt, mt, pos);
ch[new_id][1] = ch[cur_id][1];
}
else {
ch[new_id][0] = ch[cur_id][0];
ch[new_id][1] = update(ch[cur_id][1], mt + 1, rt, pos);
}
tree[new_id] = tree[ch[new_id][0]] + tree[ch[new_id][1]];
return new_id;
}
void init(int _N, int A[], int B[]) {
N = _N;
for (int i = 0; i < N; i++) {
add[B[i]].push_back(A[i]);
}
for (int i = N; i >= 1; i--) {
root[i] = root[i + 1];
for (auto pos: add[i]) {
root[i] = update(root[i], 1, N, pos);
}
}
}
int get(int id, int lt, int rt, int ql, int qr) {
if (lt == ql && rt == qr) {
return tree[id];
}
int mt = (lt + rt) / 2;
if (qr <= mt) {
return get(ch[id][0], lt, mt, ql, qr);
}
else if (ql >= mt + 1) {
return get(ch[id][1], mt + 1, rt, ql, qr);
}
else {
return get(ch[id][0], lt, mt, ql, mt) + get(ch[id][1], mt + 1, rt, mt + 1, qr);
}
}
int get(int lx, int rx, int ly) {
if (lx > rx) {
return 0;
}
return get(root[ly], 1, N, lx, rx);
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> events;
int *K;
void add_bad(int i, int j) {
int lt = K[j] + 1;
int rt = N;
int T = N + 1;
while (lt <= rt) {
int mt = (lt + rt) / 2;
if (get(K[i] + 1, K[j], mt) <= dp[j] - dp[i]) {
T = mt;
rt = mt - 1;
}
else {
lt = mt + 1;
}
}
events.emplace(T, -j - 1);
}
int can(int M, int _K[]) {
K = _K;
long long sum = 0;
for (int i = 0; i < M; i++) {
sum += K[i];
}
if (sum > N) {
return 0;
}
sort(K, K + M);
set<int> S;
while (!events.empty()) {
events.pop();
}
for (int i = 0; i < M; i++) {
events.emplace(K[i], i);
}
while (!events.empty()) {
int id = events.top().second;
events.pop();
if (id >= 0) {
dp[id] = get(1, K[id], K[id]) - K[id];
if (!S.empty()) {
int best = *S.rbegin();
dp[id] = min(dp[id], dp[best] + get(K[best] + 1, K[id], K[id]) - K[id]);
}
if (dp[id] < 0) {
return false;
}
if (!S.empty()) {
add_bad(*S.rbegin(), id);
}
S.insert(id);
}
else {
id = -id - 1;
auto it = S.find(id);
if (it == S.end()) {
continue;
}
if (it != S.begin() && next(it) != S.end()) {
add_bad(*prev(it), *next(it));
}
S.erase(it);
}
}
return true;
}
# | 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... |