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

Input
1
3 3
3 3 1
3 3 1
2 2 5
Output
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 (单调栈)的更多相关文章

  1. D - Laying Cables Gym - 100971D (单调栈)

    题目链接:https://cn.vjudge.net/problem/Gym-100971D 题目大意:给你n个城市的信息,每一个城市的信息包括坐标和人数,然后让你找每一个城市的父亲,作为一个城市的父 ...

  2. [Agc081F/At2699] Flip and Rectangles - 单调栈,结论

    [Agc081F/At2699] 给出一个拥有 \(H\times W\) 个格子的棋盘,每个格子的颜色为黑色或白色. Snuke 可以进行任意次下列操作: 选择棋盘中的一行或一列,将这一行或一列的颜 ...

  3. Gym 101102D---Rectangles(单调栈)

    题目链接 http://codeforces.com/gym/101102/problem/D problem  description Given an R×C grid with each cel ...

  4. 单调队列 + 组合数统计 Gym 101102D

    题目链接:http://codeforces.com/gym/101102/problem/D 题目大意:给你一个n*m的矩阵,矩阵里面的数值范围为[1,1e9].这个矩阵有一个值,如果相邻的多个数字 ...

  5. Gym 100971D 单调栈

    D - Laying Cables Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u ...

  6. Gym 100971D Laying Cables 单调栈

    Description One-dimensional country has n cities, the i-th of which is located at the point xi and h ...

  7. Code Forces Gym 100971D Laying Cables(单调栈)

    D - Laying Cables Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u ...

  8. Gym - 101334F 单调栈

    当时我的第一想法也是用单调栈,但是被我写炸了:我也不知道错在哪里: 看了大神的写法,用数组模拟的: 记录下单调递增栈的下标,以及每个数字作为最小值的最左边的位置. 当有数据要出栈的时候,说明栈里的数据 ...

  9. Gym 100971D Laying Cables 二分 || 单调栈

    要求找出每个a[i],找到离他最近而且权值比它大的点,若距离相同,输出权利最大的那个 我的做法有点复杂,时间也要500+ms,因为只要时间花在了map上. 具体思路是模拟一颗树的建立过程,对于权值最大 ...

随机推荐

  1. QT 捕获事件(全局拦截)

    QT 捕获应用键盘事件(全局拦截) 主窗口只有一个QTabWidget,每个tab中嵌入相应的窗口,在使用的过程中,需要主窗口响应键盘事件,而不是tab中的控件响应.故采取以下方式. 重写QAppli ...

  2. hdu3549 最大流

    #include<stdio.h> #include<string.h> #include<queue> #define MAXN 1010 using names ...

  3. CODE FESTIVAL 2017 qual B B - Problem Set【水题,stl map】

    CODE FESTIVAL 2017 qual B B - Problem Set 确实水题,但当时没想到map,用sort后逐个比较解决的,感觉麻烦些,虽然效率高很多.map确实好写点. 用map: ...

  4. oracle HEXTORAW(c1)

    [功能]将一个十六进制构成的字符串转换为二进制 [参数]c1,十六进制的字符串 [返回]字符串 [示例] select HEXTORAW('A123')  from dual;

  5. Gym - 101617D_Jumping Haybales(BFS)

    Sample Input 4 2 .### #... .#.. #.#. 3 1 .#. .#. .#. Sample Output 4 -1 题意:给一个n*n的图,每次最多能跳k个格子,只能向南( ...

  6. Vue.js 第2章 钩子函数&自定义指令&过滤器&计算属性&侦听器

    目标 钩子函数 自定义指令 自定义过滤器 计算属性 监听属性 局部自定义指令 为什么需要自定义指令 为了复用,为了代码的灵活 指令的分类:全局指令,局部指令 在vm外面创建的指令 通过Vue.dire ...

  7. 使用jQuery的 autocomplete 实现输入框 自动提示补全

    参考网址: https://www.cnblogs.com/jinzhiming/p/6768402.html 插件下载地址: 链接:https://pan.baidu.com/s/1SpP3hixZ ...

  8. 洛谷P1616 疯狂的采药

    //完全背包 #include<bits/stdc++.h> using namespace std; ; ; int n,m,v[maxn],w[maxn],f[maxv]; int m ...

  9. oracle用UNION替换OR (适用于索引列)

    通常情况下, 用UNION替换WHERE子句中的OR将会起到较好的效果. 对索引列使用OR将造成全表扫描. 注意, 以上规则只针对多个索引列有效. 如果有column没有被索引, 查询效率可能会因为你 ...

  10. 浮动,定位,flex布局

    什么是文档流 英文原文是:Normal flow. In CSS 2.1, normal flow includes block formatting of block-level boxes, in ...