当前位置: 首页 > news >正文

F - Grid Clipping

F - Grid Clipping

Problem Statement

There is a grid with $H$ rows and $W$ columns. The cell at the $r$-th row from the top and $c$-th column from the left is called cell $(r,c)$. Each cell is painted black or white: cell $(R_k,C_k)$ is black for $k=1,2,\ldots,N$, and the remaining $HW-N$ cells are white.

Find the number of rectangular regions in this grid with $h$ rows and $w$ columns such that all cells contained in them are painted white.

More formally, find the number of pairs of integers $(r_0, c_0)$ satisfying all of the following conditions:

  • $1\le r_0 \le H-h+1$
  • $1\le c_0 \le W-w+1$
  • Cell $(r_0+i,c_0+j)$ is painted white for all pairs of integers $(i,j)$ satisfying $0\le i< h,\ 0\le j< w$.

Constraints

  • $1\le h\le H\le 10^9$
  • $1\le w\le W\le 10^9$
  • $0\le N\le 2\times 10^5$
  • $1\le R_k\le H$
  • $1\le C_k\le W$
  • $(R_{k_1},C_{k_1}) \neq (R_{k_2},C_{k_2})$ $(k_1 \neq k_2)$
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

$H$ $W$ $h$ $w$ $N$
$R_1$ $C_1$
$R_2$ $C_2$
$\vdots$
$R_N$ $C_N$

Output

Output the answer.


Sample Input 1

3 4 2 2 3
1 3
2 4
3 1

Sample Output 1

2

The two rectangular regions enclosed in red in the figure below satisfy all the conditions.


Sample Input 2

4 4 3 2 2
2 2
3 4

Sample Output 2

0

No rectangular region satisfies all the conditions.


Sample Input 3

449 449 3 14 0

Sample Output 3

194892

Sample Input 4

31 9 5 7 10
14 8
8 4
18 8
12 1
8 5
9 6
18 1
14 7
5 6
26 7

Sample Output 4

12

 

解题思路

  相较于直接找出不含黑色格子的 $h \times w$ 矩形,我们更容易确定包含至少一个黑色格子的 $h \times w$ 矩形。对于每一个黑色格子 $(R_i, C_i)$,所有包含它的 $h \times w$ 矩形的左上角坐标 $(r, c)$ 满足 $\max(1, R_i-h+1) \leq r \leq \min(H-h+1, R_i)$ 且 $\max(1, C_i-w+1) \leq c \leq \min(W-w+1, C_i)$。

  这些左上角坐标 $(r, c)$ 共同构成一个矩形区域,我们称其为“不合法矩形”。以左上角坐标 $(r, c)$(其中 $1 \leq r \leq H - h + 1$,$1 \leq c \leq W - w + 1$)来唯一标识每一个可能的 $h \times w$ 矩形,那么每个不合法矩形对应的区域大小(即所覆盖的左上角个数),就表示包含该黑色格子的 $h \times w$ 矩形数量。

  一个直观的想法是用所有 $h \times w$ 矩形的总数 $(H-h+1) \cdot (W-w+1)$ 减去各个不合法矩形的面积,来得到只含白色格子的 $h \times w$ 矩形数量。然而这样做存在问题:如果一个 $h \times w$ 矩形包含多个黑色格子,那么它的左上角 $(r, c)$ 会被多个不合法矩形同时覆盖,导致被重复减去多次。正确的做法应该是考虑容斥原理,即总矩形数减去所有不合法矩形并集的面积,这样每个包含至少一个黑色格子的矩形恰好被减去一次。

  求若干矩形的并集面积可以用扫描线算法解决,可以参考【模板】扫描线。每个不合法矩形可以用左上角 $\left(\max(1, R_i-h+1), \max(1, C_i-w+1)\right)$ 和右下角 $\left(\min(H-h+1, R_i), \min(W-w+1, C_i)\right)$ 来表述。需要注意的是,题目中的面积是以格子为单位计算的,而标准的扫描线求面积通常处理连续坐标。为了统一,我们可以将右下角的坐标统一加 $1$,变为 $\left(\min(H-h+1, R_i)+1, \min(W-w+1, C_i)+1\right)$,这样矩形的面积就可以直接表示为横坐标差值与纵坐标差值的乘积,与扫描线的标准形式保持一致。另外由于纵坐标的取值范围很大,还需要进行离散化处理。

  AC 代码如下,时间复杂度为 $O(n \log{n})$:

