File size: 5,184 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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
#include <algorithm>
#include <cstring>
#include <iostream>
#include <set>
#include <tuple>
#include <utility>
#include <vector>
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<tuple<int, int, int>> A;
pair<int, int> curr;
int M;
int children[LIM2][2];
int parent[LIM2][LIM3];
int high[LIM2];
struct UnionFind {
int N;
vector<int> 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<int> 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<long long, long long> 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<long long, long long> 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;
}
|