题目链接: Lawn of the Dead
大致题意
给定一个 n × m n \times m n×m的网格, 你初始位于 ( 1 , 1 ) (1, 1) (1,1), 你只能向下或向右走.
网格中有 k k k个地雷, 地雷所在的格子是不能去的.
问: 你最终可以到达多少个不同的方格?
解题思路
思维
我们考虑到 n , m n, m n,m的值域太大了, 我们是无法模拟出所有过程的.
考虑到只能向右或向下走, 因此我们可以这样思考:
设有一个长度为
m
m
m的数组
v
i
s
[
]
vis[]
vis[], 表示第
i
−
1
i - 1
i−1行的所有位置可达情况.(不妨设可达为1, 不可达为0).
那么对于第
i
i
i行的所有点, 已知有地雷的位置均不可达, 那么考虑两个地雷之间的区域
[
l
,
r
]
[l, r]
[l,r].
我们需要找
v
i
s
[
]
vis[]
vis[]在
[
l
,
r
]
[l, r]
[l,r]区间中最左侧
1
1
1所在的位置
p
o
s
pos
pos, 我们发现
[
p
o
s
,
r
]
[pos, r]
[pos,r]区域是可达的,
[
l
,
p
o
s
−
1
]
[l, pos - 1]
[l,pos−1]不可达.
因此对于第
i
i
i行的
v
i
s
′
[
]
vis'[]
vis′[]而言, 我们需要设
[
l
,
p
o
s
−
1
]
[l, pos - 1]
[l,pos−1]为
0
0
0,
[
p
o
s
,
r
]
[pos, r]
[pos,r]为
1
1
1. 其余位置情况同理.
对于第
i
+
1
i+1
i+1行也同理.
线段树
综上所述, 我们把二维问题转化成了一维问题. 考虑到找最左侧 1 1 1以及区间修改两种操作, 我们不妨用线段树维护.
特殊情况: 对于第一行, 我们只需要找到其最左侧地雷所在位置 p o s pos pos, 那么对于 [ 1 , p o s − 1 ] [1, pos - 1] [1,pos−1]可达, [ p o s , m ] [pos, m] [pos,m]不可达.
考虑到空间复杂度, 我们其实可以借助滚动数组的思想, 建立两棵线段树分别一棵维护当前行信息, 另一棵维护上一行信息即可.
AC代码
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 1; i <= (n); ++i)
using namespace std;
typedef long long ll;
const int N = 1E5 + 10;
struct SEG {
struct node {
int l, r;
int sum;
int lazy;
}t[N << 2];
static void pushdown(node& op, int lazy) {
op.sum = (op.r - op.l + 1) * lazy;
op.lazy = lazy;
}
void pushdown(int x) {
if (t[x].lazy == -1) return;
pushdown(t[x << 1], t[x].lazy), pushdown(t[x << 1 | 1], t[x].lazy);
t[x].lazy = -1;
}
void pushup(int x) { t[x].sum = t[x << 1].sum + t[x << 1 | 1].sum; }
void build(int l, int r, int x = 1) {
t[x] = { l, r, 1, -1 };
if (l == r) return;
int mid = l + r >> 1;
build(l, mid, x << 1), build(mid + 1, r, x << 1 | 1);
pushup(x);
}
void modify(int l, int r, int c, int x = 1) {
if (l <= t[x].l and r >= t[x].r) {
pushdown(t[x], c);
return;
}
pushdown(x);
int mid = t[x].l + t[x].r >> 1;
if (l <= mid) modify(l, r, c, x << 1);
if (r > mid) modify(l, r, c, x << 1 | 1);
pushup(x);
}
int ask(int l, int r, int x = 1) {
if (!t[x].sum) return 0;
if (t[x].l == t[x].r) return t[x].l;
pushdown(x);
if (l <= t[x].l and r >= t[x].r) {
if (t[x << 1].sum) return ask(l, r, x << 1);
return ask(l, r, x << 1 | 1);
}
int mid = t[x].l + t[x].r >> 1;
int res = 0;
if (l <= mid) res = ask(l, r, x << 1);
if (!res and r > mid) res = ask(l, r, x << 1 | 1);
return res;
}
void clear() { pushdown(t[1], 1); }
}t[2];
vector<int> v[N]; // 每行的雷
int main()
{
int T; cin >> T;
while (T--) {
int n, m, k; scanf("%d %d %d", &n, &m, &k);
rep(i, n) v[i].clear();
rep(i, k) {
int a, b; scanf("%d %d", &a, &b);
v[a].push_back(b);
}
rep(i, n) sort(v[i].begin(), v[i].end());
t[0].build(1, m), t[1].build(1, m);
ll res = 1ll * n * m;
if (!v[1].empty()) {
int l = v[1].front();
t[1].modify(l, m, 0);
res -= m - l + 1;
}
for (int i = 2; i <= n; ++i) {
auto& other = t[i & 1 ^ 1], &now = t[i & 1];
now.clear();
v[i].push_back(m + 1);
int last = 0;
for (auto& y : v[i]) {
int l = other.ask(last + 1, y - 1);
int R = l ? l - 1 : y - 1, L = last + 1;
if (L <= R) now.modify(L, R, 0), res -= R - L + 1;
if (y <= m) now.modify(y, y, 0), last = y, res--;
}
}
printf("%lld\n", res);
}
return 0;
}