Codeforces 372 B. Counting Rectangles is Fun
$ >Codeforces \space 372 B. \ Counting\ Rectangles\ is\ Fun<$
题目大意 :
给出一个 \(n \times m\) 的 \(01\) 矩阵,有 \(q\) 次询问,每次给出一个矩形 $ x_1, x_2, y_1, y_2$ ,求有多这个矩形的有多少个全 \(0\) 子矩形\(1 \leq n, m \leq 50, 1 \leq q \leq 3 \times 10^5\)
解题思路 :
设 \(g(x, y, l, r)\) 表示以 \(x\) 为底,满足上边界 \(\leq y\) , 左边界 \(\geq l\) ,右边界 \(\leq r\) 的矩形的数量
那么对于一组询问 \(x_1, x_2, y_1, y_2\),\(Ans = \sum_{i = x_1}^{x_2} g(x_2, x_1, y_1, y_2)\)
所以直接考虑怎么求 \(g(x, y, l, r)\) 即可,问题转化为上下左右边界已经框好,求有多少个贴着下边界的全 \(0\) 子矩形
考虑维护一条从左边界到右边界的扫描线,每次计算当前的右端点向左延伸形成的子矩形的个数
考虑对于当前右端点 \(r\) 有一个左端点 \(l\), 设 \(len(x, y)\) 表示从点 \((x, y)\) 向上能延伸的 \(0\) 的个数
那么 \(l, r\) 作为左右端点能形成的全 \(0\) 子矩形的个数就是 \(\min(len(x, j))\ \ l \leq j \leq r\)
观察发现,对于每一个区间的答案其实就是这个区间 \(len\) 的最小值,直接枚举左端点并用单调栈维护即可
此时求 \(g\) 的复杂度是 \(O(n^5)\) 级别,由于 \(n, m\) 很小只有 \(40\) 所以可以接受,之后的询问可以直接预处理答案,总复杂度是 \(O(n^5 + q)\)
```cpp
/*program by mangoyang*/
#include
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a)
inline void read(T &x){
int f = 0, ch = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
#define N (45)
#define x1 xx1
#define y1 yy1
#define x2 xx2
#define y2 yy2
char s[N][N];
int f[N][N][N][N], g[N][N], xx[N], ff[N], n, m, q, top;
int main(){
read(n), read(m), read(q);
for(int i = 1; i = 1; k--, tot++)
if(s[k][j] == '1') break;
g[i][j] = tot;
}
for(int x1 = 1; x1 = 1; i--) res += (xx[i] - xx[i-1]) * ff[i];
f[x1][y1][x2][y2] = res;
}
}
for(int x1 = 1; x1 x1) f[x1][y1][x2][y2] += f[x1][y1][x2-1][y2];
for(int i = 1, x1, x2, y1, y2; i
</font>
Codeforces 372 B. Counting Rectangles is Fun的更多相关文章
- CodeForces 372 A. Counting Kangaroos is Fun
题意,有n只袋鼠,没每只袋鼠有个袋子,大小为si,一个袋鼠可以进入另外一个袋鼠的袋子里面,当且仅当另一个袋鼠的袋子是他的二倍或二倍一上,然后中国袋鼠就是不可见的,不能出现多个袋鼠嵌套的情况.让你求最少 ...
- Codeforces Round #219 (Div. 2) D. Counting Rectangles is Fun 四维前缀和
D. Counting Rectangles is Fun time limit per test 4 seconds memory limit per test 256 megabytes inpu ...
- Counting Rectangles
Counting Rectangles Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1043 Accepted: 546 De ...
- Project Euler 85 :Counting rectangles 数长方形
Counting rectangles By counting carefully it can be seen that a rectangular grid measuring 3 by 2 co ...
- UVA - 10574 Counting Rectangles
Description Problem H Counting Rectangles Input: Standard Input Output:Standard Output Time Limit: 3 ...
- UVA 10574 - Counting Rectangles(枚举+计数)
10574 - Counting Rectangles 题目链接 题意:给定一些点,求可以成几个边平行于坐标轴的矩形 思路:先把点按x排序,再按y排序.然后用O(n^2)的方法找出每条垂直x轴的边,保 ...
- Codeforces 954H Path Counting 【DP计数】*
Codeforces 954H Path Counting LINK 题目大意:给你一棵n层的树,第i层的每个节点有a[i]个儿子节点,然后问你树上的简单路径中长度在1~n*2-2之间的每个有多少条 ...
- codeforces 372 Complete the Word(双指针)
codeforces 372 Complete the Word(双指针) 题链 题意:给出一个字符串,其中'?'代表这个字符是可变的,要求一个连续的26位长的串,其中每个字母都只出现一次 #incl ...
- Codeforces 372B Counting Rectangles is Fun:dp套dp
题目链接:http://codeforces.com/problemset/problem/372/B 题意: 给你一个n*m的01矩阵(1 <= n,m <= 40). 然后有t组询问( ...
随机推荐
- 数据库-SQLite
技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilong 数据库-SQLite 技术博客http:// ...
- 郑轻校赛 2127 tmk射气球 (数学)
Description 有一天TMK在做一个飞艇环游世界,突然他发现有一个气球匀速沿直线飘过,tmk想起了他飞艇上有一把弓,他打算拿弓去射气球,为了提高射击的准确性,他首先在飞艇上找到一个离气球最近的 ...
- pythonif语句和循环语句
1.if语句用法 # if语句用法(缩进相同的成为一个代码块) score=90 if score>=60: print("合格") print("OK" ...
- struts获得参数(属性,对象,模型驱动)
0. strutsMVC
- django框架<二>
django框架: Models 1.基本创建 Django提供了一个抽象层("Model")的构建和管理Web应用程序的数据. Django使用一种新的方式,即:关系对象映射 ...
- 安全测试===dos攻击和ddos攻击
Dos攻击: dos攻击是Denial of Service的简称,即拒绝服务,造成DoS的攻击行为被称为DoS攻击,其目的是使计算机或网络无法提供正常的服务 DoS攻击是指故意的攻击网络协议实现的缺 ...
- javascript 之数据类型--01
写在前面 国庆整理资料时,发现刚开始入门前端时学习JS 的资料,打算以一个基础入门博客记录下来,有不写不对的多多指教: 先推荐些书籍给需要的童鞋 <JavaScript 高级程序设计.pdf&g ...
- php 学习try_catch测试抛出异常
/** * Class show * 一个catch接收抛出异常 */ class show { // 错误的演示 //try { //require ('test_try_catch.php'); ...
- cin关闭同步[转]
很多人会说cin的速度比scanf慢很多, 其实不然. cin慢的原因主要在于默认cin与stdin总是保持同步, 这一步是消耗时间大户. 只需要加上ios::sync_with_stdio(fals ...
- 结构体对齐及#pragma详细解释
在linux下c语言结构体对齐: 1.自然对齐 struct 是一种复合数据类型,其构成元素既可以是基本数据类型(如int.long.float 等)的变量,也可以是一些复合数据类型(如array.s ...