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"
#define rep(i, n) for(ll i = 0; i < ll(n); ++i)
#define rep2(i, s, n) for(ll i = ll(s); i < ll(n); ++i)
#define rrep(i, n) for(ll i = ll(n)-1; i >= 0; --i)
#define pb push_back
#define eb emplace_back
#define all(a) a.begin(),a.end()
#define SZ(a) int(a.size())
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vp = vector<P>;
using vvp = vector<vp>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vs = vector<string>;
const int inf = 1001001001;
const ll linf = 1001001001001001001;
template<class T>
bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T>
bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
class segtree {
struct node {
int l, r, d;
node *cl;
node *cr;
node(int _l, int _r, int _d) : l(_l), r(_r), d(_d), cl(nullptr), cr(nullptr) {}
};
int n;
node *root;
node *build(int l, int r, const vi::iterator &a, const vi::iterator &b) {
if (a == b) return nullptr;
node *res = new node(l, r, b - a);
if (r - l > 1) {
int m = (r + l) / 2;
auto c = lower_bound(a, b, m);
res->cl = build(l, m, a, c);
res->cr = build(m, r, c, b);
}
return res;
}
int count(int l, int r, node *nd) {
if (nd == nullptr) return 0;
if (r <= nd->l or nd->r <= l) return 0;
if (l <= nd->l and nd->r <= r) return nd->d;
return count(l, r, nd->cl) + count(l, r, nd->cr);
}
public:
segtree(int _n, vi v) : n(_n) {
root = build(0, n, v.begin(), v.end());
}
int count(int l, int r) {
assert(0 <= l and l <= r and r <= n);
return count(l, r, root);
}
};
vector<segtree> st;
int sz = 1;
void build(const vvi &v) {
vvi ls(2 * sz);
rep(i, sz) ls[sz + i] = v[i];
for (int i = sz - 1; i >= 1; --i) {
ls[i].insert(ls[i].end(), all(ls[2 * i]));
ls[i].insert(ls[i].end(), all(ls[2 * i + 1]));
sort(all(ls[i]));
}
rep(i, 2 * sz) st.pb(segtree(sz, ls[i]));
}
int count(int xl, int xr, int yl, int yr) {
assert(0 <= xl and xl <= xr and xr <= sz);
assert(0 <= yl and yl <= yr and yr <= sz);
// cerr << xl << ' ' << xr << ' ' << yl << ' ' << yr << ' ';
yl += sz, yr += sz;
int res = 0;
while (yl < yr) {
if (yl & 1) res += st[yl++].count(xl, xr);
if (yr & 1) res += st[--yr].count(xl, xr);
yl >>= 1, yr >>= 1;
}
// cerr << res << endl;
return res;
}
int n;
vi a, b;
void init(int N, int A[], int B[]) {
n = N;
a = vi(A, A + N);
b = vi(B, B + N);
for (int &i: b) ++i;
while (sz <= n + 1) sz *= 2;
vvi v(sz);
rep(i, n) v[b[i]].pb(a[i]);
rep(i, sz) sort(all(v[i]));
build(v);
// cerr << "init : " << N << endl;
}
int can(int m, int K[]) {
vi k = vi(K, K + m);
sort(all(k));
// {x, max_y, rem in max_y}
vvi v;
v.pb({0, n + 1, 0});
for (int i: k) {
// cerr << "i : " << i << endl;
// for (auto t: v) cerr << "v : " << t[0] << ' ' << t[1] << ' ' << t[2] << endl;
while (v.back()[1] < i + 1) v.pop_back();
int now = i + 1;
int rem = i;
while (true) {
if (now > n + 1) return 0;
assert(v.back()[1] >= now);
int sum = count(v.back()[0] + 1, i + 1, now, v.back()[1] + 1) + v.back()[2];
if (sum < rem) {
rem -= sum;
now = v.back()[1] + 1;
v.pop_back();
continue;
}
int psum = count(v.back()[0] + 1, i + 1, now, v.back()[1]);
if (psum < rem) {
v.back()[0] = i;
v.back()[2] += sum - rem;
} else {
int ok = v.back()[1], ng = now;
auto f = [&](int x) -> bool {
return count(v.back()[0] + 1, i + 1, now, x) >= rem;
};
while (ok - ng > 1) {
int mid = (ok + ng) / 2;
if (f(mid)) ok = mid;
else ng = mid;
}
v.eb(vi{i, ok - 1, count(v.back()[0] + 1, i + 1, now, ok) - rem});
}
break;
}
}
// cerr << "i : end" << endl;
// for (auto t: v) cerr << "v : " << t[0] << ' ' << t[1] << ' ' << t[2] << endl;
return 1;
}
Compilation message (stderr)
teams.cpp: In member function 'segtree::node* segtree::build(int, int, const iterator&, const iterator&)':
teams.cpp:57:38: warning: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
57 | node *res = new node(l, r, b - a);
| ~~^~~
# | 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... |