题目链接

BZOJ3235

题解

求出每个点为顶点,分别求出左上,左下,右上,右下的矩形的个数\(g[i][j]\)

并预处理出\(f[i][j]\)表示点\((i,j)\)到四个角的矩形内合法矩形个数

就可以容斥计数啦

枚举顶点\((i,j)\),乘上另一侧矩形个数,如图:



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



减去即可

求\(g[i][j]\)数组,枚举每一行,使用单调栈即可

复杂度\(O(n^2)\)

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<cstdio>
  6. #include<vector>
  7. #include<queue>
  8. #include<cmath>
  9. #include<map>
  10. #define LL long long int
  11. #define REP(i,n) for (int i = 1; i <= (n); i++)
  12. #define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
  13. #define cls(s,v) memset(s,v,sizeof(s))
  14. #define mp(a,b) make_pair<int,int>(a,b)
  15. #define cp pair<int,int>
  16. using namespace std;
  17. const int maxn = 1005,maxm = 100005,INF = 0x3f3f3f3f,P = 10007;
  18. inline int read(){
  19. int out = 0,flag = 1; char c = getchar();
  20. while (c < 48 || c > 57){if (c == '-') flag = 0; c = getchar();}
  21. while (c >= 48 && c <= 57){out = (out << 1) + (out << 3) + c - 48; c = getchar();}
  22. return flag ? out : -out;
  23. }
  24. int f[maxn][maxn][4],g[maxn][maxn][4],n;
  25. int S[maxn][maxn],d[maxn][maxn][2];
  26. int len[maxn],h[maxn],top,tot;
  27. void Pre(){
  28. for (int j = 1; j <= n; j++){
  29. for (int i = 1; i <= n; i++){
  30. if (!S[i][j]) continue;
  31. d[i][j][0] = d[i - 1][j][0] + 1;
  32. }
  33. }
  34. for (int j = 1; j <= n; j++){
  35. for (int i = n; i; i--){
  36. if (!S[i][j]) continue;
  37. d[i][j][1] = d[i + 1][j][1] + 1;
  38. }
  39. }
  40. for (int k = 0; k <= 1; k++){
  41. for (int i = 1; i <= n; i++){
  42. top = 0; tot = 0;
  43. for (int j = 1; j <= n; j++){
  44. if (!S[i][j]){
  45. top = 0; tot = 0;
  46. continue;
  47. }
  48. int hh = d[i][j][k],L = 1;
  49. while (top && h[top] >= hh)
  50. tot = ((tot - h[top] * len[top] % P) + P) % P,L += len[top--];
  51. h[++top] = hh; len[top] = L; tot = (tot + hh * L) % P;
  52. g[i][j][k] = (tot - 1) % P;
  53. }
  54. }
  55. }
  56. for (int k = 0; k <= 1; k++){
  57. for (int i = 1; i <= n; i++){
  58. top = 0; tot = 0;
  59. for (int j = n; j; j--){
  60. if (!S[i][j]){
  61. top = 0; tot = 0;
  62. continue;
  63. }
  64. int hh = d[i][j][k],L = 1;
  65. while (top && h[top] >= hh)
  66. tot = ((tot - h[top] * len[top] % P) + P) % P,L += len[top--];
  67. h[++top] = hh; len[top] = L; tot = (tot + hh * L) % P;
  68. g[i][j][k + 2] = (tot - 1) % P;
  69. }
  70. }
  71. }
  72. for (int i = 1; i <= n; i++)
  73. for (int j = 1; j <= n; j++)
  74. 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;
  75. for (int i = n; i; i--)
  76. for (int j = 1; j <= n; j++)
  77. 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;
  78. for (int i = 1; i <= n; i++)
  79. for (int j = n; j; j--)
  80. 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;
  81. for (int i = n; i; i--)
  82. for (int j = n; j; j--)
  83. 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;
  84. }
  85. void work(){
  86. int ans = 0;
  87. for (int i = 1; i <= n; i++)
  88. for (int j = 1; j <= n; j++)
  89. ans = (ans + (f[1][j + 1][3] + f[i + 1][1][3] - f[i + 1][j + 1][3]) * g[i][j][0] % P) % P;
  90. for (int i = 1; i <= n; i++)
  91. for (int j = 1; j <= n; j++)
  92. ans = (ans + P - g[i][j][1] * f[i - 1][j + 1][2] % P) % P;
  93. printf("%d\n",(ans + P) % P);
  94. }
  95. int main(){
  96. n = read();
  97. REP(i,n){
  98. char c = getchar(); while (c != 'B' && c != 'W') c = getchar();
  99. REP(j,n) {S[i][j] = c == 'B' ? 1 : 0; c = getchar();}
  100. }
  101. Pre();
  102. work();
  103. return 0;
  104. }

