Codeforces 372B Counting Rectangles is Fun

http://codeforces.com/problemset/problem/372/B

题意:每次给出一个区间,求里面有多少个矩形

思路:预处理,sum[i][j][k][l]代表以k,l为右下角,左上角不超过i,j有多少矩形,然后询问的时候枚举右下角就可以了

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<iostream>
 6 int n,m,T;
 7 int sum[45][45][45][45],l[45][45];
 8 char s[2005];
 9 int read(){
10     int t=0,f=1;char ch=getchar();
11     while (ch<‘0‘||ch>‘9‘){if (ch==‘-‘) f=-1;ch=getchar();}
12     while (‘0‘<=ch&&ch<=‘9‘){t=t*10+ch-‘0‘;ch=getchar();}
13     return t*f;
14 }
15 void init(){
16     for (int i=1;i<=n;i++){
17         scanf("%s",s+1);
18         for (int j=1;j<=m;j++){
19          if (s[j]==‘1‘) continue;
20          if (s[j-1]==‘0‘) l[i][j]=l[i][j-1]+1;
21          else l[i][j]=1;
22         }
23     }
24     for (int i=1;i<=n;i++)
25      for (int j=1;j<=m;j++)
26       for (int k=i;k<=n;k++)
27        for (int L=j;L<=m;L++){
28             int tmp=1<<29;
29             int ss=0;
30             for (int x=k;x>=i;x--){
31                 int ll=std::min(L-j+1,l[x][L]);
32                 tmp=std::min(tmp,ll);
33                 ss+=tmp;
34             }
35             sum[i][j][k][L]=ss;
36        }
37 }
38 int main(){
39     n=read();m=read();T=read();
40     init();
41     while (T--){
42         int x1=read(),y1=read(),x2=read(),y2=read(),ans=0;
43         for (int i=x1;i<=x2;i++)
44          for (int j=y1;j<=y2;j++)
45           ans+=sum[x1][y1][i][j];
46         printf("%d\n",ans);
47     }
48     return 0;
49 }
时间: 2024-08-28 22:04:09

Codeforces 372B Counting Rectangles is Fun的相关文章

Codeforces 372B. Counting Rectangles is Fun【动态规划,暴力枚举】(lowbit()小用法)

题目大意: 给出一个由0,1构成的矩阵,询问(a,b)到(c,d)两个点之间的只含有0的矩形有多少个. 方法: 由于矩阵不大,最多40*40,而且询问量很大(10^5)由此我们考虑o(1)输出答案,首先用一个四维数组预处理出答案,最后直接输出即可. 令dp[a][b][c][d]为(a,b)到(c,d)两个点之间的只含有0的矩形的数量, 则递推的公式: dp[a][b][c][d]=dp[a][b][c][d-1]+dp[a][b][c-1][d]-dp[a][b][c-1][d-1] 每次计算

CF 372B Counting Rectangles is Fun [dp+数据维护]

题意,给出一个n行m列的矩阵 里面元素是0或者1 给出q个询问 a,b,c,d 求(a,b)到(c,d)有多少个由0组成的矩形 我们定义 即为求(a,b)到(c,d)有多少个由0组成的矩形 对一个矩形来说 dp[a][b][c][d]=dp[a][b][c][d-1]+dp[a][b][c-1][d]-dp[a][b][c-1][d-1]+包含右下角(当前点)的矩形; 重点就在包含右下角(当前点c,d)的矩形,如何计算这个 我们可以暴力扫描,需要nm的复杂度,乘上原有复杂度,,,已经会超过时限

Project Euler 85 :Counting rectangles 数长方形

Counting rectangles By counting carefully it can be seen that a rectangular grid measuring 3 by 2 contains eighteen rectangles: Although there exists no rectangular grid that contains exactly two million rectangles, find the area of the grid with the

ZOJ (狗狗)1426 Counting Rectangles(暴力)

Counting Rectangles Time Limit: 2 Seconds      Memory Limit: 65536 KB We are given a figure consisting of only horizontal and vertical line segments. Our goal is to count the number of all different rectangles formed by these segments. As an example,

UVA - 10574 Counting Rectangles

Description Problem H Counting Rectangles Input: Standard Input Output:Standard Output Time Limit: 3Seconds   Given n points on the XY plane, count how many regular rectanglesare formed. A rectangle is regular if and only if its sides are all paralle

UVA 10574 - Counting Rectangles(枚举+计数)

10574 - Counting Rectangles 题目链接 题意:给定一些点,求能够成几个矩形 思路:先把点按x排序,再按y排序,然后用O(n^2)的方法找出每条垂直x轴的边,保存这些边两点的y坐标y1, y2.之后把这些边按y1排序,再按y2排序,用O(n)的方法找出有几个连续的y1, y2都相等,那么这些边两两是能构成矩形的,为C2cnt种,然后累加起来就是答案 代码: #include <stdio.h> #include <string.h> #include <

Codeforces 893E - Counting Arrays

893E - Counting Arrays 思路:质因子分解. 对于每个质因子,假设它有k个,那么求把它分配到y个数上的方案数. 相当于把k个小球分配到y个盒子里的方案数. 这个问题可以用隔板法(插空法)解决,要把一段分成y段,需要y-1个隔板,那么有y-1+k个位置,选y-1个位置为隔板,剩下的都是小球,那么方案数为C(y-1+k,y-1). 如果全为正数,答案就是所有质因子方案数的积. 但是这道题目可以为负数,那么在这y个数里选偶数个变成负数 答案还要乘以C(y,0)+C(y,2)+C(y

UVA 10574 - Counting Rectangles 计数

Given n points on the XY plane, count how many regular rectangles are formed. A rectangle is regular if and only if its sides are all parallel to the axis.InputThe ?rst line contains the number of tests t (1 ≤ t ≤ 10). Each case contains a single lin

Codeforces 893E Counting Arrays:dp + 线性筛 + 分解质因数 + 组合数结论

题目链接:http://codeforces.com/problemset/problem/893/E 题意: 共q组数据(q <= 10^5),每组数据给定x,y(x,y <= 10^6). 问你有多少种长度为y,乘积为x的整数数列.(可以有负数) 题解: 首先考虑数列只有正整数的情况. 将x分解质因数:x = ∑ a[i]*p[i] 由于x较大,所以要先用线性筛求出素数,再枚举素数分解质因数. 那么一个乘积为x的数列可以看做,将x的所有∑ p[i]个质因子,分配到了y个位置上. 设f(i)