#include #include #include #include #include #include using namespace std; const int INF = 1000000000; const int LIM = 1000002; struct UnionFind { int N; vector root, rank, sidx; vector>> 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> children[LIM]; vector> A; pair 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 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; }