`
20386053
  • 浏览: 461432 次
文章分类
社区版块
存档分类
最新评论

CodeForces 372B. Counting Rectangles is Fun

 
阅读更多


B. Counting Rectangles is Fun
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There is ann × mrectangular grid, each cell of the grid contains a single integer: zero or one. Let's call the cell on thei-th row and thej-th column as(i, j).

Let's define a "rectangle" as four integersa, 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 followingqqueries: calculate the number of good rectangles all of which cells are in the given rectangle.

Input

There are three integers in the first line:n,mandq(1 ≤ n, m ≤ 40, 1 ≤ q ≤ 3·105). Each of the nextnlines containsmcharacters — 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 nextqlines contains a query — four integers that describe the current rectangle,a,b,c,d(1 ≤ a ≤ c ≤ n;1 ≤ b ≤ d ≤ m).

Output

For each query output an answer — a single integer in a separate line.

Sample test(s)
input
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
output
10
1
7
34
5
input
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
output
3
1
16
27
52
Note

For the first example, there is a5 × 5rectangular grid, and the first, the second, and the third queries are represented in the following image.

  • For the first query, there are10good rectangles, five1 × 1, two2 × 1, two1 × 2, and one1 × 3.
  • For the second query, there is only one1 × 1good rectangle.
  • For the third query, there are7good rectangles, four1 × 1, two2 × 1, and one3 × 1.

4D consecutive sums 涨姿势了。。。

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

int m[50][50];
int rui[50][50];
int det[50][50][50][50];
int ans[50][50][50][50];
int x,y,q;
char str[50];

int main()
{
    scanf("%d%d%d",&x,&y,&q);
    for(int i=0;i<x;i++)
    {
        scanf("%s",str);
        for(int j=0;j<y;j++)
        {
            m[i][j]=str[j]-'0';
        }
    }
    for(int i=0;i<x;i++)
    {
        for(int j=0;j<y;j++)
        {
            rui[i+1][j+1]=rui[i+1][j]+rui[i][j+1]-rui[i][j]+m[i][j];
        }
    }
    for(int i=0;i<x;i++)
    {
        for(int j=0;j<y;j++)
        {
            for(int k=i;k<x;k++)
            {
                for(int l=j;l<y;l++)
                {
                    if(rui[k+1][l+1]+rui[i][j]-rui[i][l+1]-rui[k+1][j]==0)
                    {
                        det[i][j][k][l]=1;
                    }
                }
            }
        }
    }
    for(int i=0;i<x;i++)
    {
        for(int j=0;j<y;j++)
        {
            for(int k=0;k<x;k++)
            {
                for(int l=0;l<y;l++)
                {
                    ans[i+1][j+1][k+1][l+1]
                    =ans[i][j+1][k+1][l+1]
                    +ans[i+1][j][k+1][l+1]
                    +ans[i+1][j+1][k][l+1]
                    +ans[i+1][j+1][k+1][l]
                    -ans[i][j][k+1][l+1]
                    -ans[i][j+1][k][l+1]
                    -ans[i][j+1][k+1][l]
                    -ans[i+1][j][k][l+1]
                    -ans[i+1][j][k+1][l]
                    -ans[i+1][j+1][k][l]
                    +ans[i+1][j][k][l]
                    +ans[i][j+1][k][l]
                    +ans[i][j][k+1][l]
                    +ans[i][j][k][l+1]
                    -ans[i][j][k][l]
                    +det[i][j][k][l];
                }
            }
        }
    }
    while(q--)
    {
        int a,b,c,d;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        a--; b--; c--; d--;
        printf("%d\n",
               ans[c+1][d+1][c+1][d+1]
               -ans[a][d+1][c+1][d+1]
               -ans[c+1][b][c+1][d+1]
               -ans[c+1][d+1][a][d+1]
               -ans[c+1][d+1][c+1][b]
               +ans[a][b][c+1][d+1]
               +ans[a][d+1][a][d+1]
               +ans[a][d+1][c+1][b]
               +ans[c+1][b][a][d+1]
               +ans[c+1][b][c+1][b]
               +ans[c+1][d+1][a][b]
               -ans[c+1][b][a][b]
               -ans[a][d+1][a][b]
               -ans[a][b][c+1][b]
               -ans[a][b][a][d+1]
               +ans[a][b][a][b]
               );
    }
    return 0;
}

一种更加简洁的写法。。。。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int n, m, q, sum[50][50], ct[50][50][50][50];
char map[50][50];

int main()
{
	cin >> n >> m >> q;
	for (int i = 0; i < n; i++)
	{
		cin >> map[i];
	}
	for (int i = n - 1; i >= 0; i--)
	{
		for (int j = m - 1; j >= 0; j--)
		{
			sum[i][j] = sum[i + 1][j] + sum[i][j + 1] - sum[i + 1][j + 1] + (map[i][j] == '1');
		}
	}
	for (int x1 = n - 1; x1 >= 0; x1--)
	{
		for (int x2 = x1 + 1; x2 <= n; x2++)
		{
			for (int y1 = m - 1; y1 >= 0; y1--)
			{
				for (int y2 = y1 + 1; y2 <= m; y2++)
				{
					int &c = ct[x1][y1][x2][y2];
					c = ct[x1 + 1][y1][x2][y2]
						+ ct[x1][y1 + 1][x2][y2]
						+ ct[x1][y1][x2 - 1][y2]
						+ ct[x1][y1][x2][y2 - 1]

						- ct[x1][y1][x2 - 1][y2 - 1]
						- ct[x1][y1 + 1][x2][y2 - 1]
						- ct[x1][y1 + 1][x2 - 1][y2]
						- ct[x1 + 1][y1][x2][y2 - 1]
						- ct[x1 + 1][y1][x2 - 1][y2]
						- ct[x1 + 1][y1 + 1][x2][y2]

						+ ct[x1][y1 + 1][x2 - 1][y2 - 1]
						+ ct[x1 + 1][y1][x2 - 1][y2 - 1]
						+ ct[x1 + 1][y1 + 1][x2][y2 - 1]
						+ ct[x1 + 1][y1 + 1][x2 - 1][y2]

						- ct[x1 + 1][y1 + 1][x2 - 1][y2 - 1];
					if (sum[x1][y1] - sum[x1][y2] - sum[x2][y1] + sum[x2][y2] == 0) c++;
				}
			}
		}
	}
	while (q--)
	{
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		a--; b--;
		cout << ct[a][b][c][d] << endl;
	}
	return 0;
}



分享到:
评论

相关推荐

    zhidd#ZXBlog#Codeforces - 1107B. Digital root & 1107C. Brutality

    Codeforces - 1107B. Digital root & 1107C. Brutality(规律 & 贪心)Codeforces - 1107B.

    Codeforces round 678 D2_Codeforces_源码.zip

    Codeforces 是一个知名的在线编程竞赛平台,吸引了众多程序员参与,以提升编程技能和解决算法问题的能力。"Codeforces round 678 D2" 指的是一场特定的编程比赛,其中"D2"可能代表该轮比赛的难度级别或类别,可能是...

    codeforces算法竞赛.zip

    1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,...

    (源码)基于Python的Codeforces题目集锦.zip

    # 基于Python的Codeforces题目集锦 ## 项目简介 本项目是一个基于Python的Codeforces题目集锦,旨在通过Python脚本来解决Codeforces平台上的各种算法和数据结构问题。项目包含了多个Python脚本,每个脚本对应一个...

    Codeforces 988 D. Points and Powers of Two(数学+结论)

    《Codeforces 988 D. Points and Powers of Two》是一道融合了数学与算法的编程竞赛题目。问题的核心在于找到一个数组中的子序列,使得其中任意两个元素之间的差值都是2的幂次。目标是找到这样的子序列,其长度尽...

    CodeForces-Div.2A

    Watermelon: http://codeforces.com/problemset/problem/4/A 2- 71A. Way Too Long Words: http://codeforces.com/problemset/problem/71/A 3- 118A. String Task: ...

    Educational Codeforces Round 157D. XOR Construction

    Educational Codeforces Round 157D. XOR Construction

    Codeforces 1324 F. Maximum White Subtree (树形dp) /详解

    F. Maximum White Subtree time limit per test2 seconds memory limit per test... A tree is a connected undirected graph with n−1 edges. Each vertex v of this tree has a color assigned to it (av=1 if the

    Codeforces 626 D. Jerry’s Protest(概率DP)

    codeforces每日一练。 题意: 有n张卡片,卡片上的数字就是分数,比如说甲乙两人抽卡,三局两胜,一局得分高的胜,求在甲赢了两局的情况下乙赢了第三局且总分比甲高的概率。 思路: 数据1e3,很明显的On^2算法,所以...

    codeforces.dev:codeforces.dev

    codeforces.dev codeforces.dev 这是应用程序的项目模板。 它位于 。 要使用基于此模板创建一个新项目: npx degit sveltejs/template svelte-app cd svelte-app 请注意,您将需要安装 开始吧 安装依赖项... ...

    Codeforces 1305 D. Kuroni and the Celebration (交互题)

    题意: 给出 nnn 个点,n−1n-1n−1 条边,最多询问 n2\frac{n}{2}2n​ 次,每次询问 u,vu,vu,v,会给出 uvuvuv的最近公共祖先,求树的根。 ...操作就是一个删除叶子节点的过程。 AC代码: const int N = 1010;...

    Codeforces 1083 A. The Fair Nut and the Best Path(树形DP)

    codeforces每日一练。 题意: 给一棵树,每个点有一个点权,每条边有一个边权,求一条链使得点权和-边权和最大。 思路: 由于我没看清楚题意,以为是求联通子图的点权和-边权和最大,用link-cut-tree写换根,wa10了两...

    Codeforces比赛用模板.zip

    Codeforces是一个知名的在线编程竞赛平台,它吸引了众多程序员参与,以提升编程技能和解决实际问题的能力。"Codeforces比赛用模板.zip"是一个压缩包,包含了参赛者在Codeforces平台上进行比赛时可能会用到的源码模板...

    Codeforces 1333C. Eugene and an array(思维) /详解

    Codeforces Round #632 (Div. 2) C. Eugene and an array 题意: 求出一个数列中子区间满足 此区间的任意子区间之和 不为0的区间个数。 思路: 考虑用dp[x]dp[x]dp[x]记录前缀和为xxx的区间右端点。 那么这道题其实...

    Codeforces 1333 F. Kate and imperfection

    题意: 在集合 S=1,2,⋯,nS={1,2,⋯,n}S=1,2,⋯,n 中,对于每个正整数 kkk ,找出一个大小为 kkk 的子集,使得该子集中两两间最大公因数的最大值最小,求这个最小值。 我们考虑如何构造两两间最大公因数的最大值最小...

    Codeforces 721 C. Journey(拓扑排序+DP)

    codeforces每日一练。 题意: 给定n个点,m条有向边,以及k时间。求不超过k时间1-n最多能经过多少个点。 思路: 数据&lt;=5000,说明是个暴力dp。 那么可以用dp[i][j]维护从1到i点经过了j个点,然后初始化为inf,再设...

    Codeforces 1312 E. Array Shrinking (区间dp)

    E. Array Shrinking time limit per test2 seconds memory limit per test256 megabytes inputstandard input ...Choose a pair of two neighboring equal elements ai=ai+1 (if there is at least o

    CodeForces1230 F. Konrad and Company Evaluation (复杂度分析)

    如果a嘲讽b,b嘲讽c,那么(a,b,c)是一个三元组 问每次操作之后一共有多少对三元组 数据范围:n,m&lt;=1e5,q&lt;=1e5 三元组图例(未修改工资时): 图中三元组一共4组: 4-&gt;3-&gt;1 4-&gt;3-&gt;2 4-&gt;2-&gt;1 3-&gt;2-&gt;1 解法...

    简单个人聊天室

    【简单个人聊天室】是一个基于ASP.NET技术构建的在线交流平台,主要面向个人用户,提供了一个简易而实用的沟通环境。在这个项目中,我们将探讨ASP.NET的核心特性、Web应用程序的基本结构以及如何实现基本的实时聊天...

    Codeforces 1325 D. Ehab the Xorcist (思维/构造) /详解

    D. Ehab the Xorcist ...Given 2 integers u and v, find the shortest array such that bitwise-xor of its elements is u, and the sum of its elements is v. Input The only line contains 2 integers u and v

Global site tag (gtag.js) - Google Analytics