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 ...
随机推荐
- bitset常用用法&&简单题分析
Preface bitset,还是一个比较好用的STL,可以给一些题目做到神奇的常数优化(\(O(\frac{原来的复杂度}{机器的位数(32位or64位)})\)) 关于一些具体的函数等内容可以参考 ...
- SAAS云平台搭建札记: (一) 浅论SAAS多租户自助云服务平台的产品、服务和订单
最近在做一个多租户的云SAAS软件自助服务平台,途中遇到很多问题,我会将一些心得.体会逐渐分享出来,和大家一起探讨.这是本系列的第一篇文章. 大家知道,要做一个全自助服务的SAAS云平台是比较复杂的, ...
- python基础学习笔记(十一)
迭代器 本节进行迭代器的讨论.只讨论一个特殊方法---- __iter__ ,这个方法是迭代器规则的基础. 迭代器规则 迭代的意思是重复做一些事很多次---就像在循环中做的那样.__iter__ 方 ...
- 牛客训练赛25-A-因数个数
题目链接https://www.nowcoder.com/acm/contest/158/A 无语...这题很迷啊,原谅我的菜,刚开始想用预处理欧拉筛和前缀和,可是这题太血崩了,这样一样要遍历,1-e ...
- Ubuntu14.04安装PyMuPDF
最近写的一个东西需要将pdf转成图片然后放在网页上展示,找到了个非常好用的轮子叫做PyMuPDF,在windows上测试的时候跑的666,在ubuntu上安装依赖的时候,简直万脸懵逼.github上给 ...
- 第六周分析Linux内核创建一个新进程的过程
潘恒 原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 task_struct结构: ...
- 第一阶段,第二阶段,第三阶段团队github更新项目地址
第一阶段:https://github.com/yuhancheng/stage-1--last-sprint 第二阶段:https://github.com/yuhancheng/stage-2-- ...
- C#获取当月第一天和最后一天
当月第一天0时0分0秒: DateTime.Now.AddDays(1 - DateTime.Now.Day).Date 当月最后一天23时59分59秒: DateTime.Now.AddDays(1 ...
- org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unexpected failure during bean definition parsing Offending resource: class path resource [applicationC
这个错误是 org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration proble ...
- Delphi的idhttp报IOHandler value is not valid错误的原因[转]
出现这种问题的原因是由于访问的 URL地址为https或存在其跳转地址为https. 首先单纯使用idhttp是只能访问http,而https则需要搭配IdSSLIOHandlerSocketOpen ...