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

CodeForces 611:New Year and Domino ← 二维前缀和

【题目来源】
https://codeforces.com/contest/611/problem/C

【题目描述】
They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so.
Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right.
Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid.
Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle?

大意:给出一个由 '.' 及 '#' 构成的 h×w 的矩形格子,其中 “." 代表空,"#" 代表满。现在将一个大小为 1*2 的长方形多米诺骨牌,放进这个 h×w 的矩形格子的多个子矩形中,问在每个子矩形里有多少种放法。

【输入格式】
The first line of the input contains two integers h and w (
1≤h,w≤500) – the number of rows and the number of columns, respectively.
The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' — denoting an empty or forbidden cell, respectively.
The next line contains a single integer q (
1≤q≤100000) — the number of queries.
Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1≤r1i≤r2i≤h,1≤c1i≤c2i≤w) — the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle.

大意:第一行,两个整数 h 和 w,表示矩形格子的行数和列数;接下来 h 行,每行是由 '.' 或 '#' 构成的长度为 w 的字符串。其中,“." 代表空,"#" 代表满;接下来一行,为一个整数 q,表示询问次数;接下来 q 行,每行 4 个整数,每行的头两个数,表示子矩形的左上角坐标,每行的后两个数,表示子矩形的右下角坐标。

【输出格式】
Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle.

大意:q 行,每行一个整数,表示在给定子矩形中放置一块多米诺骨牌的方法数。

【输入样例1】

5 8
....#..#
.#......
##.#....
##..#.##
........
4
1 1 2 3
4 1 4 1
1 2 4 5
2 5 5 8

【输出样例1】

4
0
10
15

【输入样例2】

7 39
.......................................
.###..###..#..###.....###..###..#..###.
...#..#.#..#..#.........#..#.#..#..#...
.###..#.#..#..###.....###..#.#..#..###.
.#....#.#..#....#.....#....#.#..#..#.#.
.###..###..#..###.....###..###..#..###.
.......................................
6
1 1 3 20
2 10 6 30
2 10 7 30
2 2 7 7
1 7 7 7
1 8 7 8

【输出样例2】

53
89
120
23
0
2

【说明】
A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways.

大意:下面的红色框对应于第一个示例的第一个查询。针对此查询,多米诺骨牌有4种摆放方式。

【算法分析】
● 前缀和及差分:https://blog.csdn.net/hnjzsyjyj/article/details/145290457

【算法代码】

#include<bits/stdc++.h>
using namespace std;

const int maxn=505;
char g[maxn][maxn];
int s[maxn][maxn];
int tx[maxn][maxn],ty[maxn][maxn];

int main() {
    int n,m;
    cin>>n>>m;

    for(int i=1; i<=n; i++) {
        for(int j=1; j<=m; j++) {
            cin>>g[i][j];
        }
    }

    for(int i=1; i<=n; i++) {
        for(int j=1; j<=m; j++) {
            if(g[i-1][j]=='.' && g[i][j]=='.') {
                s[i][j]++;
                tx[i][j]=1;
            }
            if(g[i][j-1]=='.' && g[i][j]=='.') {
                s[i][j]++;
                ty[i][j]=1;
            }
            s[i][j]+=s[i-1][j]+s[i][j-1]-s[i-1][j-1];
        }
    }

    int q;
    cin>>q;
    while(q--) {
        int x1,x2,y1,y2;
        cin>>x1>>y1>>x2>>y2;
        int ans=s[x2][y2]-s[x2][y1-1]-s[x1-1][y2]+s[x1-1][y1-1];
        for(int i=x1; i<=x2; i++) ans-=ty[i][y1];
        for(int i=y1; i<=y2; i++) ans-=tx[x1][i];
        cout<<ans<<endl;
    }

    return 0;
}

/*
in:
5 8
....#..#
.#......
##.#....
##..#.##
........
4
1 1 2 3
4 1 4 1
1 2 4 5
2 5 5 8

out:
4
0
10
15
*/




【参考文献】
https://zhuanlan.zhihu.com/p/568454667
https://blog.csdn.net/hnjzsyjyj/article/details/145292538
https://blog.csdn.net/hnjzsyjyj/article/details/145290457
https://www.cnblogs.com/WABoss/p/5700154.html

https://blog.csdn.net/Z_sea/article/details/81180751



 


http://www.kler.cn/a/519943.html

相关文章:

  • VB6.0 显示越南语字符
  • 网易Android开发面试题200道及参考答案 (下)
  • kafka-保姆级配置说明(consumer)
  • 【Linux】21.基础IO(3)
  • C++ list 容器用法
  • 数据库的JOIN连接查询算法
  • 单链表OJ篇
  • docker日志保留策略设置
  • Avalonia系列文章之再试牛刀
  • 【数据结构】时间复杂度空间复杂度
  • 用python实现接口下单
  • 用Ollama跑DeepSeek R1
  • 【Eigen教程】矩阵、数组和向量类(二)
  • P3978 [TJOI2015] 概率论
  • 利用metaGPT多智能体框架实现智能体-2
  • MarsCode青训营打卡Day10(2025年1月23日)|稀土掘金-147.寻找独一无二的糖葫芦串、119.游戏队友搜索
  • 什么是 Token,Token 的作用是什么?
  • 突破与重塑:逃离Java舒适区,借Go语言复刻Redis的自我突破和成长
  • flutter-android混合编译,原生接入
  • 在 JavaScript 中调用 OpenAI 的接口初识
  • 3-scala的类
  • 自定义数据集使用框架的线性回归方法对其进行拟合
  • 第30章 测试驱动开发中的设计模式解析(Python 版)
  • 三年级数学知识边界总结思考-下册
  • GSI快速收录服务:让你的网站内容“上架”谷歌
  • 从Spring请求处理到分层架构与IOC:注解详解与演进实战