# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1052664 | thomas_li | Teams (IOI15_teams) | C++17 | 0 ms | 0 KiB |
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>
using namespace std;
namespace std {
template <class Fun> class y_combinator_result {
Fun fun_;
public:
template <class T>
explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)) {}
template <class... Args> decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template <class Fun> decltype(auto) yc(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
} // namespace std
typedef long long ll;
// #define int ll
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define PB push_back
#define FS first
#define SD second
#define ary(k) array<int, k>
template <class A, class B> void cmx(A &x, B y) { x = max<A>(x, y); }
template <class A, class B> void cmn(A &x, B y) { x = min<A>(x, y); }
typedef pair<int, int> pii;
typedef vector<int> vi;
signed main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
struct Node {
int l, r, sum;
};
vector<Node> seg;
auto build = yc([&](auto &&self, int l, int r) -> int {
int u = sz(seg);
seg.emplace_back();
seg[u].sum = 0;
if (l != r) {
int mid = (l + r) / 2;
seg[u].l = self(l, mid);
seg[u].r = self(mid + 1, r);
}
return u;
});
auto inc = yc([&](auto &&self, int u, int l, int r, int x) -> int {
int nu = sz(seg);
seg.PB(seg[u]);
if (l == r) {
seg[nu].sum++;
return nu;
}
int mid = (l + r) / 2;
if (x <= mid)
seg[nu].l = self(seg[nu].l, l, mid, x);
else if (x > mid)
seg[nu].r = self(seg[nu].r, mid + 1, r, x);
seg[nu].sum = seg[seg[nu].l].sum + seg[seg[nu].r].sum;
return nu;
});
auto sum = yc([&](auto &&self, int u, int l, int r, int ql, int qr) -> int {
if (l == ql && r == qr)
return seg[u].sum;
int mid = (l + r) / 2;
if (qr <= mid)
return self(seg[u].l, l, mid, ql, qr);
else if (ql > mid)
return self(seg[u].r, mid + 1, r, ql, qr);
else
return self(seg[u].l, l, mid, ql, mid) +
self(seg[u].r, mid + 1, r, mid + 1, qr);
});
int n;
cin >> n;
vector<vi> ls(n + 1);
vector<pii> in;
rep(i, 0, n) {
int l, r;
cin >> l >> r;
ls[r].PB(l);
}
int root = build(1, n);
vi ver(n + 1);
for (int i = n; i >= 1; i--) {
for (int l : ls[i]) {
root = inc(root, 1, n, l);
}
ver[i] = root;
}
int q;
cin >> q;
while (q--) {
int m;
cin >> m;
vi vals;
rep(i, 0, m) {
int x;
cin >> x;
vals.PB(x);
}
if (accumulate(all(vals), 0ll) > n) {
cout << "0\n";
continue;
}
sort(all(vals));
vi dp = {0}, unq = {0};
rep(i, 0, sz(vals)) {
int j = i;
while (j + 1 < sz(vals) && vals[j + 1] == vals[j])
j++;
int num = j - i + 1;
int tot = num * vals[i];
int cur = 1e9;
rep(k, 0, sz(dp)) {
int add = sum(ver[vals[i]], 1, n, unq[k] + 1, vals[i]) - tot;
/*
for (auto [l, r] : in)
// l in [unq[k]+1,vals[i]],r in [vals[i],n]
if (l > unq[k] && r >= vals[i] && vals[i] >= l) {
add++;
}*/
cmn(cur, dp[k] + add);
}
dp.PB(cur);
// cerr << vals[i] << " " << cur << " " << tot << "\n";
unq.PB(vals[i]);
i = j;
}
cout << (*min_element(all(dp)) >= 0) << "\n";
}
}
// delta = sumpeople - sumsizes