BZOJ3235 [Ahoi2013]好方的蛇 【单调栈 + dp】
题目链接
题解
求出每个点为顶点,分别求出左上,左下,右上,右下的矩形的个数\(g[i][j]\)
并预处理出\(f[i][j]\)表示点\((i,j)\)到四个角的矩形内合法矩形个数
就可以容斥计数啦
枚举顶点\((i,j)\),乘上另一侧矩形个数,如图:

但是会算重,对于这样的情况

减去即可
求\(g[i][j]\)数组,枚举每一行,使用单调栈即可
复杂度\(O(n^2)\)
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define cls(s,v) memset(s,v,sizeof(s))
#define mp(a,b) make_pair<int,int>(a,b)
#define cp pair<int,int>
using namespace std;
const int maxn = 1005,maxm = 100005,INF = 0x3f3f3f3f,P = 10007;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = 0; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 1) + (out << 3) + c - 48; c = getchar();}
return flag ? out : -out;
}
int f[maxn][maxn][4],g[maxn][maxn][4],n;
int S[maxn][maxn],d[maxn][maxn][2];
int len[maxn],h[maxn],top,tot;
void Pre(){
for (int j = 1; j <= n; j++){
for (int i = 1; i <= n; i++){
if (!S[i][j]) continue;
d[i][j][0] = d[i - 1][j][0] + 1;
}
}
for (int j = 1; j <= n; j++){
for (int i = n; i; i--){
if (!S[i][j]) continue;
d[i][j][1] = d[i + 1][j][1] + 1;
}
}
for (int k = 0; k <= 1; k++){
for (int i = 1; i <= n; i++){
top = 0; tot = 0;
for (int j = 1; j <= n; j++){
if (!S[i][j]){
top = 0; tot = 0;
continue;
}
int hh = d[i][j][k],L = 1;
while (top && h[top] >= hh)
tot = ((tot - h[top] * len[top] % P) + P) % P,L += len[top--];
h[++top] = hh; len[top] = L; tot = (tot + hh * L) % P;
g[i][j][k] = (tot - 1) % P;
}
}
}
for (int k = 0; k <= 1; k++){
for (int i = 1; i <= n; i++){
top = 0; tot = 0;
for (int j = n; j; j--){
if (!S[i][j]){
top = 0; tot = 0;
continue;
}
int hh = d[i][j][k],L = 1;
while (top && h[top] >= hh)
tot = ((tot - h[top] * len[top] % P) + P) % P,L += len[top--];
h[++top] = hh; len[top] = L; tot = (tot + hh * L) % P;
g[i][j][k + 2] = (tot - 1) % P;
}
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
f[i][j][0] = (f[i - 1][j][0] + f[i][j - 1][0] - f[i - 1][j - 1][0] + g[i][j][0]) % P;
for (int i = n; i; i--)
for (int j = 1; j <= n; j++)
f[i][j][1] = (f[i + 1][j][1] + f[i][j - 1][1] - f[i + 1][j - 1][1] + g[i][j][1]) % P;
for (int i = 1; i <= n; i++)
for (int j = n; j; j--)
f[i][j][2] = (f[i - 1][j][2] + f[i][j + 1][2] - f[i - 1][j + 1][2] + g[i][j][2]) % P;
for (int i = n; i; i--)
for (int j = n; j; j--)
f[i][j][3] = (f[i + 1][j][3] + f[i][j + 1][3] - f[i + 1][j + 1][3] + g[i][j][3]) % P;
}
void work(){
int ans = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
ans = (ans + (f[1][j + 1][3] + f[i + 1][1][3] - f[i + 1][j + 1][3]) * g[i][j][0] % P) % P;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
ans = (ans + P - g[i][j][1] * f[i - 1][j + 1][2] % P) % P;
printf("%d\n",(ans + P) % P);
}
int main(){
n = read();
REP(i,n){
char c = getchar(); while (c != 'B' && c != 'W') c = getchar();
REP(j,n) {S[i][j] = c == 'B' ? 1 : 0; c = getchar();}
}
Pre();
work();
return 0;
}
BZOJ3235 [Ahoi2013]好方的蛇 【单调栈 + dp】的更多相关文章
- 【BZOJ 3235】 3235: [Ahoi2013]好方的蛇 (单调栈+容斥原理)
3235: [Ahoi2013]好方的蛇 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 187 Solved: 95 Description 有一天, ...
- 3235: [Ahoi2013]好方的蛇
3235: [Ahoi2013]好方的蛇 链接 分析: 可以求出以每个点为顶点的满足条件的矩形有多少个,单调栈求.设为sum. 然后对这个数组进行二维前缀和,可以求出每个矩阵内,以右下角.左下角为端点 ...
- BZOJ 3235: [Ahoi2013]好方的蛇
BZOJ 3235: [Ahoi2013]好方的蛇 标签(空格分隔): OI-BZOJ OI-DP OI-容斥原理 Time Limit: 10 Sec Memory Limit: 64 MB Des ...
- BZOJ_3238_[Ahoi2013]差异_后缀数组+单调栈
BZOJ_3238_[Ahoi2013]差异_后缀数组+单调栈 Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao ...
- 洛谷 P4697 Balloons [CEOI2011] 单调栈/dp (待补充qwq)
正解:单调栈/dp 解题报告: 先放个传送门qwq 话说这题是放在了dp的题单里呢?但是听说好像用单调栈就可以做掉所以我就落实下单调栈的解法好了qwq (umm主要如果dp做好像是要斜率优化凸壳维护双 ...
- BZOJ3238 [Ahoi2013]差异 【后缀数组 + 单调栈】
题目链接 BZOJ3238 题解 简单题 经典后缀数组 + 单调栈套路,求所有后缀\(lcp\) #include<iostream> #include<cstdio> #in ...
- Discrete Centrifugal Jumps CodeForces - 1407D 单调栈+dp
题意: 给你n个数hi,你刚开始在第1个数的位置,你需要跳到第n个数的位置. 1.对于i.j(i<j) 如果满足 max(hi+1,-,hj−1)<min(hi,hj) max(hi,hj ...
- Codeforces 1383E - Strange Operation(线段树优化 DP or 单调栈+DP)
Codeforces 题目传送门 & 洛谷题目传送门 Yet another 自己搞出来的难度 \(\ge 2800\) 的题 介绍一个奇奇怪怪的 \(n\log n\) 的做法.首先特判掉字 ...
- bzoj4709 柠檬 单调栈,DP,斜率优化
目录 前言吐槽 思路 错误 代码 /* 前言吐槽 我真的不知道是咋做的 不过大约就是栈的斜率优化 哪位大佬见识广,给看看吧(乞讨) 思路 s是值等于a[i]的前缀和 转移方程$f[i]=max(f[i ...
随机推荐
- Dubbo(四) Dubbo-Admin项目 Dubbo管理台
前言 在dubbo项目中,有注册中心,消费者,提供者就足以构成一个完整的项目了.但是仅仅有这三个角色,很难对整个项目状态有直观的了解,以及对项目操作. 因此早有前辈对此原因作出了贡献——一个通用的du ...
- Nginx+upstream针对后端服务器容错的运维笔记
熟练掌握Nginx负载均衡的使用对运维人员来说是极其重要的!下面针对Nignx负载均衡upstream容错机制的使用做一梳理性说明: 一.nginx的upstream容错 1)nginx 判断节点失效 ...
- VMware vSphere虚拟化-VMware ESXi 5.5组件安装过程记录
几种主要的虚拟化 ESXi是VMware公司研发的虚拟机服务器,ESXi已经实现了与Virtual Appliance Marketplace的直接整合,使用户能够即刻下载并运行虚拟设备.这为 即插即 ...
- db2修改最大连接数
查看当前连接数,sample为数据库名db2 list applications for db sample db2 list applications for db sample show deta ...
- 作用域&作用域链和with,catch语句&闭包
作用域(函数) 作用域:变量与函数的可访问范围,即作用域控制着变量与函数的可见性和生命周期; 在一些类C编程语言中花括号内的每一段代码都有各自的作用域,而且变量在声明它们的代码段外是不可见的,称之为块 ...
- Github上传更新
通过2天的时间,不停的网上找各种资料,今天下午终于可以登录上github for Windows 客户端了,,, 然后通过一整晚的摸索,也把项目上传到github里. github地址:https:/ ...
- Spring MVC静态资源处理(转)
原文地址: http://www.cnblogs.com/fangqi/archive/2012/10/28/2743108.html 优雅REST风格的资源URL不希望带 .html 或 .do 等 ...
- php 文件上传 $_FILES 错误码
假设文件上传字段的名称file_name,则: $_FILES['file_name']['error']有以下几种类型 1.UPLOAD_ERR_OK 其值为 0,没有错误发生,文件上传成功. 2. ...
- Oracle备份恢复简单过程以及中间的坑.
Oracle 冷备: 貌似需要dbca创建一致的oracle instance 服务器配置版本尽量相同,安装路径相同. 关闭Oracle服务 将oracle app 目录下的oradata以及有快速闪 ...
- PP模块的主要功能及标准业务流程
主要功能:1.SOP (Sales and operations Planning).2.资源分配计划划 (Distribution Resource Planning)3.生产计划编制 (Produ ...