Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
File size: 3,693 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
#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;
}