Codeforces Round #219 (Div. 2) D. Counting Rectangles is Fun 四维前缀和
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's Digital Board 暴力 前缀和
B. Nanami's Digital Board 题目连接: http://www.codeforces.com/contest/434/problem/B Description Nanami i ...
随机推荐
- Autel MaxiSys Pro MS908P Software Update Gudie
This article aims to guide on how to update software for Autel MaxiSys Pro. (Suitable for MaxiDiag E ...
- MaxiSYS Elite
The Maxisys Elite is Autel UK’s top of the range diagnostic and analysis scanner with advanced J2534 ...
- 深度点评五种常见WiFi搭建方案
总结十年无线搭建经验,针对企业常见的五种办公室无线网络方案做个简要分析,各种方案有何优劣,又适用于那种类型的企业. 方案一:仅路由器或AP覆盖 简述:使用路由器或AP覆盖多个无线盲区,多个AP的部署实 ...
- APIClound 弹出层 Frame
JS api.openFrame({ name: 'showPic', url: './showPic.html', rect: { // x: api.pageParam.marginBottom, ...
- 离开(切换)当前页面时改变页面title
document.addEventListener('visibilitychange', function () { if (document.visibilityState == 'hidden' ...
- c++ STL中的set和multiset
1.结构 set和multiset会根据特定的排序原则将元素排序.两者不同之处在于,multisets允许元素重复,而set不允许重复. set中的元素可以是任意类型的,但是由于需要排序,所以元素必须 ...
- MySql概念(二)
一.Sql规范? sql是Structured Query Language(结构化查询语言)的缩写.SQL是专为数据库而建立的操作命令集,是一种功能齐全的数据库语言. 在使用它时,只需要发出“做什么 ...
- 头像上传uploadPreview插件
原文链接:https://blog.csdn.net/Alisa_L/article/details/52923953 uploadPreview 今天写头像上传,使用到uploadPreview插件 ...
- 正则表达式验证HTTP地址是否合法
转载:https://blog.csdn.net/fsdad/article/details/52637426 判断url是否合法 const std::regex urlpattern(" ...
- FJUT3591 侦测到在途的聚变打击(最小不可相交路径覆盖)题解
题意:给你n个点,点间m条路,给出在每条路要走的时间.现在有q个任务,要摧毁q个点,每次提供ci和ti表示在时间ti摧毁点ci(必须正好在时间ti才能摧毁),每个点可能需要多次摧毁(同一时间能在同一个 ...