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组询问( ...
随机推荐
- [linux]安装code::blocks
1.安装基本编译环境 $sudo apt-get install build-essential $sudo apt-get install gdb 2.安装codeblock $sudo apt-g ...
- CALayer---iOS-Apple苹果官方文档翻译之CALayer
CHENYILONG Blog CALayer---iOS-Apple苹果官方文档翻译之CALayer CALayer /*技术博客http://www.cnblogs.com/ChenYilong/ ...
- NYOJ 328 完全覆盖 (找规律)
题目链接 描述 有一天小董子在玩一种游戏----用21或12的骨牌把mn的棋盘完全覆盖.但他感觉游戏过于简单,于是就随机生成了两个方块的位置(可能相同),标记一下,标记后的方块不用覆盖.还要注意小董子 ...
- let块级作用域
let是es6中新加的作用域,即块级作用域. var申明的变量要么全局,要么函数级,而let允许把变量的作用域限制在块级域中,这里的块级可以是()内,或{}内. 示例: code_1: "u ...
- VueJS ElementUI el-table 的 formatter 和 scope template 不能同时存在
暂时可以通过 在 scope template 中自己处理格式化解决 相关issue: 2548
- Mysql储存过程5: while
循环结构 while create procedure name() begin while 条件 do SQL语句 end while; end$ create procedure aa6() be ...
- php webshell常见函数
0x1 直接在字符串变量后面加括号, 会调用这个函数: <?php $s = 'system'; $e = 'assert'; $s('whoami'); $e('phpinfo();'); 0 ...
- FPGA与CPLD的概念及其区别
一.FPGA与CPLD的基本概念 1.CPLD CPLD主要是由可编程逻辑宏单元(LMC,Logic Macro Cell)围绕中心的可编程互连矩阵单元组成,其中LMC逻辑结构较复杂,并具有复杂的I/ ...
- Python下urllib2应用
#coding=utf-8 import urllib,urllib2 url = 'http://www.xxx.com' values = {'wd' : 'python', 'language' ...
- hdu 5833(欧拉路)
The Best Path Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...