File size: 3,555 Bytes
f7ba5f2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
#include <iostream>
#include <vector>
using namespace std;
const int INF = (int)1e9;
struct fenwick { // For 1-based indices.
vector<int> a, t;
fenwick(int N) : a(N + 1), t(N + 1) {}
int sum(int hi) const {
int res = 0;
for (; hi > 0; hi -= hi & -hi) {
res += t[hi];
}
return res;
}
void inc(int i, int x) {
a[i] += x;
for (; i < (int)t.size(); i += i & -i) {
t[i] += x;
}
}
int at(int i) const { return a[i]; }
int sum(int lo, int hi) const { return sum(hi) - sum(lo - 1); }
void set(int i, int x) { inc(i, x - a[i]); }
};
// Let L be the subarray with lower sum, and H be the one with higher sum.
// Try to reduce d = sum(H) - sum(L) down to 0 with the follow operations:
// 1. Decrease d by 4 by swapping a 1 in L with a 3 in H.
// 2. Decrease d by 2 by swapping a 1 in L with a 2 in H,
// or a 2 in L with a 3 in H.
// 3. Increase d by 2 by swapping a 2 in L with a 1 in H,
// or a 3 in L with a 2 in H.
// Update L/H after swapping v1 in L with v2 in H a total of swaps times.
void do_swaps(vector<int> &L, vector<int> &H, int v1, int v2, int swaps) {
L[v1] -= swaps;
H[v1] += swaps;
H[v2] -= swaps;
L[v2] += swaps;
}
// Perform op 1 until no longer possible or d = 0 or 1. Then op 2 for the rest.
int strategy_a(vector<int> L, vector<int> H, int d) {
// We can perform op 1 at most min(num 1s in L, num 3s in H) times.
// Each swap reduces d by 4, so we should also swap no more than floor(d/4).
int swap13 = min(min(L[0], H[2]), d / 4);
do_swaps(L, H, 0, 2, swap13);
d -= 4*swap13;
// Do the rest as needed with L[0], H[1] and L[1], H[2].
int swap12 = min(min(L[0], H[1]), d / 2);
do_swaps(L, H, 0, 1, swap12);
d -= 2*swap12;
int swap23 = min(min(L[1], H[2]), d / 2);
do_swaps(L, H, 1, 2, swap23);
d -= 2*swap23;
return d != 0 ? INF : swap13 + swap12 + swap23;
}
// Perform op 1 until d becomes equal to -1, then perform op 3 once.
int strategy_b(vector<int> L, vector<int> H, int d) {
// This time we can let d go below 0 by swapping up to ceil(d/4).
int swap13 = min(min(L[0], H[2]), d / 4 + (int)(d % 4 != 0));
do_swaps(L, H, 0, 2, swap13);
d -= 4*swap13;
// Only if d < 0, try to bring d back up to 0 with more swaps.
int swap21 = min(min(L[1], H[0]), d < 0 ? (-d)/2 : 0);
do_swaps(L, H, 1, 0, swap21);
d += 2*swap21;
int swap32 = min(min(L[2], H[1]), d < 0 ? (-d)/2 : 0);
do_swaps(L, H, 2, 1, swap32);
d += 2*swap32;
return d != 0 ? INF : swap13 + swap21 + swap32;
}
long long solve() {
int N, M;
cin >> N >> M;
fenwick T[]{fenwick(N), fenwick(N), fenwick(N)};
for (int i = 1, a; i <= N; i++) {
cin >> a;
T[--a].set(i, 1);
}
long long ans = 0;
for (int i = 0, x, y, z; i < M; i++) {
cin >> x >> y >> z;
--y;
for (int v : {0, 1, 2}) {
T[v].set(x, v == y);
}
vector<int> L(3), H(3);
int sumL = 0, sumH = 0;
for (int v : {0, 1, 2}) {
L[v] = T[v].sum(1, z);
H[v] = T[v].sum(z + 1, N);
sumL += (v + 1) * L[v];
sumH += (v + 1) * H[v];
}
if (sumL == sumH) {
continue;
}
if (sumL > sumH) {
swap(sumL, sumH);
L.swap(H);
}
int d = sumH - sumL;
int swaps_a = strategy_a(L, H, d), swaps_b = strategy_b(L, H, d);
int Q = min(swaps_a, swaps_b);
ans += (Q == INF) ? -1 : Q;
}
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;
}
|