#include #include #include #include #include #include #include using namespace std; const int INF = 1000000000; const int LIM = 1000002; const int LIM2 = 2000002; const int LIM3 = 23; int R, C, N, K; int H[LIM], S[LIM]; vector> A; pair curr; int M; int children[LIM2][2]; int parent[LIM2][LIM3]; int high[LIM2]; struct UnionFind { int N; vector root, rank, node; UnionFind(int _N) : N(_N), root(_N), rank(_N, 0), node(_N) { for (int i = 0; i < N; i++) { root[i] = i; node[i] = M; children[M][0] = children[M][1] = -1; high[M++] = INF + 1; } } int find(int i) { if (root[i] != i) { root[i] = find(root[i]); } return root[i]; } void merge(int i, int j, int e) { 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]++; } // Make both components' tree M children of a new node. children[M][0] = node[i]; children[M][1] = node[j]; high[M] = e; node[j] = M++; } }; struct HeavyLightTree { int heavy[LIM2]; int root[LIM2], treePos[LIM2], inv[LIM2]; int ind, L[LIM2], R[LIM2]; multiset S, S2; int dfs(int v) { L[v] = ind++; for (int i = 0; i < LIM3 - 1; i++) { int p = parent[v][i]; if (p < 0) { break; } parent[v][i + 1] = parent[p][i]; } int size = 1, maxSubtree = 0; for (int u : children[v]) { if (u < 0) { continue; } parent[u][0] = v; int subtree = dfs(u); if (subtree > maxSubtree) { heavy[v] = u; maxSubtree = subtree; } size += subtree; } R[v] = ind - 1; return size; } void init() { fill_n(heavy, M, -1); ind = 0; dfs(M - 1); for (int i = 0, currentPos = 0; i < M; i++) { if (parent[i][0] == -1 || heavy[parent[i][0]] != i) { for (int j = i; j != -1; j = heavy[j]) { root[j] = i; inv[currentPos] = j; treePos[j] = currentPos++; } } } S.clear(); S2.clear(); S.insert(-1); } void update(int i, bool ins) { if (ins) { S.insert(treePos[i]); S2.insert(L[i]); } else { S.erase(S.find(treePos[i])); S2.erase(S2.find(L[i])); } } int find_closest_ancestor_set(int i) { while (i >= 0) { int j = root[i]; int k = *prev(S.lower_bound(treePos[i] + 1)); if (k >= treePos[j]) { return inv[k]; } i = parent[j][0]; } return -1; } bool subtree_has_set_M(int i, bool inc) { auto it = S2.lower_bound(L[i] + (inc ? 0 : 1)); return it != S2.end() && *it <= R[i]; } }; HeavyLightTree HLT; void update_cell(int i, bool ins) { // Sample is irrelevant (impossible to collect)? int s = S[i]; if (s >= H[i]) { return; } // Register collection. int d = ins ? 1 : -1; curr.first += d; // Find corresponding tree node (highest valid ancestor of leaf node). for (int j = LIM3 - 1; j >= 0; j--) { if (parent[i][j] >= 0 && high[parent[i][j]] > s) { i = parent[i][j]; } } // Update HLD (if deleting). if (!ins) { HLT.update(i, ins); } // Update can only be relevant if no samples in i's subtree. if (!HLT.subtree_has_set_M(i, true)) { // Find closest ancestor with sample (if any). int a = HLT.find_closest_ancestor_set(i); // Update is relevant if a doesn't exist, or if there are other samples // amongst a's descendants if (a < 0 || HLT.subtree_has_set_M(a, false)) { curr.second += d; } } // Update HLD (if inserting). if (ins) { HLT.update(i, ins); } } pair solve() { A.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]), i, j); } if (i % C > 0 && (j = i - 1) >= 0) { // Left. A.emplace_back(-min(H[i], H[j]), i, j); } } for (int i = 0; i < N; i++) { cin >> S[i]; } // Process edges in non-increasing order of elevation, and construct tree of // connected regions. M = 0; UnionFind U(N); sort(A.begin(), A.end()); for (auto t : A) { U.merge(get<1>(t), get<2>(t), -get<0>(t)); } memset(parent, -1, sizeof(parent[0]) * M); HLT.init(); // Insert initial samples. curr = make_pair(0, 0); for (int i = 0; i < N; i++) { update_cell(i, 1); } // Process updates. cin >> K; pair ans{0, 0}; int prevY = 0; while (K--) { int i, j; cin >> i >> j; i = (i ^ prevY) - 1; j = (j ^ prevY) - 1; i = i * C + j; update_cell(i, 0); cin >> S[i]; S[i] ^= prevY; update_cell(i, 1); ans.first += curr.first; ans.second += curr.second; prevY = curr.second; } 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; }