File size: 4,294 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 |
#include <algorithm>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
const int T_INF = 0x3f3f3f3f;
const int INF = T_INF - 1;
const int LIM = 1000000;
const int LIM2 = 2100000;
const int MOD = 1000000007;
struct Event {
int v, t, r1, c1, r2, c2;
Event(int v, int r, int c) : v(v), t(0), r1(r), c1(c) {}
Event(int v, int r1, int c1, int r2, int c2)
: v(v), t(1), r1(r1), c1(c1), r2(r2), c2(c2) {}
bool operator<(const Event& o) const {
return make_pair(v, t) < make_pair(o.v, o.t);
}
};
int N, M;
vector<Event> E;
bool G[LIM][3];
set<int> G0[3], G1[3];
// tree[r1, r2][a, b] = distance from (r1, a) to (r2, b), going only right.
int sz, tree[LIM2][3][3];
int mk, memK[LIM2][3][3];
long long mem[LIM2][3][3];
long long query(int i, int ir1, int ir2, int r1, int r2, int a, int b) {
if (r1 == ir1 && ir2 == r2) { // Full node?
return tree[i][a][b];
}
long long& mm = mem[i][a][b];
if (memK[i][a][b] == mk) { // Already memoized?
return mm;
}
memK[i][a][b] = mk;
i <<= 1;
int m = (ir1 + ir2) >> 1;
if (r2 <= m) { // All on left?
return mm = query(i, ir1, m, r1, r2, a, b);
}
if (r1 > m) { // All on right?
return mm = query(i + 1, m + 1, ir2, r1, r2, a, b);
}
// Consider all possible intermediate columns.
long long ans = INF;
for (int c = 0; c < 3; c++) {
ans = min(
ans,
query(i, ir1, m, r1, m, a, c) + 1 + query(i + 1, m + 1, ir2, m + 1, r2, c, b)
);
}
return mm = ans;
}
long long query_path(int r1, int c1, int r2, int c2) {
long long ans = INF;
// Consider all possible intermediate column pairs (a, b).
mk++;
for (int a = 0; a < 3; a++) {
for (int b = 0; b < 3; b++) {
int d1 = tree[sz + r1][c1][a], d2 = tree[sz + r2][c2][b];
if (d1 > INF && abs(a - c1) == 2 && G[r1][c1] && G[r1][a]) {
int i = *prev(G1[1].lower_bound(r1));
if (
i >= 0 &&
i > *prev(G0[0].lower_bound(r1)) &&
i > *prev(G0[2].lower_bound(r1))
) {
d1 = (r1 - i + 1) * 2;
}
}
if (d2 > INF && abs(b - c2) == 2 && G[r2][c2] && G[r2][b]) {
int i = *G1[1].lower_bound(r2);
if (
i < N &&
i < *G0[0].lower_bound(r2) &&
i < *G0[2].lower_bound(r2)
) {
d2 = (i - r2 + 1) * 2;
}
}
ans = min(ans, d1 + query(1, 0, sz - 1, r1, r2, a, b) + d2);
}
}
return ans == INF ? 1 : ans;
}
void add_cell(int r, int c) {
G[r][c] = 1;
G0[c].erase(r);
G1[c].insert(r);
// Update affected leaf node in segment tree.
int i = sz + r;
for (int a = 0; a < 3; a++) {
for (int b = a; b < 3; b++) {
if (!G[r][b]) {
break;
}
tree[i][a][b] = tree[i][b][a] = b - a;
}
}
// Update ancestors in segment tree.
while (i > 1) {
i >>= 1;
for (int a = 0; a < 3; a++) {
for (int b = 0; b < 3; b++) {
for (int c = 0; c < 3; c++) {
tree[i][a][b] = min(
tree[i][a][b],
tree[i << 1][a][c] + 1 + tree[(i << 1) + 1][c][b]
);
}
}
}
}
}
int solve() {
E.clear();
for (int c = 0; c < 3; c++) {
G0[c].clear();
G1[c].clear();
}
// Input.
cin >> N >> M;
for (int r = 0; r < N; r++) {
for (int c = 0; c < 3; c++) {
int a;
cin >> a;
E.push_back(Event(a, r, c));
G0[c].insert(r);
}
}
for (int i = 0; i < M; i++) {
int r1, c1, r2, c2, l;
cin >> r1 >> c1 >> r2 >> c2 >> l;
if (r1 > r2) {
swap(r1, r2);
swap(c1, c2);
}
E.push_back(Event(l, r1 - 1, c1 - 1, r2 - 1, c2 - 1));
}
// Init grid / segment tree.
memset(G, 0, sizeof(G[0]) * N);
for (int c = 0; c < 3; c++) {
G0[c].insert(-1);
G1[c].insert(-1);
G0[c].insert(N);
G1[c].insert(N);
}
for (sz = 1; sz < N; sz <<= 1);
memset(tree, T_INF, sizeof(tree[0]) * sz * 2);
// Process events.
int ans = 1;
sort(E.begin(), E.end());
for (auto e : E) {
if (!e.t) {
add_cell(e.r1, e.c1);
} else {
ans = ans * query_path(e.r1, e.c1, e.r2, e.c2) % MOD;
}
}
return ans;
}
int main() {
int T;
cin >> T;
for (int t = 1; t <= T; t++) {
cout << "Case #" << t << ": " << solve() << endl;
}
return 0;
}
|