Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
File size: 3,819 Bytes
d3f4f72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <algorithm>
#include <iostream>
#include <set>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

const int INF = 1000000000;
const int LIM = 1000002;

struct UnionFind {
  int N;
  vector<int> root, rank, sidx;
  vector<set<pair<int, int>>> S;

  UnionFind(int _N) : N(_N), root(_N), rank(_N, 0), sidx(_N), S(_N) {
    for (int i = 0; i < N; i++) {
      root[i] = i;
      sidx[i] = i;
      S[i].emplace(-INF, -INF);
      S[i].emplace(INF, INF);
    }
  }

  int find(int i) {
    if (root[i] != i) {
      root[i] = find(root[i]);
    }
    return root[i];
  }

  void merge(int i, int j) {
    i = find(i);
    j = find(j);
    if (i == j) {
      return;
    }
    if (rank[i] > rank[j]) {
      swap(i, j);
    }
    root[i] = j;
    if (rank[i] == rank[j]) {
      rank[j]++;
    }
    // Merge robot interval sets (ensuring j gets the larger one, and merging the
    // other into it).
    if (S[sidx[i]].size() > S[sidx[j]].size()) {
      swap(sidx[i], sidx[j]);
    }
    auto &SA = S[sidx[i]], &SB = S[sidx[j]];
    for (auto p : SA) {
      if (p.first == -INF || p.first == INF) {
        continue;
      }
      auto it = prev(SB.lower_bound(make_pair(p.first + 1, -1)));
      if (it->second < p.first - 1) {
        it++;
      }
      while (it->first <= p.second + 1) {
        p.first = min(p.first, it->first);
        p.second = max(p.second, it->second);
        it = SB.erase(it);
      }
      SB.insert(p);
    }
    SA.clear();
  }
};

int R, C, N, K;
int H[LIM];
vector<pair<int, int>> children[LIM];
vector<tuple<int, int, int, int>> A;

pair<long long, long long> solve() {
  A.clear();
  for (int i = 0; i < LIM; i++) {
    children[i].clear();
  }
  // Input.
  cin >> R >> C;
  N = R * C;
  for (int i = 0, j; i < N; i++) {
    cin >> H[i];
    if ((j = i - C) >= 0) {  // Up.
      A.emplace_back(-min(H[i], H[j]), INF, i, j);
    }
    if (i % C > 0 && (j = i - 1) >= 0) {  // Left.
      A.emplace_back(-min(H[i], H[j]), INF, i, j);
    }
  }
  for (int i = 0; i < N; i++) {
    int s;
    cin >> s;
    children[i].emplace_back(0, s);
  }
  cin >> K;
  for (int i = 0; i < K; i++) {
    int a, b, u;
    cin >> a >> b >> u;
    a--;
    b--;
    int j = a * C + b;
    if (i == 0) {
      children[j].clear();
    }
    children[j].emplace_back(i, u);
  }
  // Determine interval of situations per original/updated S value.
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < children[i].size(); j++) {
      int a = children[i][j].first;
      int b = j < int(children[i].size()) - 1
        ? children[i][j + 1].first - 1
        : K - 1;
      A.emplace_back(-children[i][j].second, i, a, b);
    }
  }
  // Process edges/samples in non-increasing order of elevation.
  UnionFind U(N);
  sort(A.begin(), A.end());
  pair<long long, long long> ans{0, 0};
  for (const auto &t : A) {
    int h = -get<0>(t), i = get<1>(t), a = get<2>(t), b = get<3>(t);
    if (i < INF) {  // Sample.
      if (H[i] <= h) {  // Impossible to collect.
        continue;
      }
      ans.first += b - a + 1;  // Collect it in each relevant situation.
      auto &S = U.S[U.sidx[U.find(i)]];
      // Add new robots to relevant situations as necessary.
      auto it = prev(S.lower_bound(make_pair(a + 1, -1)));
      if (it->second < a - 1) {
        it++;
      }
      while (it->first <= b + 1) {
        a = min(a, it->first);
        b = max(b, it->second);
        ans.second -= it->second - it->first + 1;
        it = S.erase(it);
      }
      S.emplace(a, b);
      ans.second += b - a + 1;
      continue;
    }
    U.merge(a, b);  // Edge.
  }
  return ans;
}

int main() {
  int T;
  cin >> T;
  for (int t = 1; t <= T; t++) {
    auto ans = solve();
    cout << "Case #" << t << ": " << ans.first << " " << ans.second << endl;
  }
  return 0;
}