This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*************************************
* author: marvinthang *
* created: 06.03.2023 21:08:07 *
*************************************/
#include "teams.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define REPD(i, n) for (int i = (n); i--; )
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; )
#define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
#include "debug.h"
#else
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define DB(...) 23
#define db(...) 23
#define debug(...) 23
#endif
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> scan_op(pair <U, V>) { return in >> u.fi >> u.se; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
static char buf[450 << 20];
void* operator new(size_t s) {
static size_t i = sizeof buf;
assert(s < i);
return (void*)&buf[i -= s];
}
void operator delete(void*) {}
struct Node {
Node *lc, *rc;
int sum;
Node(int v = 0): lc(nullptr), rc(nullptr), sum(v) {}
Node(Node *l, Node *r): lc(l), rc(r), sum(0) {
if (l != nullptr) sum += l->sum;
if (r != nullptr) sum += r->sum;
}
};
Node *build(int l, int r) {
if (l == r) return new Node();
int m = l + r >> 1;
return new Node(build(l, m), build(m + 1, r));
}
Node *update(Node *cur, int l, int r, int p, int v) {
if (l == r) return new Node(cur->sum + v);
int m = l + r >> 1;
if (p <= m) return new Node(update(cur->lc, l, m, p, v), cur->rc);
return new Node(cur->lc, update(cur->rc, m + 1, r, p, v));
}
int get_sum(Node *cur, int l, int r, int u, int v) {
if (l > v || r < u) return 0;
if (u <= l && r <= v) return cur->sum;
int m = l + r >> 1;
return get_sum(cur->lc, l, m, u, v) + get_sum(cur->rc, m + 1, r, u, v);
}
// end of template
const int MAX = 5e5 + 5;
int N, pref[MAX];
vector <int> rightAt[MAX];
Node *versions[MAX];
void init(int n, int A[], int B[]) {
N = n;
REP(i, N) rightAt[B[i]].push_back(A[i]);
versions[N + 1] = build(1, N);
FORDE(r, N, 1) {
versions[r] = versions[r + 1];
for (int l: rightAt[r]) versions[r] = update(versions[r], 1, N, l, 1);
}
}
int cnt[MAX], dp[MAX];
int can(int M, int K[]) {
long long sum = 0;
REP(i, M) sum += K[i];
if (sum > N) return 0;
REP(i, M) ++cnt[K[i]];
vector <pair <int, int>> v;
REP(i, M) if (cnt[K[i]]) {
v.push_back(make_pair(K[i], K[i] * cnt[K[i]]));
cnt[K[i]] = 0;
}
v.emplace_back(0, 0);
sort(ALL(v));
stack <pair <int, int>> st;
st.emplace(0, N + 2);
auto getTime = [&] (int i, int j) {
int l = v[j].fi + 1, r = N;
while (l <= r) {
int m = l + r >> 1;
if (get_sum(versions[m], 1, N, v[i].fi + 1, v[j].fi) < dp[j] - dp[i]) r = m - 1;
else l = m + 1;
}
return l;
};
FOR(i, 1, v.size()) {
while (st.top().se <= v[i].fi) st.pop();
dp[i] = dp[st.top().fi] + get_sum(versions[v[i].fi], 1, N, v[st.top().fi].fi + 1, v[i].fi) - v[i].se;
if (dp[i] < 0) return 0;
while (dp[i] < dp[st.top().fi]) st.pop();
int t = getTime(st.top().fi, i);
while (st.top().se <= t) {
st.pop();
t = getTime(st.top().fi, i);
}
st.emplace(i, t);
}
return 1;
}
#ifdef LOCAL
#include <stdio.h>
#include <stdlib.h>
static char _buffer[1024];
static int _currentChar = 0;
static int _charsNumber = 0;
static FILE *_inputFile, *_outputFile;
static inline int _read() {
if (_charsNumber < 0) {
exit(1);
}
if (!_charsNumber || _currentChar == _charsNumber) {
_charsNumber = (int)fread(_buffer, sizeof(_buffer[0]), sizeof(_buffer), _inputFile);
_currentChar = 0;
}
if (_charsNumber <= 0) {
return -1;
}
return _buffer[_currentChar++];
}
static inline int _readInt() {
int c, x, s;
c = _read();
while (c <= 32) c = _read();
x = 0;
s = 1;
if (c == '-') {
s = -1;
c = _read();
}
while (c > 32) {
x *= 10;
x += c - '0';
c = _read();
}
if (s < 0) x = -x;
return x;
}
int main() {
_inputFile = fopen("teams.in", "rb");
_outputFile = fopen("teams.out", "w");
int N;
N = _readInt();
int *A = (int*)malloc(sizeof(int)*(unsigned int)N);
int *B = (int*)malloc(sizeof(int)*(unsigned int)N);
for (int i = 0; i < N; ++i) {
A[i] = _readInt();
B[i] = _readInt();
}
init(N, A, B);
int Q;
Q = _readInt();
for (int i = 0; i < Q; ++i) {
int M;
M = _readInt();
int *K = (int*)malloc(sizeof(int)*(unsigned int)M);
for (int j = 0; j < M; ++j) {
K[j] = _readInt();
}
fprintf(_outputFile,"%d\n", can(M, K));
}
return 0;
}
#endif
Compilation message (stderr)
teams.cpp: In function 'Node* build(int, int)':
teams.cpp:66:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
66 | int m = l + r >> 1;
| ~~^~~
teams.cpp: In function 'Node* update(Node*, int, int, int, int)':
teams.cpp:72:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
72 | int m = l + r >> 1;
| ~~^~~
teams.cpp: In function 'int get_sum(Node*, int, int, int, int)':
teams.cpp:80:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
80 | int m = l + r >> 1;
| ~~^~~
teams.cpp: In lambda function:
teams.cpp:122:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
122 | int m = l + r >> 1;
| ~~^~~
teams.cpp: In function 'int can(int, int*)':
teams.cpp:128:18: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
22 | #define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
| ~~~
......
128 | FOR(i, 1, v.size()) {
teams.cpp:22:54: note: in definition of macro 'FOR'
22 | #define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
| ^
teams.cpp: At global scope:
teams.cpp:52:6: warning: the program should also define 'void operator delete(void*, std::size_t)' [-Wsized-deallocation]
52 | void operator delete(void*) {}
| ^~~~~~~~
# | 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... |