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
For the first example, there is a 5 × 5 rectangular grid, and the first, the second, and the third queries are represented in the following image.
- For the first query, there are 10 good rectangles, five 1 × 1, two 2 × 1, two 1 × 2, and one 1 × 3.
- For the second query, there is only one 1 × 1 good rectangle.
- For the third query, there are 7 good rectangles, four 1 × 1, two 2 × 1, and one 3 × 1.
题意:给你一个n*m的矩形,q个询问,求左上角的(a,b)位置到(c, d)位置所有全0的矩形的个数
思路:四维前缀和;利用容斥,写写方程,或者dp的思想也行;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stdlib.h>
#include<time.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=+,M=1e6+,inf=1e9+;
const ll INF=5e17+,mod=1e9+;
///数组大小
int a[N][N],cnt[N][N][N][N];
int s[N][N],sum[N][N][N][N];
/// a表示初始数组
/// cnt表示以i.j为固定起点的总0个数
/// s表示1,1的1的数目,也就是前缀和
/// sum表示答案
int main()
{
int n,m,q;
scanf("%d%d%d",&n,&m,&q);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
scanf("%1d",&a[i][j]);
s[i][j]=a[i][j]+s[i-][j]+s[i][j-]-s[i-][j-];
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
for(int k=i;k<=n;k++)
{
for(int l=j;l<=m;l++)
{
int x=s[k][l]-s[i-][l]-s[k][j-]+s[i-][j-];
cnt[i][j][k][l]=cnt[i][j][k][l-]+cnt[i][j][k-][l]-cnt[i][j][k-][l-]+!x;
//cout<<i<<" "<<j<<" "<<k<<" "<<l<<" "<<cnt[i][j][k][l]<<endl;
}
}
}
}
for(int i=n;i>=;i--)
{
for(int j=m;j>=;j--)
{
for(int k=i;k<=n;k++)
{
for(int l=j;l<=m;l++)
{
sum[i][j][k][l]=sum[i+][j][k][l]+sum[i][j+][k][l]-sum[i+][j+][k][l]+cnt[i][j][k][l];
}
}
}
}
while(q--)
{
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
printf("%d\n",sum[a][b][c][d]);
}
return ;
}
Codeforces Round #219 (Div. 2) D. Counting Rectangles is Fun 四维前缀和的更多相关文章
-
数学 Codeforces Round #219 (Div. 2) B. Making Sequences is Fun
题目传送门 /* 数学:这题一直WA在13组上,看了数据才知道是计算cost时超long long了 另外不足一个区间的直接计算个数就可以了 */ #include <cstdio> #i ...
-
Codeforces Round #219 (Div. 2) D题
D. Counting Rectangles is Fun time limit per test 4 seconds memory limit per test 256 megabytes inpu ...
-
Codeforces Round #371 (Div. 2) D. Searching Rectangles 交互题 二分
D. Searching Rectangles 题目连接: http://codeforces.com/contest/714/problem/D Description Filya just lea ...
-
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 #579 (Div. 3) B Equal Rectangles、C. Common Divisors
B Equal Rectangles 题意: 给你4*n个数,让你判断能不能用这个4*n个数为边凑成n个矩形,使的每个矩形面积相等 题解: 原本是想着用二分来找出来那个最终的面积,但是仔细想一想,那个 ...
-
Codeforces Round #219 (Div. 1) C. Watching Fireworks is Fun
C. Watching Fireworks is Fun time limit per test 4 seconds memory limit per test 256 megabytes input ...
-
Codeforces Round #219 (Div. 2) B. Making Sequences is Fun
B. Making Sequences is Fun time limit per test 2 seconds memory limit per test 256 megabytes input s ...
-
Codeforces Round #248 (Div. 1) B. Nanami&#39;s Digital Board 暴力 前缀和
B. Nanami's Digital Board 题目连接: http://www.codeforces.com/contest/434/problem/B Description Nanami i ...
随机推荐
-
CPU过高的排查方法
一个应用占用CPU很高,除了确实是计算密集型应用之外,通常原因都是出现了死循环. (友情提示:本博文章欢迎转载,但请注明出处:hankchen,http://www.blogjava.net/hank ...
-
net SqlBulkCopy拷贝数据的问题
服务器配置:windows 2008 ,sql server 2008, oracle 10g. 在本地和同样配置的其他服务器上同样的程序,数据200万都很快就采集过来了,但是在发布的服务器上,如果b ...
-
The entity type <;type>; is not part of the model for the current context
这是在网站里遇到的一个错误,自动生成的不能手动添加, reference: http://*.com/questions/19695545/the-entity-type-xx ...
-
《黄聪:手机移动站SEO优化教程》3、如何禁止百度对PC网站进行自动转码
视频地址:http://v.youku.com/v_show/id_XNzE2OTM0NzU2.html
-
Json.Net使用JSON Schema验证JSON格式【实例】
给出一个Json,验证其格式是否符合规则. { "coord": { //对象 "lon": 145.77, "lat": -16.92 } ...
-
第八届河南省赛G.Interference Signal(dp)
G.Interference Signal Time Limit: 2 Sec Memory Limit: 128 MB Submit: 35 Solved: 17 [Submit][Status ...
-
java系列笔记---正则表达式(2)
正则表达式 说真的正则表达式真不好写,当我收集资料准备开始写的时候,发现收集的东西越来越多范围也越来越广,我文章的前提就是文章要清晰, 在缕清自己思路之后,我从先简后难的方式来写有关正表达式,你们如果 ...
-
Asp.Net MVC Https设置
1. IIS设置 1.1 创建SSL证书 点击左侧菜单栏顶部,点击“功能视图”里的“服务器证书”: 点击“创建自动签名证书”创建自动签名证书: 1.2 设置SSL证书 点开网站,在“功能视图”里点 ...
-
解决Linux 环境 GLIBCXX_3.4.15&#39; not found问题
升级Centos系统之后,运行filezilla时,出现如下错误的提示信息: ./filezilla: /usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.15 ...
-
CPU TFLOPS 计算
CPU TFLOPS 计算 姚伟峰 yaoweifeng0301@126.com] http://www.cnblogs.com/Matrix_Yao/ 深度学习任务是一个计算密集型任务,所以很关注计 ...