이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define type(x) __typeof((x).begin())
#define foreach(it, x) for(type(x) it = (x).begin(); it != (x).end(); it++)
typedef long long ll;
typedef pair < int, int > ii;
const int inf = 1e9 + 333;
const ll linf = 1e18 + inf;
#include "teams.h"
const int N = 5e5 + 5;
ii a[N];
class node{ public:
int x;
node *l, *r;
node() {
x = 0;
l = r = 0;
}
};
typedef node* pNode;
pNode t[N];
pNode insert(pNode x, int l, int r, int x1) {
pNode nw = new node;
if(l == r) {
nw -> x = 1;
return nw;
}
int m = l + r >> 1;
if(x1 <= m) {
nw -> l = insert(x -> l, l, m, x1);
nw -> r = x -> r;
}
else {
nw -> l = x -> l;
nw -> r = insert(x -> r, m + 1, r, x1);
}
nw -> x = nw -> l -> x + nw -> r -> x;
return nw;
}
int n;
void init(pNode x, int l, int r) {
if(l == r)
return;
int m = l + r >> 1;
x -> l = new node;
x -> r = new node;
init(x -> l, l, m);
init(x -> r, m + 1, r);
}
void init(int N, int A[], int B[]) {
n = N;
for(int i = 1; i <= n; i++) {
a[i] = ii(B[i - 1], A[i - 1]);
}
sort(a + 1, a + n + 1);
vector < ii > vs;
for(int i = 1; i <= n; i++)
vs.push_back(ii(a[i].second, i));
sort(vs.begin(), vs.end());
int ptr = 0;
t[0] = new node;
init(t[0], 1, n);
for(int i = 1; i <= n; i++) {
t[i] = t[i - 1];
//printf("vs = %d ", i);
while(ptr < vs.size() and vs[ptr].first <= i) {
//printf("%d ", vs[ptr].second);
t[i] = insert(t[i], 1, n, vs[ptr].second);
ptr++;
}
//puts("");
}
}
int get(pNode x, pNode del, int l, int r, int x1, int x2, int k) {
if(x1 <= l and r <= x2 and x -> x - del -> x <= k) {
int ret = x -> x - del -> x;
del -> x += ret;
return ret;
}
if(x2 < l or r < x1 or l == r or del -> x == x -> x)
return 0;
int m = l + r >> 1;
int lf = get(x -> l, del -> l, l, m, x1, x2, k);
if(lf == k) {
del -> x = del -> l -> x + del -> r -> x;
return k;
}
k -= lf;
int rf = get(x -> r, del -> r, m + 1, r, x1, x2, k);
del -> x = del -> l -> x + del -> r -> x;
return lf + rf;
}
void clear(pNode x) {
if(x -> x and x -> l)
clear(x -> l);
if(x -> x and x -> r)
clear(x -> r);
x -> x = 0;
}
int can(int M, int K[]) {
sort(K, K + M);
int sum = 0;
for(int i = 0; i < M; i++) {
if(sum + K[i] > n)
return 0;
sum += K[i];
}
clear(t[0]);
pNode del = t[0];
for(int i = 0; i < M; i++) {
int id = lower_bound(a + 1, a + n + 1, ii(K[i], 0)) - a;
//printf("id = %d ", id);
int res = get(t[K[i]], del, 1, n, id, n, K[i]);
//printf("res = %d\n", res);
if(res != K[i])
return 0;
}
return 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... |