Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
hackercup / 2022 /round2 /work_life_balance_ch2.cpp
wjomlex's picture
2022 Problems
f7ba5f2 verified
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
using LL = long long;
struct fenwick { // For 1-based indices.
vector<LL> a, t;
fenwick(int N) : a(N + 1), t(N + 1) {}
LL sum(int hi) const {
LL res = 0;
for (; hi > 0; hi -= hi & -hi) {
res += t[hi];
}
return res;
}
void inc(int i, LL x) {
a[i] += x;
for (; i < (int)t.size(); i += i & -i) {
t[i] += x;
}
}
LL at(int i) const { return a[i]; }
LL sum(int lo, int hi) const { return sum(hi) - sum(lo - 1); }
void set(int i, LL x) { inc(i, x - a[i]); }
};
template <typename Predicate>
int binary_search_last_true(int lo_incl, int hi_excl, Predicate pred) {
int mid, lo = lo_incl, hi = hi_excl - 1;
while (lo < hi) {
mid = lo + (hi - lo + 1) / 2;
if (pred(mid)) {
lo = mid;
} else {
hi = mid - 1;
}
}
return pred(lo) ? lo : hi_excl;
}
LL solve() {
int N, M;
cin >> N >> M;
auto rev = [=](int i) -> int { return N - i + 1; };
fenwick Tv_fwd(N), Ti_fwd(N), Tv_rev(N), Ti_rev(N);
for (int i = 1, a; i <= N; i++) {
cin >> a;
Tv_fwd.set(i, a == 2);
Ti_fwd.set(i, a == 2 ? i : 0);
Tv_rev.set(rev(i), a == 2);
Ti_rev.set(rev(i), a == 2 ? i : 0);
}
LL ans = 0;
for (int i = 0, x, y, z; i < M; i++) {
cin >> x >> y >> z;
Tv_fwd.set(x, y == 2);
Ti_fwd.set(x, y == 2 ? x : 0);
Tv_rev.set(rev(x), y == 2);
Ti_rev.set(rev(x), y == 2 ? x : 0);
int L2cnt = Tv_fwd.sum(1, z), H2cnt = Tv_fwd.sum(z + 1, N);
int sumL = 1 * (z - L2cnt) + 2 * L2cnt;
int sumH = 1 * (N - z - H2cnt) + 2 * H2cnt;
if (sumL == sumH) {
continue;
}
auto getQ = [&](
const fenwick &Tv, const fenwick &Ti, int d, int z, bool is_rev
) -> LL {
int req2s = d / 2, tgt2s = Tv.sum(1, z) + req2s;
if (
d % 2 == 1 || // Can't reduce d by an odd number with swaps.
tgt2s > z || // Not enough room to fit all the 2's to make d = 0.
req2s > Tv.sum(z + 1, N) // Not enough 2's from H to move over to L.
) {
return -1;
}
// Assuming sumL < sumH, check that a choice of s will make sumL >= sumH
// if we fill a A[s..z] by shifting 2's from A[(z+1)..N] leftwards, all
// without touching the 2's in A[1..(s-1)].
int s = binary_search_last_true(1, z + 1, [&](int s) {
// On the domain s=1..z, this predicate yields a pattern 1111000, as
// we'll need force sufficiently many 2's in A[s..z] to hit the target
// number of 2s required to make sumL = sumH.
return Tv.sum(1, s - 1) + (z - s + 1) >= tgt2s;
});
assert(s <= z);
int c = z - s + 1;
// Now compute the total cost of shifting the 2s from the range s..t
// leftwards to fill the indices s..z.
int t = 1 + binary_search_last_true(s, N + 1, [&](int t) {
// On the domain t=s..N, this predicate yields a pattern 1111000, as
// we'll need to choose sufficiently large t for there to be enough 2s.
return Tv.sum(s, t) < c;
});
assert(t <= N);
// Q_i = (sum of final indices of 2s) - (sum of initial indices of 2s).
if (is_rev) {
return (LL)c*(rev(s) + rev(z))/2 - Ti.sum(s, t);
}
return Ti.sum(s, t) - (LL)c*(s + z)/2;
};
if (sumL < sumH) {
ans += getQ(Tv_fwd, Ti_fwd, sumH - sumL, z, false);
} else {
ans += getQ(Tv_rev, Ti_rev, sumL - sumH, rev(z) - 1, true);
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
for (int t = 1; t <= T; t++) {
cout << "Case #" << t << ": " << solve() << endl;
}
return 0;
}