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 ;
}

最新文章

  1. Mvc传值
  2. PLSQL查询字段为科学计数法,修正显示
  3. 在MVC架构中使用CodeSmith生成NHibernate映射对象和实体类
  4. windows svn 配置以及iis同步传送
  5. Collection(数组、字典、集合)
  6. QQ登入(4)QQ分享-内容转载
  7. 并查集 基础 AC 2014-01-14 13:37 202人阅读 评论(0) 收藏
  8. 问题-提示“adodataset.command”
  9. 2015华为机试——数字基root
  10. Jquery中日期插件jquery.datepick的使用
  11. thinkphp pdo 重写问题
  12. for’ loop initial declarations are only allowed in C99 mode
  13. JSR-303校验类型
  14. Cs231n课堂内容记录-Lecture 8 深度学习框架
  15. dos下编译java
  16. Twitter基于R语言的时序数据突变检测(BreakoutDetection)
  17. PHP使用echo输出标签设置CSS样式问题
  18. Daily record-August
  19. 微信小程序客服消息使用
  20. 使用 IntraWeb (16) - 基本控件之 TIWList、TIWListbox、TIWComboBox、TIWOrderedListbox

热门文章

  1. Linux进程管理(一、 基本概念和数据结构)
  2. C++简单读取 &amp; 写入实例
  3. asp.net ajax客户端框架如何调用Web Service
  4. 《C程序设计语言》笔记(一)
  5. css技巧——垂直居中
  6. 实现一个简易的promise
  7. Vue知识点——vue数据深拷贝方法
  8. Linux的 crontab定时任务小记
  9. 网络编程--多线程 , socketserver
  10. @总结 - 7@ 生成树计数 —— matrix - tree 定理(矩阵树定理)与 pr&#252;fer 序列