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,可是 ...
随机推荐
- 士兵杀敌 三 --- O( 1 ) 的时间复杂度 .
一看就是 十分简单的 题 , 然后上去开始无脑程序 超时~~~ 感觉时间复杂度 , 已经很低了 , 但是并没有什么卵用 . #include<stdio.h> #in ...
- 图论 HDOJ 5348 MZL's endless loop
题目传送门 /* 题意:给一个n个点,m条边的无向图,要求给m条边定方向,使得每个定点的出入度之差的绝对值小于等于1. 输出任意一种结果 图论:一个图,必定存在偶数个奇度顶点.那么从一个奇度定点深搜, ...
- 【转】mysql中select用法
转自:http://blog.sina.com.cn/s/blog_a74f39a201013c3b.html 1.选择所有的记录 select * from table_name; 其中*表示表中的 ...
- 【转】mysql INSERT的用法
转自:http://www.cnblogs.com/ggjucheng/archive/2012/11/05/2754938.html insert的语法 INSERT [LOW_PRIORITY | ...
- log4j建立propertie后要建立log4j2.xml
log4j.properties ### \u8BBE\u7F6E### log4j.rootLogger = debug,stdout,D,E ### \u8F93\u51FA\u4FE1\u606 ...
- 一个JavaScript贷款计算器
通过本案例,将会学到: . 如何在文档中查找元素 . 如何通过表单input元素来获取用户的输入数据 . 如何通过文档元素来设置HTML内容 . 如何将数据存储在浏览器中 . 如何使用脚本发起HTTP ...
- Dom编程的入门
<html><head> <meta http-equiv="Content-Type" content="text/html; ch ...
- CF814B An express train to reveries
思路: 模拟,枚举. 实现: #include <iostream> using namespace std; ; int a[N], b[N], cnt[N], n, x, y; int ...
- js中true和false判断
布尔类型里只有这6参数个返回false,其它都为true Boolean(false) // false Boolean(undefined) // false Boolean(null) // fa ...
- 用Docker实现nginx多端口
一.安装docker 需要阿里的epel源,需要联网 [root@bogon ~]#yum -y install docker [root@bogon ~]#systemctl start docke ...