Description

Little Bob is a famous builder. He bought land and wants to build a house. Unfortunately, the problem is the

land’s terrain, it has a variable elevation.

The land is shaped like a rectangle, N meters wide and M meters long. It can be divided into N*M squares

(see the image). Bob’s house will be shaped like a rectangle that has sides parallel with the land’s edges and

its vertices coincide with the vertices of the squares. All the land covered by Bob’s house must be of equal

elevation to prevent it from collapsing.

Calculate the number of ways Bob can build his house!

Input

The first line of input contains integers N and M (1 <= N, M <= 1000).

Each of the following N lines contains M integers aij (1 <= aij <= 1000000000), respectively the height of each square of

land.

Warning: Please use faster input methods beacuse the amount of input is very large. (For example, use scanf

instead of cin in C++ or BufferedReader instead of Scanner in Java.)

Output

The first and only line of output must contain the required number from the task statement.

Sample Input


5 3
2 2 2
2 2 1
1 1 1
2 1 2
1 2 1

Sample Output


27

Hint

Clarification of the first example: Some of the possible house locations are rectangles with opposite vertices in (0,0)-

(1,1), (0,0)-(0,2) (height 2) i (2,0)-(2,2), (1,2)-(2,2) (height 1). The first number in the brackets represents the row number

and the second one the column number (0-indexed).

题意:给你一个n*m的矩阵,每一个格子都有自己的价值,让你在矩阵中找一些格子,使得矩形里的格子的价值都相同,问最多能找到的矩形数。

思路:如果是普通暴力的话,那么时间复杂度为O(n*m*m)爆了,所以要找其他方法。我们可以用单调队列做(单调栈也可以做的,其实它们两者的本质是一样的),这题所要维护的东西和之前求一个平面内的最大矩形面积不同,这里求的是最多的矩形个数,我们可以先把二维矩阵转化为一维,即对于每一行的格子,把上面和它价值相同的格子数作为它的高度。那么这个问题就转化成了有许多长方形格子放在地上,让你找到包含矩形底的矩形总个数。用单调队列的时候要维护三个值,q[][0]表示高度,q[][1]表示以它为右下角格子的矩形个数,q[][2]表示坐标,维护一个严格递增单调队列就行了。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 1005
int gra[maxn][maxn],a[maxn][maxn];
int q[1111111][3]; int main()
{
int n,m,i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
scanf("%d",&gra[i][j]);
a[i][j]=1;
if(i>1 && gra[i][j]==gra[i-1][j]){
a[i][j]+=a[i-1][j];
}
}
}
ll cnt=0;
int front,rear,qidian;
for(i=1;i<=n;i++){
gra[i][0]=-1;
for(j=1;j<=m;j++){
if(gra[i][j]==gra[i][j-1]){
while(front<=rear && q[rear][0]>=a[i][j]){
rear--;
}
rear++;
q[rear][0]=a[i][j];q[rear][2]=j;
if(rear==1){
q[rear][1]=a[i][j]*(j-qidian); //这里相当于底*高
}
else{
q[rear][1]=a[i][j]*(j-q[rear-1][2])+q[rear-1][1] ; //这里要加上队列里前一个高度比它低的矩形算出来的符合要求的矩形
}
cnt+=q[rear][1];
}
else{
front=1;rear=1;
qidian=j-1;
q[rear][0]=a[i][j];
q[rear][1]=a[i][j];
q[rear][2]=j;
cnt+=a[i][j];
}
}
}
printf("%lld\n",cnt);
}
return 0;
}

