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"
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7, N = 5e5 + 7;
int n;
int *l, *r;
int p[N], rp[N];
namespace wt { // wavelet tree
int a[N]; // a is a permutation
int b[N]; // sorted a
int t[20][N], p[20][N];
void build(int tl, int tr, int lv) {
if(tr - tl == 1) return;
int tm = (tl + tr) / 2, mid = b[tm];
int lbuf = tl, rbuf = tm;
for(int i = tl; i < tr; i++) {
if(t[lv][i] < mid) t[lv + 1][lbuf++] = t[lv][i], p[lv][i] = 1;
else t[lv + 1][rbuf++] = t[lv][i];
p[lv][i] += i ? p[lv][i - 1] : 0;
}
build(tl, tm, lv + 1);
build(tm, tr, lv + 1);
}
void build() {
for(int i = 0; i < n; i++) {
b[i] = i;
t[0][i] = a[i];
}
build(0, n, 0);
}
int qry(int l, int r, int x, int tl = 0, int tr = n, int lv = 0) { // [l, r], [tl, tr)
if(b[tl] >= x || l > r) return 0;
if(b[tr - 1] < x) return r - l + 1;
int tm = (tl + tr) / 2;
int r_l = p[lv][r] - (tl ? p[lv][tl - 1] : 0);
int l_l = (l ? p[lv][l - 1] : 0) - (tl ? p[lv][tl - 1] : 0);
return qry(tl + l_l, tl + r_l - 1, x, tl, tm, lv + 1)
+ qry(tm + l - 1 - tl + 1 - l_l, tm + r - tl + 1 - r_l - 1, x, tm, tr, lv + 1);
}
int find_who(int l, int r, int at_least, int need) { // [l, r]
int lb = at_least, rb = n - 1;
while(lb <= rb) {
int mb = (lb + rb) / 2;
if(qry(l, r, mb + 1) >= need + 1) rb = mb - 1;
else lb = mb + 1;
}
return lb;
}
}
V<pi> aux;
int as_lb[N], as_rb[N];
int qry(int lb, int rb, int x) { // # of elements < x in [lb, rb]
lb = as_lb[lb], rb = as_rb[rb];
if(lb > rb) return 0;
return wt::qry(lb, rb, x); // [lb, rb]
}
int find_who(int lb, int rb, int at_least, int need) {
lb = as_lb[lb], rb = as_rb[rb];
assert(lb <= rb);
return wt::find_who(lb, rb, at_least, need); // [lb, rb]
}
void init(int _n, int _l[], int _r[]) {
n = _n;
l = _l, r = _r;
iota(p, p + n, 0);
sort(p, p + n, [&] (int x, int y) {
return r[x] < r[y];
});
for(int i = 0; i < n; i++)
rp[i] = r[p[i]];
for(int i = 0; i < n; i++)
aux.EB(l[p[i]], i);
sort(ALL(aux));
for(int i = 0; i < n; i++)
wt::a[i] = aux[i].S;
wt::build();
for(int i = 1; i <= n; i++) {
as_lb[i] = as_lb[i - 1];
while(as_lb[i] < n && aux[as_lb[i]].F < i) as_lb[i]++;
as_rb[i] = as_rb[i - 1];
while(as_rb[i] + 1 < n && aux[as_rb[i] + 1].F <= i) as_rb[i]++;
}
}
struct seg {
int r, hi;
};
V<seg> stk;
int can(int m, int a[]) {
if(accumulate(a, a + m, 0LL) > n) return 0;
sort(a, a + m);
stk.clear();
stk.PB({0, n});
for(int i = 0; i < m; i++) {
int need = 0, ii = i;
while(i < m && a[i] == a[ii]) need += a[i++];
i--;
int j = lower_bound(rp, rp + n, a[i]) - rp
;
while(stk.back().hi < j) {
assert(SZ(stk));
stk.pop_back();
}
int at_least = j;
while(need && SZ(stk)) {
int lb = stk.back().r, rb = a[i]; // (lb, rb]
int cnt = qry(lb + 1, rb, stk.back().hi) - qry(lb + 1, rb, at_least);
if(cnt < need) {
at_least = stk.back().hi;
stk.pop_back();
need -= cnt;
} else if(cnt == need) {
at_least = stk.back().hi;
stk.pop_back();
stk.PB({a[i], at_least});
need -= cnt;
break;
} else {
int dead = qry(lb + 1, rb, at_least);
int who = find_who(lb + 1, rb, at_least, need + dead);
stk.PB({a[i], who});
break;
}
}
if(stk.empty()) return 0;
}
return 1;
}
Compilation message (stderr)
teams.cpp: In function 'int can(int, int*)':
teams.cpp:123:41: warning: conversion from 'long int' to 'int' may change value [-Wconversion]
123 | int j = lower_bound(rp, rp + n, a[i]) - rp
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
# | 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... |