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 ...
随机推荐
- JustOj 1936: 小明A+B
题目描述 小明今年3岁了, 现在他已经能够认识100以内的非负整数, 并且能够进行100以内的非负整数的加法计算. 对于大于等于100的整数, 小明仅保留该数的最后两位进行计算, 如果计算结果大于等于 ...
- Ubuntu 为 root 帐号开启 SSH 登录
1. 修改 root 密码sudo passwd root 2. 以其他账户登录,通过 sudo nano 修改 /etc/ssh/sshd_config :xxx@ubuntu:~$ su - ro ...
- Python学习路线人工智能线性代数知识点汇总
人工智能和数据分析相关的线性代数知识.比如什么是矢量,什么是矩阵,矩阵的加减乘除.矩阵对角化,三角化,秩,QR法,最小二法.等等 矢量: 高中数学中都学过复数,负数表达式是: a+bi 复数实际上和二 ...
- 费马小定理与GCD&LCM
若 t = 1 , a ^ ( p - 2 ) 为 a 在取模 p 意义下的乘法逆元 通常用 inv 表示 证明: b * a =(三等)1(mod p) a ^ ( p - 2 ) * a =(三 ...
- ubunta apt install error
ubuntu系统: 用apt-get命令安装一些软件包时,总报错:E:could not get lock /var/lib/dpkg/lock -open等 出现这个问题的原因可能是有另外一个程序正 ...
- JS笔记—01
1.JS的代码一般在头部写2.当页面载入时,会执行位于body部分的JavaScript当被调用时,位于head部分的JavaScript被执行.3.要对外部的JS文件的一个变量操作,代码是写在内部J ...
- java基础之包装类型
包装类型引入该类型的原因: 因为基本数据类型不具备对象的特性,不能调用方法,所以有时需要将其转换为包装类. 包装类型有两大类方法: 1.将本类型和其它基本类型进行转换方法. ...
- mycat分片操作
mycat位于应用与数据库的中间层,可以灵活解耦应用与数据库,后端数据库可以位于不同的主机上.在mycat中将表分为两大类:对于数据量小且不需要做数据切片的表,称之为分片表:对于数据量大到单库性能,容 ...
- Golang面向对象_继承
package main import "fmt" type Person struct { name string //名字 sex byte //性别 age int //年龄 ...
- jQuery中prop和attr区别
问题 今天给一个button加onclick事件,由于最后button根据需要转成字符串,因此不能使用jQurey.click(),只能给button添加onclick属性的方式. 于是,$butto ...