Gym - 101102D Rectangles (单调栈)
Given an R×C grid with each cell containing an integer, find the number of subrectangles in this grid that contain only one distinct integer; this means every cell in a subrectangle contains the same integer.
A subrectangle is defined by two cells: the top left cell (r1, c1), and the bottom-right cell (r2, c2) (1 ≤ r1 ≤ r2 ≤ R) (1 ≤ c1 ≤ c2 ≤ C), assuming that rows are numbered from top to bottom and columns are numbered from left to right.
Input
The first line of input contains a single integer T, the number of test cases.
The first line of each test case contains two integers R and C (1 ≤ R, C ≤ 1000), the number of rows and the number of columns of the grid, respectively.
Each of the next R lines contains C integers between 1 and 109, representing the values in the row.
Output
For each test case, print the answer on a single line.
Example
1
3 3
3 3 1
3 3 1
2 2 5
16 题意:问由单一数字组成的矩阵的个数。
思路:
dp[i][j]表示以位置i,j为右下角的矩阵个数。
先处理出当前位置向上延伸相同的数字最高有多高。用num[i]记录。
用单调栈处理出在此之前的,第一个小于自己的num[i].
判断中间有没有插入别的数,再进行处理。详见solve函数。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int maxm = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-);
int n,m;
int mp[maxn][maxn];
int num[maxn];
ll ans;
struct node{
int num,pos;
};
stack<node>st;
int pre[maxn];
int pre1[maxn];
ll dp[maxn];
void solve(int t){
memset(dp,,sizeof(dp));
memset(pre,,sizeof(pre));
while(!st.empty()){
st.pop();
}
num[]=-;
for(int i=m;i>=;i--){
while(!st.empty()&&st.top().num>num[i]){
pre[st.top().pos]=i;
st.pop();
}
st.push(node{num[i],i});
}
int k=;
for(int i=;i<=m;i++){
if(mp[t][i]!=mp[t][i-]){
k=i;
}
pre1[i]=k;
}
int pree;
for(int i=;i<=m;i++){
if(pre1[i]>pre[i]){
dp[i]=1ll*num[i]*(i-pre1[i]+);
ans+=dp[i];
}
else{
dp[i]=1ll*num[i]*(i-pre[i])+dp[pre[i]];
ans+=dp[i];
}
}
} int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
scanf("%d",&mp[i][j]);
}
}
ans=;
memset(num,,sizeof(num));
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mp[i][j]==mp[i-][j]){
num[j]++;
}
else{
num[j]=;
}
}
solve(i);
}
printf("%lld\n",ans);
}
return ;
}
Gym - 101102D Rectangles (单调栈)的更多相关文章
- D - Laying Cables Gym - 100971D (单调栈)
题目链接:https://cn.vjudge.net/problem/Gym-100971D 题目大意:给你n个城市的信息,每一个城市的信息包括坐标和人数,然后让你找每一个城市的父亲,作为一个城市的父 ...
- [Agc081F/At2699] Flip and Rectangles - 单调栈,结论
[Agc081F/At2699] 给出一个拥有 \(H\times W\) 个格子的棋盘,每个格子的颜色为黑色或白色. Snuke 可以进行任意次下列操作: 选择棋盘中的一行或一列,将这一行或一列的颜 ...
- Gym 101102D---Rectangles(单调栈)
题目链接 http://codeforces.com/gym/101102/problem/D problem description Given an R×C grid with each cel ...
- 单调队列 + 组合数统计 Gym 101102D
题目链接:http://codeforces.com/gym/101102/problem/D 题目大意:给你一个n*m的矩阵,矩阵里面的数值范围为[1,1e9].这个矩阵有一个值,如果相邻的多个数字 ...
- Gym 100971D 单调栈
D - Laying Cables Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- Gym 100971D Laying Cables 单调栈
Description One-dimensional country has n cities, the i-th of which is located at the point xi and h ...
- Code Forces Gym 100971D Laying Cables(单调栈)
D - Laying Cables Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- Gym - 101334F 单调栈
当时我的第一想法也是用单调栈,但是被我写炸了:我也不知道错在哪里: 看了大神的写法,用数组模拟的: 记录下单调递增栈的下标,以及每个数字作为最小值的最左边的位置. 当有数据要出栈的时候,说明栈里的数据 ...
- Gym 100971D Laying Cables 二分 || 单调栈
要求找出每个a[i],找到离他最近而且权值比它大的点,若距离相同,输出权利最大的那个 我的做法有点复杂,时间也要500+ms,因为只要时间花在了map上. 具体思路是模拟一颗树的建立过程,对于权值最大 ...
随机推荐
- SDUT-3375_数据结构实验之查找三:树的种类统计
数据结构实验之查找三:树的种类统计 Time Limit: 400 ms Memory Limit: 65536 KiB Problem Description 随着卫星成像技术的应用,自然资源研究机 ...
- bzoj1295 最长距离
Description windy有一块矩形土地,被分为 N*M 块 1*1 的小格子. 有的格子含有障碍物. 如果从格子A可以走到格子B,那么两个格子的距离就为两个格子中心的欧几里德距离. 如果从格 ...
- iphone开发中使用nib(xib)文件的内存管理
iphoneuinavigationcontrollercocoauiviewvariableswindows 在使用nib文件做界面开发的过程中,加载nib文件后,由于设置了outlet和deleg ...
- shell学习(18)- split切分文件命令
Linux split命令用于将一个文件分割成数个. 该指令将大文件分割成较小的文件,在默认情况下将按照每1000行切割成一个小文件. 语法: split [--help][--version][-& ...
- Python基础:15私有化
默认情况下,属性在Python 中都是“public”. 1:双下划线(__) Python 为类元素(属性和方法)的私有性提供初步的形式.由双下划线开始的属性在运行时被“混淆”,所以直接访问是不允许 ...
- Laravel中利用队列发送邮件的方法示例
https://www.jb51.net/article/121647.htm 本文主要给大家介绍了关于Laravel中队列发送邮件的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的 ...
- MapReduce数据流-Mapper
- 2019-2-11-WPF-获取应用的所有窗口
title author date CreateTime categories WPF 获取应用的所有窗口 lindexi 2019-02-11 08:55:31 +0800 2019-02-11 0 ...
- php解压缩
1.zip文件 2.rar文件 3.php调用linux指令进行解压缩 解压7z文件: 注:Windows下的文件编码和LINUX不一样,中文系统为GB,LINUX为UTF-8编码,这种情况下,中文名 ...
- 2018-11-19-win10-uwp-使用-Matrix3DProjection-进行-3d-投影
title author date CreateTime categories win10 uwp 使用 Matrix3DProjection 进行 3d 投影 lindexi 2018-11-19 ...