이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
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 r_r = r - tl + 1 - r_l;
int l_l = (l ? p[lv][l - 1] : 0) - (tl ? p[lv][tl - 1] : 0);
int l_r = l - 1 - tl + 1 - l_l;
return qry(tl + l_l, tl + r_l - 1, x, tl, tm, lv + 1)
+ qry(tm + l_r, tm + r_r - 1, x, tm, tr, lv + 1);
}
int find_who(int l, int r, int need) { // [l, r]
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;
}
컴파일 시 표준 에러 (stderr) 메시지
teams.cpp: In function 'int wt::find_who(int, int, int)':
teams.cpp:61:6: warning: declaration of 'b' shadows a global declaration [-Wshadow]
61 | vi b;
| ^
teams.cpp:27:6: note: shadowed declaration is here
27 | int b[N]; // sorted a
| ^
teams.cpp: In function 'void init(int, int*, int*)':
teams.cpp:100: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]
100 | as_lb[i] = lower_bound(ALL(aux), pi(i, -INF)) - aux.begin();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
teams.cpp:101: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]
101 | 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... |