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[N], r[N];
vi p;
// int find_who(node* x, node* y, int cnt, int tl = 0, int tr = n) {
// assert(ct(y) - ct(x) > cnt);
// if(tr - tl == 1) return tl;
// int tm = (tl + tr) / 2;
// if(ct(lc(y)) - ct(lc(x)) > cnt)
// return find_who(lc(x), lc(y), cnt, tl, tm);
// else
// return find_who(rc(x), rc(y), cnt - (ct(lc(y)) - ct(lc(x))), tm, tr);
// }
namespace wt { // wavelet tree
int a[N];
void build() {
}
int qry(int l, int r, int x) { // [l, r]
int ans = 0;
for(int i = l; i <= r; i++)
ans += a[i] < x;
return ans;
}
int find_who(int l, int r, int need) {
vi b;
for(int i = l; i <= r; i++)
b.PB(a[i]);
sort(ALL(b));
return b[need];
}
}
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 need) {
lb = as_lb[lb], rb = as_rb[rb];
assert(lb <= rb);
return wt::find_who(lb, rb, need); // [lb, rb]
}
void init(int _n, int _l[], int _r[]) {
n = _n;
for(int i = 0; i < n; i++)
l[i] = _l[i], r[i] = _r[i];
p = vi(n); iota(ALL(p), 0);
sort(ALL(p), [&] (int x, int y) {
return r[x] < r[y];
});
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] = lower_bound(ALL(aux), pi(i, -INF)) - aux.begin();
as_rb[i] = upper_bound(ALL(aux), pi(i, INF)) - aux.begin() - 1;
}
}
struct seg {
int r, hi;
};
int can(int m, int a[]) {
if(accumulate(a, a + m, 0LL) > n) return 0;
sort(a, a + m);
V<seg> stk;
stk.PB({0, n});
for(int i = 0, j = 0; i < m; i++) {
int need = 0, ii = i;
while(i < m && a[i] == a[ii]) need += a[i++];
i--;
while(j < n && r[p[j]] < a[i]) j++;
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(h[rb], at_least, stk.back().hi) - qry(h[lb], at_least, stk.back().hi);
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, need + dead);
stk.PB({a[i], who});
break;
}
}
if(stk.empty()) return 0;
}
return 1;
}
Compilation message (stderr)
teams.cpp: In function 'void init(int, int*, int*)':
teams.cpp:85:49: warning: conversion from '__gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int> > >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
85 | as_lb[i] = lower_bound(ALL(aux), pi(i, -INF)) - aux.begin();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
teams.cpp:86:62: warning: conversion from '__gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int> > >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
86 | as_rb[i] = upper_bound(ALL(aux), pi(i, INF)) - aux.begin() - 1;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
# | 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... |