题目链接

http://codeforces.com/gym/101102/problem/D

problem  description

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

题意:输入R C表示一个矩阵,里面每一个矩阵元素中有一个数在1~1e9之间 ,求有多少个子矩阵里面的所有数相同?

思路:先对每一列从上到下遍历,记录h[i][j]表示从当前元素往上最多有多少数相同, 然后对每一行遍历,记录ans[i][j] 表示以a[i][j]为右下角的元素的子矩阵个数,那么可以发现: 设当前元素之前同一行中小于它且最近的元素为a[i][k] 那么ans[i][j]=ans[i][k]+(j-k)*h[i][j] 为了降低复杂度可以在当前行从左向右遍历的过程中把各列的高放入单调栈中,这样可以快速知道小于当前元素最近的元素的位置,最后对ans[][]求和就是结果 ;

代码如下:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <set>
#include <queue>
#include <vector>
using namespace std;
int a[][];
int h[][];
int ans[][];///包含某一点的贡献值;
int S[],pos[];///单调栈; int main()
{
int T;
cin>>T;
int R,C;
while(T--)
{
scanf("%d%d",&R,&C);
memset(h,,sizeof(h));
memset(a,,sizeof(a));
memset(ans,,sizeof(ans));
for(int i=;i<=R;i++)
for(int j=;j<=C;j++)
scanf("%d",&a[i][j]);
for(int j=;j<=C;j++)
{
int s;
for(int i=;i<=R;i++)
{
if(a[i][j]==a[i-][j]) s++;
else s=;
h[i][j]=s;
}
}
long long sum=;
for(int i=;i<=R;i++)
{
int num;
for(int j=;j<=C;j++)
{
if(a[i][j]!=a[i][j-]){
num=; S[]=j-; h[i][j-]=;
S[++num]=j;
pos[j]=j-;
ans[i][j]=h[i][j];
sum+=(long long)ans[i][j];
continue;
}
while(h[i][j]<=h[i][S[num]]) num--;
pos[j]=S[num];
S[++num]=j;
ans[i][j]+=h[i][j]*(j-pos[j]);
if(h[i][pos[j]]!=)
ans[i][j]+=ans[i][pos[j]];
sum+=(long long)ans[i][j];
}
}
printf("%lld\n",sum);
}
return ;
}

最新文章

  1. 【转】SVN提交一般原则
  2. 实现iOS图片等资源文件的热更新化(一): 从Images.xcassets导出合适的图片
  3. caffe中添加local层
  4. window对象中的常见方法
  5. php计算脚本执行时间
  6. oracle登录时shared memory realm does not exist的解决方法
  7. 一步一步学多线程-ThreadLocal源码解析
  8. bzoj1087 [SCOI2005][状压DP] 互不侵犯King (状压)
  9. #tensorflow入门(1)
  10. Cortex-M 实现互斥操作的三种方法
  11. android 2018 面试题
  12. java入门--学习地址
  13. mysql 原理 ~ double write
  14. Entity Framework学习初级篇3--LINQ TO Entities
  15. PropertiesUtil 读取properties
  16. SpringMVC(三)-- springmvc的系统学习之数据的处理,乱码及restful
  17. 修复 Tween.JS 的 onStop 设置无效
  18. jrMz and angles(水题)
  19. 读Bayes&#39; Theorem
  20. Mysql中关键词执行顺序

热门文章

  1. Atitit python3.0 3.3 3.5 3.6 新特性&#160;Python2.7新特性1Python 3_x 新特性1python3.4新特性1python3.5新特性1值得关注的新特性1Pyth
  2. FIR.im Weekly - 劳动节我们也没有停下来
  3. iOS-推送通知详解
  4. js 四舍五入函数 toFixed(),小数位数精度
  5. KnockoutJS 3.X API 第四章 表单绑定(7) event绑定
  6. CAS原子锁 高效自旋无锁的正确用法
  7. poj 2187 Beauty Contest(凸包求解多节点的之间的最大距离)
  8. java interface的两个经典用法
  9. 机器学习&amp;数据挖掘笔记_21(PGM练习五:图模型的近似推理)
  10. AngularJS in Action读书笔记6(实战篇)——bug hunting