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组询问( ...
随机推荐
- 如何写出高性能SQL语句
优化SQL查询:如何写出高性能SQL语句 1.首先要搞明白什么叫执行计划? 执行计划是数据库根据SQL语句和相关表的统计信息作出的一个查询方案,这个方案是由查询优化器自动分析产生欀如一条SQL语句如果 ...
- [HDU1205]吃糖果 题解(鸽巢原理)
[HDU1205]吃糖果 Description -HOHO,终于从Speakless手上赢走了所有的糖果,是Gardon吃糖果时有个特殊的癖好,就是不喜欢将一样的糖果放在一起吃,喜欢先吃一种,下一次 ...
- python3爬虫.2.伪装浏览器
有的网页在爬取时候会报错返回 urllib.error.HTTPError: HTTP Error 403: Forbidden 这是网址在检测连接对象,所以需要伪装浏览器,设置User Agent ...
- Openflow Plugin学习笔记2
OpenDaylight OpenFlow Plugin 过载保护 过载保护 OF Plugin中的过载保护按如下流程工作: ConnectionConductor将消息送入队列,是最靠近OFJava ...
- 安装node-sass的正确姿势【转】
安装 node-sass 的时候总是会各种不成功,今天我琢磨了一会儿总算知道要怎么解决了. 首先要知道的是,安装 node-sass 时在 node scripts/install 阶段会从 gith ...
- [Leetcode] N-Queens 系列
N-Queens 系列题解 题目来源: N-Queens N-Queens II N-Queens The n-queens puzzle is the problem of placing n qu ...
- Hashtable之Properties
properties的使用:1.Hashtable的实现类,线程安全.与HashMap不同,Hashtable不允许使用null作为key和value2.和HashMap一样,Hashtable也不能 ...
- PlantUML——3.Graphviz的安装
官网:http://www.graphviz.org/Home.php 由于plantuml使用Graphviz来生成相关图形(只有序列图可以不依赖它),其它图形都需要, 因此得安装它,否则生成图 ...
- Delphi 绘图对象
来自:http://blog.csdn.net/lailai186/article/details/8755430 ========================================== ...
- 深度学习方法(九):自然语言处理中的Attention Model注意力模型
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.NET/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术感兴趣的同学加入. 上一篇博文深度学习方法(八):Enc ...