BZOJ3235 [Ahoi2013]好方的蛇 【单调栈 + dp】的更多相关文章

  1. 【BZOJ 3235】 3235: [Ahoi2013]好方的蛇 (单调栈+容斥原理)

    3235: [Ahoi2013]好方的蛇 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 187  Solved: 95 Description 有一天, ...

  2. 3235: [Ahoi2013]好方的蛇

    3235: [Ahoi2013]好方的蛇 链接 分析: 可以求出以每个点为顶点的满足条件的矩形有多少个,单调栈求.设为sum. 然后对这个数组进行二维前缀和,可以求出每个矩阵内,以右下角.左下角为端点 ...

  3. BZOJ 3235: [Ahoi2013]好方的蛇

    BZOJ 3235: [Ahoi2013]好方的蛇 标签(空格分隔): OI-BZOJ OI-DP OI-容斥原理 Time Limit: 10 Sec Memory Limit: 64 MB Des ...

  4. BZOJ_3238_[Ahoi2013]差异_后缀数组+单调栈

    BZOJ_3238_[Ahoi2013]差异_后缀数组+单调栈 Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao ...

  5. 洛谷 P4697 Balloons [CEOI2011] 单调栈/dp (待补充qwq)

    正解:单调栈/dp 解题报告: 先放个传送门qwq 话说这题是放在了dp的题单里呢?但是听说好像用单调栈就可以做掉所以我就落实下单调栈的解法好了qwq (umm主要如果dp做好像是要斜率优化凸壳维护双 ...

  6. BZOJ3238 [Ahoi2013]差异 【后缀数组 + 单调栈】

    题目链接 BZOJ3238 题解 简单题 经典后缀数组 + 单调栈套路,求所有后缀\(lcp\) #include<iostream> #include<cstdio> #in ...

  7. 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 ...

  8. Codeforces 1383E - Strange Operation(线段树优化 DP or 单调栈+DP)

    Codeforces 题目传送门 & 洛谷题目传送门 Yet another 自己搞出来的难度 \(\ge 2800\) 的题 介绍一个奇奇怪怪的 \(n\log n\) 的做法.首先特判掉字 ...

  9. bzoj4709 柠檬 单调栈,DP,斜率优化

    目录 前言吐槽 思路 错误 代码 /* 前言吐槽 我真的不知道是咋做的 不过大约就是栈的斜率优化 哪位大佬见识广,给看看吧(乞讨) 思路 s是值等于a[i]的前缀和 转移方程$f[i]=max(f[i ...

随机推荐

  1. bat基础知识

    1.打日志:使用重定向 eg:call test.bat>log/test.log 2.不关闭cmd窗口:使用pause eg: 结果: ps:注意,在自动化运维的时候,比如创建自动发版的脚本的 ...

  2. Robot Framework的日期处理

    http://www.cnblogs.com/channy14/p/6160831.html http://blog.csdn.net/r455678/article/details/52993765

  3. [Oracle]数据库的Control File 取Dump后的样例

    [Oracle]数据库的Control File 取Dump后的样例: 片段截取-------------------------------(size = 40, compat size = 40, ...

  4. 开源数据同步神器——canal

    前言 如今大型的IT系统中,都会使用分布式的方式,同时会有非常多的中间件,如redis.消息队列.大数据存储等,但是实际核心的数据存储依然是存储在数据库,作为使用最广泛的数据库,如何将mysql的数据 ...

  5. jQuery中.html(“xxx”)和.append("xxx") 的区别

    append是追加,html是完全替换比如<p id="1"><p>123</p></p> $("#1").ht ...

  6. 支持自定义协议的虚拟仪器【winform版】

    首先,这个程序的由来,额,工作以来,做的最久的就是上位机,对市面上的大部分组态软件都感到不满,不好用,LabView虽然用起来不错,但是入门还是不够简单,刚好现在工作比较闲(已经不再做上位机了),所以 ...

  7. 轮廓(Outline) 实例

    1.在元素周围画线本例演示使用outline属性在元素周围画一条线. <style type="text/css"> p{border:red solid thin;o ...

  8. Mycat读写分离、主从切换、分库分表的操作记录

    系统开发中,数据库是非常重要的一个点.除了程序的本身的优化,如:SQL语句优化.代码优化,数据库的处理本身优化也是非常重要的.主从.热备.分表分库等都是系统发展迟早会遇到的技术问题问题.Mycat是一 ...

  9. StackOverflow 问题

    StackOverflow  这个问题一般是你的程序里头可能是有死循环或递归调用所产生的:可以查看一下你的程序,也可以增大你JVM的内存~~~在Eclipse中JDK的配置中加上   -XX:MaxD ...

  10. THE First Individual Project - Word frequency program

    第一次写博客,这次也是本学期写到第一个程序. 老师要求网址:http://www.cnblogs.com/jiel/p/3311400.html#2777556 一.项目预计时间 一开始想使用不熟悉的 ...