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>
#include "teams.h"
using namespace std;
struct Node {
int cnt;
Node *lc, *rc;
Node() : cnt(0), lc(this), rc(this) {}
Node* add(int l, int r, int ind) {
if (ind < l || r <= ind)
return this;
Node* cur = new Node();
if (r - l == 1) {
cur->cnt++;
return cur;
}
int mid = (l + r) >> 1;
cur->lc = lc->add(l, mid, ind);
cur->rc = rc->add(mid, r, ind);
cur->cnt = cur->lc->cnt + cur->rc->cnt;
return cur;
}
int get(Node* prv, int l, int r, int qr) {
if (r <= qr)
return cnt - prv->cnt;
if (qr <= l)
return 0;
int mid = (l + r) >> 1;
return lc->get(prv->lc, l, mid, qr) + rc->get(prv->rc, mid, r, qr);
}
int query(Node* prv, int l, int r, int t) {
if (cnt - prv->cnt < t)
return -1;
if (r - l == 1)
return l;
int mid = (l + r) >> 1;
int lft = lc->cnt - prv->lc->cnt;
if (lft >= t)
return lc->query(prv->lc, l, mid, t);
return rc->query(prv->rc, mid, r, t - lft);
}
Node* sub(Node* prv, int l, int r, int qr) {
if (r <= qr)
return this;
if (qr <= l)
return prv;
int mid = (l + r) >> 1;
Node* cur = new Node();
cur->lc = lc->sub(prv->lc, l, mid, qr);
cur->rc = rc->sub(prv->rc, mid, r, qr);
cur->cnt = cur->lc->cnt + cur->rc->cnt;
return cur;
}
};
int n;
vector<Node*> root;
vector<int> R;
vector<pair<int, int>> vec;
void init(int n, int *A, int *B) {
::n = n;
sort(A, A + n, [B] (int i, int j) {
return B[i] < B[j];
});
sort(B, B + n);
vector<int> L[n];
for (int i = 0; i < n; i++) {
L[A[i] - 1].push_back(i);
vec.emplace_back(A[i], B[i]);
}
root.resize(n + 1);
R.resize(n + 1);
root[0] = new Node();
for (int i = 0; i < n; i++) {
root[i + 1] = root[i];
for (int j : L[i]) {
root[i + 1] = root[i + 1]->add(0, n, j);
}
R[i + 1] = lower_bound(B, B + n, i + 1) - B;
}
}
int can(int M, int K[]) {
sort(K, K + M);
Node* cur = new Node();
for (int i = 0; i < M; i++) {
int t = root[K[i]]->get(cur, 0, n, R[K[i]]);
int qr = root[K[i]]->query(cur, 0, n, t + K[i]);
if (qr == -1 || vec[qr].second < K[i])
return 0;
cur = root[K[i]]->sub(cur, 0, n, qr + 1);
}
return 1;
}
Compilation message (stderr)
teams.cpp: In function 'void init(int, int*, int*)':
teams.cpp:66:32: warning: declaration of 'n' shadows a global declaration [-Wshadow]
66 | void init(int n, int *A, int *B) {
| ^
teams.cpp:61:5: note: shadowed declaration is here
61 | int n;
| ^
teams.cpp:85:49: warning: conversion from 'long int' to '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} may change value [-Wconversion]
85 | R[i + 1] = lower_bound(B, B + n, i + 1) - B;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
# | 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... |