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;
typedef pair <int, int> pii;
int n;
pii range[500010];
int dp[500010];
const int inf = 1e9;
vector <int> g[500010];
struct node {
node *l, *r;
int sum;
node () {
l = NULL;
r = NULL;
sum = 0;
}
void merge() {
sum = 0;
if(l) sum += l -> sum;
if(r) sum += r -> sum;
}
} *t[500010];
typedef node* pnode;
void insert(int x, pnode &cur, int b, int e) {
if(!cur) cur = new node();
if(b == e) {
cur -> sum += 1;
return ;
}
int m = (b + e) >> 1;
if(x <= m) insert(x, cur -> l, b, m);
else insert(x, cur -> r, m+1, e);
}
void correct(pnode &cur, pnode &prev, int b, int e) {
if(!cur) {
cur = prev;
return ;
}
if(b == e) {
cur -> sum += prev -> sum;
return ;
}
int m = (b + e) >> 1;
correct(cur -> l, prev -> l, b, m);
correct(cur -> r, prev -> r, m+1, e);
cur -> merge();
}
void init(pnode &cur, int b, int e) {
cur = new node();
if(b == e) {
return ;
}
int m = (b + e) >> 1;
init(cur -> l, b, m);
init(cur -> r, m+1, e);
}
int count(int x, int y, int k, pnode cur, pnode prev, int b, int e) {
if(b == e) {
return (cur -> sum) - (prev -> sum);
}
int m = (b + e) >> 1;
if(k <= m) {
return ((cur -> r -> sum) - (prev -> r -> sum)) + count(x, y, k, cur -> l, prev -> l, b, m);
} else {
return count(x, y, k, cur -> r, prev -> r, m+1, e);
}
}
int count(int x, int y, int k) {
if(x > y) return 0;
return count(x, y, k, t[y], t[x-1], 1, n);
}
void init(int N, int A[], int B[]) {
n = N;
for(int i = 0; i < n; i++) {
range[i] = pii(A[i], B[i]);
g[A[i]].push_back(B[i]);
}
for(int i = 1; i <= n; i++) {
sort(g[i].begin(), g[i].end());
}
for(int i = 0; i <= n; i++) {
t[i] = NULL;
}
init(t[0], 1, n);
for(int i = 1; i <= n; i++) {
for(auto j : g[i]) {
insert(j, t[i], 1, n);
}
correct(t[i], t[i - 1], 1, n);
}
}
int aux[200010];
int W(int x, int y) {
return count(aux[x] + 1, aux[y], aux[y]);
}
int can(int M, int K[]) {
sort(K, K+M);
int sum = 0;
for(int i = 0; i < M; i++) {
sum += K[i];
aux[i] = K[i];
if(sum > n) return false;
}
vector <int> s;
for(int i = 0; i < M; i++) {
dp[i] = count(1, K[i], K[i]) - K[i];
while(s.size() > 1 && dp[s[s.size() - 2]] + W(s[s.size() - 2], i) <= dp[s[s.size() - 1]] + W(s[s.size() - 1], i)) {
s.pop_back();
}
if(!s.empty()) dp[i] = min(dp[i], dp[s.back()] + W(s.back(), i) - K[i]);
while(!s.empty() && dp[s.back()] >= dp[i]) {
s.pop_back();
}
s.push_back(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... |