結果
| 問題 | No.919 You Are A Project Manager |
| コンテスト | |
| ユーザー |
みかん
|
| 提出日時 | 2026-11-24 00:46:26 |
| 言語 | C++23 (gcc 15.3.0 + boost 1.92.0 + ACL) |
| 結果 |
WA
不安定
|
| 実行時間 | - |
| コード長 | 19,620 bytes |
| 記録 | |
| コンパイル時間 | 4,073 ms |
| コンパイル使用メモリ | 378,288 KB |
| 実行使用メモリ | 9,924 KB |
| 最終ジャッジ日時 | 2026-11-24 00:46:35 |
| 合計ジャッジ時間 | 8,369 ms |
|
ジャッジサーバーID (参考情報) |
judge5_0 / judge6_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 46 WA * 9 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Group {
using S = long long;
static S op(const S& a, const S& b) { return a + b; }
static S inv(const S& x) { return -x; }
static S e() { return 0; }
};
template<typename T, typename Group = Group>
struct WaveletMatrix {
private:
using S = typename Group::S;
struct BitVector {
using u64 = uint64_t;
vector<u64> bits;
vector<int> count;
explicit BitVector(int N) : bits(N / 64 + 1), count(N / 64 + 2) {}
void set(int i) {
bits[i>>6] |= u64(1) << (i & 63);
}
void build() {
for(int i=0; i<static_cast<int>(bits.size()); i++) count[i+1] = count[i] + popcount(bits[i]);
}
bool get(int i) const {
return bits[i>>6] >> (i & 63) & 1;
}
int rank1(int i) const {
return count[i>>6] + popcount(bits[i>>6] & ((u64(1) << (i & 63)) - 1));
}
int rank0(int i) const {
return i - rank1(i);
}
};
int N_, M_, log_;
bool fenwick_;
vector<int> start_;
vector<int> positions_;
vector<T> keys_;
vector<S> weights_;
vector<T> values_;
vector<BitVector> bits_;
vector<int> zeros_;
vector<vector<S>> sums_;
vector<vector<int>> counts_;
int index(T x) const {
return static_cast<int>(lower_bound(values_.begin(), values_.end(), x) - values_.begin());
}
int count_of(int d, int l, int r) const {
if(counts_.empty()) return r - l;
int ret = 0;
for(int p=r; 0<p; p-=p&-p) ret += counts_[d][p];
for(int p=l; 0<p; p-=p&-p) ret -= counts_[d][p];
return ret;
}
static int bisect(int lo, int hi, auto f) {
while(hi - lo > 1) {
int mid = (lo + hi) / 2;
if(f(mid)) lo = mid;
else hi = mid;
}
return lo;
}
int kth_active(int d, int l, int r, int k) const {
if(counts_.empty()) return l + k - 1;
return bisect(l, r, [&](int m) { return count_of(d, l, m) < k; });
}
template<typename F>
void follow(int& l, int& r, int c, F update) const {
for(int d=0; d<log_; d++) {
const BitVector& b = bits_[d];
if(c >> (log_ - d - 1) & 1) {
update(d);
l = zeros_[d] + b.rank1(l);
r = zeros_[d] + b.rank1(r);
} else {
l = b.rank0(l);
r = b.rank0(r);
}
}
}
template<typename F>
void follow_kth(int& l, int& r, int& k, F update) const {
for(int d=0; d<log_; d++) {
const BitVector& b = bits_[d];
int zero = count_of(d + 1, b.rank0(l), b.rank0(r));
if(k <= zero) {
l = b.rank0(l);
r = b.rank0(r);
} else {
update(d);
k -= zero;
l = zeros_[d] + b.rank1(l);
r = zeros_[d] + b.rank1(r);
}
}
}
int kth_index(int l, int r, int k) const {
int ret = 0;
follow_kth(l, r, k, [&](int d) {
ret |= 1 << (log_ - d - 1);
});
return ret;
}
int slot_of(int c, int i) const {
for(int d=log_-1; d>=0; d--) {
const BitVector& b = bits_[d];
bool one = c >> (log_ - d - 1) & 1;
int j = one ? i - zeros_[d] : i;
i = bisect(0, M_, [&](int m) { return (one ? b.rank1(m) : b.rank0(m)) <= j; });
}
return i;
}
S prefix(int d, int p) const {
S ret = Group::e();
while(0 < p) {
ret = Group::op(ret, sums_[d][p]);
p -= p & -p;
}
return ret;
}
S diff(int d, int l, int r) const {
if(fenwick_) return Group::op(prefix(d, r), Group::inv(prefix(d, l)));
return Group::op(sums_[d][r], Group::inv(sums_[d][l]));
}
int find(int i, const T& x, const char* name) const {
int j = static_cast<int>(lower_bound(keys_.begin() + start_[i], keys_.begin() + start_[i+1], x) - keys_.begin());
bool found = j < start_[i+1] && keys_[j] == x;
if(!found) cerr << "WaveletMatrix::" << name << ": i = " << i << " is not registered with this key\n";
assert(found && "WaveletMatrix::find: found");
return j;
}
void update(int i, const S& w, int c) {
if(!fenwick_) {
for(int d=0; d<=log_; d++) for(int p=M_; 1<=p; p--) sums_[d][p] = Group::op(sums_[d][p], Group::inv(sums_[d][p - (p & -p)]));
fenwick_ = true;
}
if(c != 0 && counts_.empty()) {
counts_.assign(log_ + 1, vector<int>(M_ + 1));
for(int d=0; d<=log_; d++) for(int p=1; p<=M_; p++) counts_[d][p] = p & -p;
}
weights_[i] = Group::op(weights_[i], w);
for(int d=0; d<=log_; d++) {
for(int p=i+1; p<=M_; p+=p&-p) {
sums_[d][p] = Group::op(sums_[d][p], w);
if(c != 0) counts_[d][p] += c;
}
if(d == log_) break;
const BitVector& b = bits_[d];
i = b.get(i) ? zeros_[d] + b.rank1(i) : b.rank0(i);
}
}
tuple<optional<T>, S, int> search(int l, int r, auto ok, bool largest) const {
S all = diff(0, l, r);
if(ok(all)) return {nullopt, all, count_of(0, l, r)};
S acc = Group::e();
int cnt = 0, ret = 0;
for(int d=0; d<log_; d++) {
const BitVector& b = bits_[d];
int l0 = b.rank0(l), r0 = b.rank0(r);
int l1 = zeros_[d] + b.rank1(l), r1 = zeros_[d] + b.rank1(r);
int fl = largest ? l1 : l0, fr = largest ? r1 : r0;
S next = Group::op(acc, diff(d + 1, fl, fr));
bool taken = ok(next);
if(taken) {
acc = next;
cnt += count_of(d + 1, fl, fr);
}
if(taken != largest) {
ret |= 1 << (log_ - d - 1);
l = l1, r = r1;
} else {
l = l0, r = r0;
}
}
auto part = [&](int m) { return largest ? pair{r - m, r} : pair{l, l + m}; };
int m = bisect(0, r - l, [&](int m) {
auto [pl, pr] = part(m);
return ok(Group::op(acc, diff(log_, pl, pr)));
});
auto [pl, pr] = part(m);
return {values_[ret], Group::op(acc, diff(log_, pl, pr)), cnt + count_of(log_, pl, pr)};
}
static vector<tuple<int, T, S>> zip(const vector<T>& a, const vector<S>& w) {
assert(a.size() == w.size() && "WaveletMatrix::WaveletMatrix: a.size() == w.size()");
vector<tuple<int, T, S>> ret;
for(int i=0; i<static_cast<int>(a.size()); i++) ret.emplace_back(i, a[i], w[i]);
return ret;
}
void check_index(int i, const char* name) const {
if(0 <= i && i < N_) return;
cerr << "WaveletMatrix::" << name << ": i = " << i << ", N_ = " << N_ << '\n';
assert(0 <= i && "WaveletMatrix::check_index: 0 <= i");
assert(i < N_ && "WaveletMatrix::check_index: i < N_");
}
void check_range(int l, int r, const char* name) const {
if(0 <= l && l <= r && r <= N_) return;
cerr << "WaveletMatrix::" << name << ": l = " << l << ", r = " << r << ", N_ = " << N_ << '\n';
assert(0 <= l && "WaveletMatrix::check_range: 0 <= l");
assert(l <= r && "WaveletMatrix::check_range: l <= r");
assert(r <= N_ && "WaveletMatrix::check_range: r <= N_");
}
void check_k(int l, int r, int k, int lower, const char* name) const {
int c = count_of(0, l, r);
if(lower <= k && k <= c) return;
cerr << "WaveletMatrix::" << name << ": lower = " << lower << ", k = " << k << ", count = " << c << '\n';
assert(lower <= k && "WaveletMatrix::check_k: lower <= k");
assert(k <= c && "WaveletMatrix::check_k: k <= count");
}
public:
explicit WaveletMatrix(const vector<T>& a) : WaveletMatrix(a, vector<S>(a.begin(), a.end())) {}
WaveletMatrix(const vector<T>& a, const vector<S>& w) : WaveletMatrix(zip(a, w)) {}
WaveletMatrix(const vector<T>& a, const vector<pair<int, T>>& cand) : WaveletMatrix(a, vector<S>(a.begin(), a.end()), cand) {}
WaveletMatrix(const vector<T>& a, const vector<S>& w, const vector<pair<int, T>>& cand) : WaveletMatrix(zip(a, w), cand) {}
explicit WaveletMatrix(const vector<tuple<int, T, S>>& a) : WaveletMatrix(a, {}) {}
WaveletMatrix(const vector<tuple<int, T, S>>& a, const vector<pair<int, T>>& cand) : N_(0), M_(0), log_(0), fenwick_(!cand.empty()) {
vector<tuple<int, T, S, int>> e;
for(const auto& [p, x, w] : a) {
e.emplace_back(p, x, w, 1);
N_ = max(N_, p + 1);
}
for(const auto& [p, x] : cand) {
e.emplace_back(p, x, Group::e(), 0);
N_ = max(N_, p + 1);
}
auto less = [](const tuple<int, T, S, int>& a, const tuple<int, T, S, int>& b) {
if(std::get<0>(a) != std::get<0>(b)) return std::get<0>(a) < std::get<0>(b);
return std::get<1>(a) < std::get<1>(b);
};
if(!is_sorted(e.begin(), e.end(), less)) sort(e.begin(), e.end(), less);
start_.assign(N_ + 1, 0);
vector<int> counts;
for(const auto& [p, x, w, c] : e) {
assert(0 <= p && "WaveletMatrix::WaveletMatrix: 0 <= p");
if(!keys_.empty() && positions_.back() == p && keys_.back() == x) {
weights_.back() = Group::op(weights_.back(), w);
counts.back() |= c;
continue;
}
positions_.push_back(p);
keys_.push_back(x);
weights_.push_back(w);
counts.push_back(c);
start_[p+1]++;
}
for(int p=0; p<N_; p++) start_[p+1] += start_[p];
M_ = static_cast<int>(keys_.size());
values_ = keys_;
sort(values_.begin(), values_.end());
values_.erase(unique(values_.begin(), values_.end()), values_.end());
while((1 << log_) < static_cast<int>(values_.size())) log_++;
vector<tuple<int, S, int>> cur(M_);
for(int i=0; i<M_; i++) cur[i] = {index(keys_[i]), weights_[i], counts[i]};
bits_.assign(log_, BitVector(M_));
zeros_.assign(log_, 0);
sums_.assign(log_ + 1, vector<S>(M_ + 1, Group::e()));
if(!cand.empty()) counts_.assign(log_ + 1, vector<int>(M_ + 1, 0));
for(int d=0; d<=log_; d++) {
for(int i=1; i<=M_; i++) {
sums_[d][i] = Group::op(sums_[d][i], std::get<1>(cur[i-1]));
if(!cand.empty()) counts_[d][i] += std::get<2>(cur[i-1]);
if(fenwick_) {
const int p = i + (i & -i);
if(p <= M_) {
sums_[d][p] = Group::op(sums_[d][p], sums_[d][i]);
if(!cand.empty()) counts_[d][p] += counts_[d][i];
}
} else {
sums_[d][i] = Group::op(sums_[d][i-1], sums_[d][i]);
}
}
if(d == log_) break;
int bit = log_ - d - 1;
for(int i=0; i<M_; i++) if(std::get<0>(cur[i]) >> bit & 1) bits_[d].set(i);
bits_[d].build();
zeros_[d] = bits_[d].rank0(M_);
stable_partition(cur.begin(), cur.end(), [&](const tuple<int, S, int>& x) {
return !(std::get<0>(x) >> bit & 1);
});
}
}
int size() const {
return N_;
}
S get(int i, const T& x) const {
check_index(i, __func__);
int j = static_cast<int>(lower_bound(keys_.begin() + start_[i], keys_.begin() + start_[i+1], x) - keys_.begin());
if(j == start_[i+1] || keys_[j] != x) return Group::e();
return weights_[j];
}
void set(int i, const T& x) { set(i, x, static_cast<S>(x)); }
void set(int i, const T& x, const S& w) {
check_index(i, __func__);
int j = find(i, x, __func__);
update(j, Group::op(w, Group::inv(weights_[j])), 1 - count_of(0, j, j + 1));
}
void apply(int i, const T& x, const S& w) {
check_index(i, __func__);
int j = find(i, x, __func__);
update(j, w, 1 - count_of(0, j, j + 1));
}
void erase(int i, const T& x) {
check_index(i, __func__);
int j = find(i, x, __func__);
assert(count_of(0, j, j + 1) == 1 && "WaveletMatrix::erase: used");
update(j, Group::inv(weights_[j]), -1);
}
T kth_smallest(int l, int r, int k) const {
check_range(l, r, __func__);
l = start_[l], r = start_[r];
check_k(l, r, k, 1, __func__);
return values_[kth_index(l, r, k)];
}
T kth_largest(int l, int r, int k) const {
check_range(l, r, __func__);
l = start_[l], r = start_[r];
check_k(l, r, k, 1, __func__);
return values_[kth_index(l, r, count_of(0, l, r) - k + 1)];
}
int count(int l, int r) const {
check_range(l, r, __func__);
return count_of(0, start_[l], start_[r]);
}
int count(int l, int r, T x) const {
check_range(l, r, __func__);
l = start_[l], r = start_[r];
int c = index(x);
if(c == static_cast<int>(values_.size()) || x != values_[c]) return 0;
follow(l, r, c, [&](int) {});
return count_of(log_, l, r);
}
int count_lt(int l, int r, T upper) const {
check_range(l, r, __func__);
l = start_[l], r = start_[r];
int c = index(upper);
if(c == static_cast<int>(values_.size())) return count_of(0, l, r);
int ret = 0;
follow(l, r, c, [&](int d) {
ret += count_of(d + 1, bits_[d].rank0(l), bits_[d].rank0(r));
});
return ret;
}
int count_ge(int l, int r, T lower) const {
check_range(l, r, __func__);
return count(l, r) - count_lt(l, r, lower);
}
int count_ge_lt(int l, int r, T lower, T upper) const {
check_range(l, r, __func__);
assert(lower <= upper && "WaveletMatrix::count_ge_lt: lower <= upper");
return count_lt(l, r, upper) - count_lt(l, r, lower);
}
S sum(int l, int r) const {
check_range(l, r, __func__);
return diff(0, start_[l], start_[r]);
}
S sum(int l, int r, T x) const {
check_range(l, r, __func__);
l = start_[l], r = start_[r];
int c = index(x);
if(c == static_cast<int>(values_.size()) || x != values_[c]) return Group::e();
follow(l, r, c, [&](int) {});
return diff(log_, l, r);
}
S sum_lt(int l, int r, T upper) const {
check_range(l, r, __func__);
l = start_[l], r = start_[r];
int c = index(upper);
if(c == static_cast<int>(values_.size())) return diff(0, l, r);
S ret = Group::e();
follow(l, r, c, [&](int d) {
ret = Group::op(ret, diff(d + 1, bits_[d].rank0(l), bits_[d].rank0(r)));
});
return ret;
}
S sum_ge(int l, int r, T lower) const {
check_range(l, r, __func__);
return Group::op(sum(l, r), Group::inv(sum_lt(l, r, lower)));
}
S sum_ge_lt(int l, int r, T lower, T upper) const {
check_range(l, r, __func__);
assert(lower <= upper && "WaveletMatrix::sum_ge_lt: lower <= upper");
return Group::op(sum_lt(l, r, upper), Group::inv(sum_lt(l, r, lower)));
}
S sum_smallest(int l, int r, int k) const {
check_range(l, r, __func__);
l = start_[l], r = start_[r];
check_k(l, r, k, 0, __func__);
S ret = Group::e();
follow_kth(l, r, k, [&](int d) {
ret = Group::op(ret, diff(d + 1, bits_[d].rank0(l), bits_[d].rank0(r)));
});
if(k == 0) return ret;
return Group::op(ret, diff(log_, l, kth_active(log_, l, r, k) + 1));
}
S sum_largest(int l, int r, int k) const {
check_range(l, r, __func__);
check_k(start_[l], start_[r], k, 0, __func__);
return Group::op(sum(l, r), Group::inv(sum_smallest(l, r, count(l, r) - k)));
}
int kth_smallest_position(int l, int r, int k) const {
check_range(l, r, __func__);
l = start_[l], r = start_[r];
check_k(l, r, k, 1, __func__);
int c = 0;
follow_kth(l, r, k, [&](int d) {
c |= 1 << (log_ - d - 1);
});
return positions_[slot_of(c, kth_active(log_, l, r, k))];
}
int kth_largest_position(int l, int r, int k) const {
check_range(l, r, __func__);
check_k(start_[l], start_[r], k, 1, __func__);
return kth_smallest_position(l, r, count(l, r) - k + 1);
}
int select(int l, int r, T x, int k) const {
check_range(l, r, __func__);
assert(1 <= k && "WaveletMatrix::select: 1 <= k");
l = start_[l], r = start_[r];
int c = index(x);
if(c == static_cast<int>(values_.size()) || x != values_[c]) return -1;
follow(l, r, c, [&](int) {});
if(k > count_of(log_, l, r)) return -1;
return positions_[slot_of(c, kth_active(log_, l, r, k))];
}
int prev_position(int l, int r, T upper) const {
check_range(l, r, __func__);
int cnt = count_lt(l, r, upper);
if(cnt == 0) return -1;
return kth_smallest_position(l, r, cnt);
}
int next_position(int l, int r, T lower) const {
check_range(l, r, __func__);
int cnt = count_lt(l, r, lower);
if(cnt == count(l, r)) return -1;
return kth_smallest_position(l, r, cnt + 1);
}
optional<T> prev_value(int l, int r, T upper) const {
check_range(l, r, __func__);
int cnt = count_lt(l, r, upper);
if(cnt == 0) return nullopt;
return kth_smallest(l, r, cnt);
}
optional<T> next_value(int l, int r, T lower) const {
check_range(l, r, __func__);
int cnt = count_lt(l, r, lower);
if(cnt == count(l, r)) return nullopt;
return kth_smallest(l, r, cnt + 1);
}
tuple<optional<T>, S, int> search_smallest(int l, int r, auto ok) const {
check_range(l, r, __func__);
assert(ok(Group::e()) && "WaveletMatrix::search_smallest: ok(Group::e())");
return search(start_[l], start_[r], ok, false);
}
tuple<optional<T>, S, int> search_largest(int l, int r, auto ok) const {
check_range(l, r, __func__);
assert(ok(Group::e()) && "WaveletMatrix::search_largest: ok(Group::e())");
return search(start_[l], start_[r], ok, true);
}
void debug() const {
cout << " keys = ";
for(int i=0; i<M_; i++) if(0 < count_of(0, i, i + 1)) cout << keys_[i] << ' ';
cout << '\n';
cout << "weights = ";
for(int i=0; i<M_; i++) if(0 < count_of(0, i, i + 1)) cout << weights_[i] << ' ';
cout << '\n';
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll N, ans = 0;
cin >> N;
vector<ll> A(N);
for(int i=0; i<N; i++) cin >> A[i];
WaveletMatrix<ll> WM(A);
for(int K=1; K<=N; K++) {
ll t = 0;
int l = 0, r = N;
while(K <= r - l) {
ll L = WM.kth_smallest(l, l + K, (K + 1) / 2);
ll R = WM.kth_smallest(r - K, r, (K + 1) / 2);
if(L < R) {
t += R * K;
r -= K;
} else {
t += L * K;
l += K;
}
ans = max(ans, t);
}
}
cout << ans << endl;
}
みかん