zjnu1735BOB (单调队列,单调栈)的更多相关文章

  1. 单调队列 && 单调栈

    单调队列 && 单调栈 单调队列 维护某个滑动区间的min or max,可用于dp的优化 以维护min为例,采用STL双端队列实现 每次加入元素x前 先检查队首元素==滑动后要删除的 ...

  2. 联赛模拟测试18 A. 施工 单调队列(栈)优化DP

    题目描述 分析 对于 \(Subtask\ 1\),可以写一个 \(n^3\) 的 \(DP\),\(f[i][j]\) 代表第 \(i\) 个建筑高度为 \(j\) 时的最小花费,随便转移即可 时间 ...

  3. 数据结构录 之 单调队列&单调栈。

    队列和栈是很常见的应用,大部分算法中都能见到他们的影子. 而单纯的队列和栈经常不能满足需求,所以需要一些很神奇的队列和栈的扩展. 其中最出名的应该是优先队列吧我觉得,然后还有两种比较小众的扩展就是单调 ...

  4. 单调队列&单调栈

    单调队列 例题: Poj 2823给定一个数列,从左至右输出每个长度为m的数列段内的最小数和最大数.数列长度:N<=106,m<=N 对于单调队列,我们这样子来定义: 1.维护区间最值 2 ...

  5. 数据结构录 之 单调队列&单调栈。(转)

    http://www.cnblogs.com/whywhy/p/5066306.html 队列和栈是很常见的应用,大部分算法中都能见到他们的影子. 而单纯的队列和栈经常不能满足需求,所以需要一些很神奇 ...

  6. 大视野 1012: [JSOI2008]最大数maxnumber(线段树/ 树状数组/ 单调队列/ 单调栈/ rmq)

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 9851  Solved: 4318[Submi ...

  7. 小Z爱序列(NOIP信(sang)心(bin)赛)From FallDream(粗制单调队列&单调栈的算法解析)

    原题: 小Z最擅长解决序列问题啦,什么最长公共上升然后下降然后上升的子序列,小Z都是轻松解决的呢. 但是小Z不擅长出序列问题啊,所以它给了你一道签到题. 给定一个n个数的序列ai,你要求出满足下述条件 ...

  8. POJ 3494 Largest Submatrix of All 1’s 单调队列||单调栈

    POJ 3494 Largest Submatrix of All 1’s Description Given a m-by-n (0,1)-matrix, of all its submatrice ...

  9. 单调队列&单调栈归纳

    单调队列 求长度为M的区间内的最大(小)值 单调队列的基本操作,也就是经典的滑动窗口问题. 求长度为M的区间内最大值和最小值的最大差值 两个单调队列,求出长度为M的区间最大最小值的数组,分别求最大最小 ...

  10. 【NOIP数据结构专项】单调队列单调栈

    [FZYZ P1280 ][NOIP福建夏令营]矩形覆盖 Description 有N个矩形,矩形的底边边长为1,且均在X轴上,高度给出,第i个矩形的高为h[i],求最少需要几个矩形才能覆盖这个图形. ...

随机推荐

  1. 详解线程池的作用及Java中如何使用线程池

    服务端应用程序(如数据库和 Web 服务器)需要处理来自客户端的高并发.耗时较短的请求任务,所以频繁的创建处理这些请求的所需要的线程就是一个非常消耗资源的操作.常规的方法是针对一个新的请求创建一个新线 ...

  2. nginx文件结构与解析,例子

    1.nginx文件结构 1 ... #全局块 2 3 events { #events块 4 ... 5 } 6 7 http #http块 8 { 9 ... #http全局块 10 server ...

  3. 【JavaWeb】Servlet 程序

    Servlet 程序 Servlet Servlet 是在 Web 服务器中运行的小型 Java 程序.Servlet 通常通过 HTTP(超文本传输​​协议)接收和响应来自 Web 客户端的请求. ...

  4. 【MyBatis】MyBatis CRUD

    MyBtis CRUD 文章源码 基于代理 DAO 的 CRUD 根据 ID 查询操作 在持久层接口中添加 findById 方法: public interface UserDAO { /** * ...

  5. Linux应急响应--入侵排查

    1.入侵者可能会删除机器的日志信息,可以查看日志信息是否还存在或者是否被清空,相关命令示例: ll -h /var/log/*  系统日志一般都存在/var/log下常用的系统日志如下:核心启动日志: ...

  6. awk -v参数

    -v var=val --assign var=val Assign the value val to the variable var, before execution of the progra ...

  7. AWD生存之道

    比赛开始阶段 常见漏洞的防御手段:https://www.freebuf.com/articles/web/208778.html 一.登陆SSH 重点 如果ssh的密码不是随机密码,记得一开始就进行 ...

  8. CMU数据库(15-445)Lab1-BufferPoolManager

    0. 关于环境搭建请看 https://www.cnblogs.com/JayL-zxl/p/14307260.html 1. Task1 LRU REPLACEMENT POLICY 0. 任务描述 ...

  9. mysqldumpslow基本使用

    参数解释 -s, 是表示按照何种方式排序 c: 访问计数 l: 锁定时间 r: 返回记录 t: 查询时间 al:平均锁定时间 ar:平均返回记录数 at:平均查询时间 -t, 是top n的意思,即为 ...

  10. eNSP启动设备AR1失败记一次解决步骤

    eNSP稳定版本下载:   微信搜索公众号"疯刘小三" 关注后回复ensp即可获得下载链接地址 eNSP V100R002C00B510 Setup.exe 最近在用eNSp的时候 ...