#include <bits/stdc++.h>
using namespace std;typedef long long LL;const int N = 4e5 + 5;array<int, 4> p[N];
int xs[N * 2], sz;
struct Node {
    int l, r, mn, s, add;
}tr[N * 4];void pushup(int u) {
    tr[u].mn = min(tr[u << 1].mn, tr[u << 1 | 1].mn);
    tr[u].s = 0;
    if (tr[u << 1].mn == tr[u].mn) tr[u].s += tr[u << 1].s;
    if (tr[u << 1 | 1].mn == tr[u].mn) tr[u].s += tr[u << 1 | 1].s;
}void build(int u, int l, int r) {
    tr[u] = {l, r};
    if (l == r) {
        tr[u].s = xs[l + 1] - xs[l];
    }
    else {
        int mid = l + r >> 1;
        build(u << 1, l, mid);
        build(u << 1 | 1, mid + 1, r);
        pushup(u);
    }
}void upd(int u, int c) {
    tr[u].mn += c;
    tr[u].add += c;
}void pushdown(int u) {
    if (tr[u].add) {
        upd(u << 1, tr[u].add);
        upd(u << 1 | 1, tr[u].add);
        tr[u].add = 0;
    }
}void modify(int u, int l, int r, int c) {
    if (tr[u].l >= l && tr[u].r <= r) {
        upd(u, c);
    }
    else {
        pushdown(u);
        int mid = tr[u].l + tr[u].r >> 1;
        if (l <= mid) modify(u << 1, l, r, c);
        if (r >= mid + 1) modify(u << 1 | 1, l, r, c);
        pushup(u);
    }
}int find(int x) {
    return lower_bound(xs + 1, xs + sz + 1, x) - xs;
}int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m, h, w, k;
    cin >> n >> m >> h >> w >> k;
    for (int i = 0; i < k; i++) {
        int x, y;
        cin >> x >> y;
        int l = max(1, y - w + 1), r = min(y, m - w + 1) + 1;
        p[i << 1] = {max(1, x - h + 1), l, r, 1};
        p[i << 1 | 1] = {min(x, n - h + 1) + 1, l, r, -1};
        xs[++sz] = l, xs[++sz] = r;
    }
    sort(xs + 1, xs + sz + 1);
    sz = unique(xs + 1, xs + sz + 1) - xs - 1;
    sort(p, p + 2 * k);
    if (sz) build(1, 1, sz - 1);
    LL ret = (n - h + 1ll) * (m - w + 1);
    for (int i = 0; i + 1 < k << 1; i++) {
        modify(1, find(p[i][1]), find(p[i][2]) - 1, p[i][3]);
        ret -= LL(p[i + 1][0] - p[i][0]) * (xs[sz] - xs[1] - !tr[1].mn * tr[1].s);
    }
    cout << ret;
    
    return 0;
}

 

参考资料

  Editorial - AtCoder Beginner Contest 449:https://atcoder.jp/contests/abc449/editorial/17260

  AtCoder Beginner Contest 449 题解(ABCDEF) - 知乎:https://zhuanlan.zhihu.com/p/2016300670106490612

http://www.jsqmd.com/news/539659/

相关文章:

  • HunyuanVideo-Foley效果对比:不同prompt长度对Foley音效细节影响分析
  • 告别阅读焦虑:fanqienovel-downloader打造个人数字阅读图书馆全攻略
  • 2026年USB转网口方案商趋势洞察--从技术到场景的适配选择
  • 开发自己的IValueConverter
  • 2026港校申请全攻略:硬核门槛解析与高端规划机构甄选 - 品牌2026
  • 2026机动车行人事故道路交通事故快速勘查系统厂商哪家好?怎么选更实用 - 品牌2026
  • 信号(signal)是“异步中断”,不能直接做复杂操作,异步中断是什么意思?
  • OpenClaw+GLM-4.7-Flash:自动化邮件处理系统搭建指南
  • 某鱼关键词搜索商品接口实战:合规调用 + 二手商品结构化解析(2026 最新版)
  • QRazyBox:5分钟快速修复损坏二维码的终极免费工具
  • 5步征服显存难题:多语言MiniLM模型量化优化实战指南
  • 全面对比:RTO设备生产企业的优势与特点 - 品牌推荐大师1
  • 喵飞AI深耕天津本土,OPC社区服务打通个人与企业AI落地堵点
  • 破解PS3手柄连接难题:BthPS3驱动3大突破点实现Windows 11完美适配
  • League-Toolkit 程序启动故障的 3 套分级解决方案
  • League-Toolkit:提升游戏体验的英雄联盟智能辅助工具集
  • 多平台网络资源捕获工具:突破下载限制的技术实现与场景化应用
  • 自动驾驶之心实习生招募|上海线下,一起做点真东西
  • 使用腾讯云 ClawPro 助手打造南京旅游攻略应用实践
  • 如何用Idle Master高效智能挂卡?Steam交易卡片自动收集全攻略
  • 拒绝“爆表”与“盲区”:青岛格林诺尔凭借20000ppm量程树立便携式VOC检测仪行业安全新防线 - 品牌推荐大师1
  • 【无人机控制】基于人工势场法的四旋翼无人机轨迹规划几何控制器附matlab代码
  • 2025年雀魂Mod工具终极指南:从痛点分析到实践探索
  • 破解AutoDock Vina金属对接难题:3种专业方案实战深度解析
  • Cisco交换机show arp命令实战:如何快速定位网络中的‘神秘设备’?
  • 中小团队 Openclaw 落地实战:选对中转,运维成本降 80%,调用成本砍半
  • DMG2IMG终极指南:3分钟掌握苹果DMG文件跨平台转换技巧
  • 【多机器人】基于搜索(CBS)框架结合时空 A 星算法实现栅格地图下的无冲突多机器人路径规划附matlab代码
  • Illustrator批量替换实战指南:用ReplaceItems释放设计效率
  • 5路HDMI编码器如何接入海康NVR?RTSP多通道配置保姆级教程