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 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);
}
int dp[maxM];
int can(int M, int K[]) {
long long sum = 0;
for (int i = 0; i < M; i++) {
sum += K[i];
}
if (sum > N) {
return 0;
}
sort(K, K + M);
for (int i = 0; i < M; i++) {
dp[i] = get(1, K[i], K[i]) - K[i];
for (int j = 0; j < i; j++) {
dp[i] = min(dp[i], dp[j] + get(K[j] + 1, K[i], K[i]) - K[i]);
}
if (dp[i] < 0) {
return false;
}
}
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... |