Codeforces Round #219 (Div. 2) D题
4 seconds
256 megabytes
standard input
standard output
There is an n × m rectangular grid, each cell of the grid contains a single integer: zero or one. Let's call the cell on the i-th row and the j-th column as (i, j).
Let's define a "rectangle" as four integers a, b, c, d (1 ≤ a ≤ c ≤ n; 1 ≤ b ≤ d ≤ m). Rectangle denotes a set of cells of the grid {(x, y) : a ≤ x ≤ c, b ≤ y ≤ d}. Let's define a "good rectangle" as a rectangle that includes only the cells with zeros.
You should answer the following q queries: calculate the number of good rectangles all of which cells are in the given rectangle.
There are three integers in the first line: n, m and q (1 ≤ n, m ≤ 40, 1 ≤ q ≤ 3·105). Each of the next n lines contains m characters — the grid. Consider grid rows are numbered from top to bottom, and grid columns are numbered from left to right. Both columns and rows are numbered starting from 1.
Each of the next q lines contains a query — four integers that describe the current rectangle, a, b, c, d (1 ≤ a ≤ c ≤ n; 1 ≤ b ≤ d ≤ m).
For each query output an answer — a single integer in a separate line.
5 5 5
00101
00000
00001
01000
00001
1 2 2 4
4 5 4 5
1 2 5 2
2 2 4 5
4 2 5 3
10
1
7
34
5
4 7 5
0000100
0000010
0011000
0000000
1 7 2 7
3 1 3 1
2 3 4 5
1 2 2 7
2 2 4 7
3
1
16
27
52
//题意:给出一个矩阵每次输入给出查询的范围a,b,c,d求出范围内由0做成的的矩形的个数
sol: 矩阵上的dp,可以这么想例如是1维的给出:000111000,dp方程很容易dp[i] = dp[i-1]+sum[i]; 为什么,加上前缀和嫩? 可以验证一下每次向后移动一位则增加的矩阵数目为当前位和前面每一位组成的矩形加上当前的矩形,正好是前缀和(fuck-理解了好久),转换到二维上就容易了,同样是矩阵从小到大枚举
1 #include <cstdio>
2 #include <vector>
3 #include <set>
4 #include <algorithm>
5 using namespace std;
6 //using PII = pair<int,int>;
7 //using LL = long long;
8 const int INF = ;
9
int a[][],cnt[][][][];
int s[][],sum[][][][];
int main(){
int n,m,cs;
scanf("%d%d%d",&n,&m,&cs);
for(int i=;i<=n;i++) for(int j=;j<=m;j++){
scanf("%1d",&a[i][j]);
s[i][j]=s[i-][j]+s[i][j-]-s[i-][j-]+a[i][j];
// printf("%d %d %d\n",i,j,s[i][j]);
}
for(int i=n;i>=;i--) for(int j=m;j>=;j--)
for(int r=i;r<=n;r++) for(int c=j;c<=m;c++){
int dot=s[r][c]-s[r][j-]-s[i-][c]+s[i-][j-];
// printf("%d %d %d %d %d\n",i,j,r,c,dot);
cnt[i][j][r][c]=cnt[i][j][r-][c]+cnt[i][j][r][c-]-cnt[i][j][r-][c-]+!dot;
}
for(int i=n;i>=;i--) for(int j=m;j>=;j--)
for(int r=i;r<=n;r++) for(int c=j;c<=m;c++)
sum[i][j][r][c]=sum[i+][j][r][c]+sum[i][j+][r][c]-sum[i+][j+][r][c]+cnt[i][j][r][c];
while(cs--)
{
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
printf("%d\n",sum[a][b][c][d]);
}
36 }
Codeforces Round #219 (Div. 2) D题的更多相关文章
- 数学 Codeforces Round #219 (Div. 2) B. Making Sequences is Fun
题目传送门 /* 数学:这题一直WA在13组上,看了数据才知道是计算cost时超long long了 另外不足一个区间的直接计算个数就可以了 */ #include <cstdio> #i ...
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- Codeforces Round #612 (Div. 2) 前四题题解
这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...
- Codeforces Round #713 (Div. 3)AB题
Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...
- Codeforces Round #552 (Div. 3) A题
题目网址:http://codeforces.com/contest/1154/problem/ 题目意思:就是给你四个数,这四个数是a+b,a+c,b+c,a+b+c,次序未知要反求出a,b,c,d ...
- Codeforces Round #412 Div. 2 补题 D. Dynamic Problem Scoring
D. Dynamic Problem Scoring time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Codeforces Round #219 (Div. 1)(完全)
戳我看题目 A:给你n个数,要求尽可能多的找出匹配,如果两个数匹配,则ai*2 <= aj 排序,从中间切断,分成相等的两半后,对于较大的那一半,从大到小遍历,对于每个数在左边那组找到最大的满足 ...
- Codeforces Round #219 (Div. 2) E. Watching Fireworks is Fun
http://codeforces.com/contest/373/problem/E E. Watching Fireworks is Fun time limit per test 4 secon ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
随机推荐
- 清北考前刷题day2早安
/* 做法一:按h sort一遍,对于一段区间[i,j],高度花费就是h[j]-h[i] 然后枚举区间,把区间内C排序,一个一个尽量选即可. n^3logn 标算:n^3 dp 高度排序,保证从前往后 ...
- 407 Trapping Rain Water II 接雨水 II
给定一个m x n的矩阵,其中的值均为正整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水.说明:m 和 n 都是小于110的整数.每一个单位的高度都大于0 且小于 20000. ...
- headroom.js使用
为页面顶部多留些空间.在不需要页头时将其隐藏 需要添加的css代码 .headroom { transition: transform 200ms linear; } .headroom--pinne ...
- python 模块-easygui.buttonbox
2018-03-0315:43:11 ): Yes_or_No = easygui.buttonbox("是否良品?", choices=['Yes', 'No', '退出']) ...
- vc++中 .H 头文件引用的顺序与符号关系
在使用 #include "math.h" 和 #include <math.h>时,引号 与尖括号的区别如下 此时math.h_1 在工程文件中 math.h_2 ...
- QT,折腾的几天-----关于 QWebEngine的使用
几天前,不,应该是更早以前,就在寻找一种以HTML5+CSS+Javascript的方式来写桌面应用的解决方案,为什么呢?因为前端那套可以随心所欲的写样式界面啊,恩.其实我只是想使用H5的一些新增功能 ...
- Shell基础笔记一
由于工作需要,开始学习Shell编程,都是一些简单的基础知识,现整理收集分享出来,希望对大家有帮助 -------------------------------------------------- ...
- node遍历给定目录下特定文件,内容合并到一个文件
遍历目录用了fs.readdir这个异步方法,得到当前目录下所有的文件和目录的一个数组.然后判断: if文件,并且后缀符合设定的规则(本文例子是符合后缀ts,js)直接用同步方法写入, if目录,继续 ...
- Rest 参数(...)
javascript 之Rest 参数(...) ES6 Rest参数 Rest就是为解决传入的参数数量不一定, rest parameter(Rest 参数) 本身就是数组,数组的相关的方法都可以用 ...
- 在添加新内容时,creatat没有数据
首先找到controller中的add方法,然后是执行了Service.insert()方法 然后找到service对应的impl方法, impl方法中的 public void insert(Cus ...