Codeforces gym 101102 D 单调栈
来源:互联网 发布:手机数据魔方 编辑:程序博客网 时间:2024/05/28 23:12
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.
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.
For each test case, print the answer on a single line.
13 33 3 13 3 12 2 5
16
题意:求有多少子矩阵 元素是唯一的
题解:枚举矩阵的右下角 用单调栈维护左上角可能的点
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<deque>using namespace std;typedef long long ll;int a[1005][1005],up[1005][1005];deque<int>sp;int main(){int t;scanf("%d",&t);while(t--){int n,m,i,j,k;scanf("%d%d",&n,&m);for(i=1;i<=n;i++){for(j=1;j<=m;j++){scanf("%d",&a[i][j]);up[i][j]=(a[i][j]==a[i-1][j])?up[i-1][j]:i;}}ll ans=0,sum;for(i=1;i<=n;i++){for(j=1;j<=m;j++){int p=i-up[i][j]+1;if(a[i][j]!=a[i][j-1]){while(!sp.empty())sp.pop_front();sum=0;sp.push_back(p);sum+=p;ans+=sum;}else{int now=0;while(!sp.empty()&&sp.back()>p){now++;sum-=sp.back();sp.pop_back();}for(k=1;k<=now+1;k++){sum+=p;sp.push_back(p);}ans+=sum;}}}printf("%lld\n",ans);}return 0;}
- Codeforces gym 101102 D 单调栈
- Gym 101102D 单调栈
- Gym 101102D Rectangles 【单调栈】
- GYM 101102 D.Rectangles(单调栈)
- CodeForces Gym 100735D
- codeforces 602 D. Lipshitz Sequence (单调栈)
- codeforces GYM 101431D(构造)
- Codeforces Gym 101485D Debugging
- Codeforces Gym 100623D Problem D. Deposits
- 【CodeForces】406D Hill Climbing 单调栈建树+树链剖分求LCA
- Codeforces #548D Mike and Feet(单调栈)
- CodeForces - 548D Mike and Feet(单调栈)
- Codeforces Round #333 D. Lipshitz Sequence (单调栈)
- CodeForces 548D Mike and Feet (单调栈)
- codeforces 548 D. Mike and Feet (单调栈)
- CodeForces 548D (单调栈+伪DP)
- codeforces 817D Imbalanced Array(单调栈)
- Educational Codeforces Round 23 817-D Imbalanced Array 单调栈
- A
- 例题8-1 UVA
- xdoj 1190: 虢莔薅参加运动会
- 0欧姆电阻的作用
- 刷题-CC150-Java实现
- Codeforces gym 101102 D 单调栈
- POJ2080简单模拟
- Linux基础学习笔记之文件类型
- 网络编程基础
- Javascript基础学习(二)
- 图论(五)--强连通分量
- 常见错误
- java.langNoClassdefFoundError & java.lang.ClassNotFoundException
- ubuntu锁屏后,字符密码无法输入