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

题目链接

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

13 33 3 13 3 12 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[1005][1005];
int h[1005][1005];
int ans[1005][1005];///包含某一点的贡献值;
int S[1005],pos[1005];///单调栈;

int main()
{
    int T;
    cin>>T;
    int R,C;
    while(T--)
    {
        scanf("%d%d",&R,&C);
        memset(h,0,sizeof(h));
        memset(a,0,sizeof(a));
        memset(ans,0,sizeof(ans));
        for(int i=1;i<=R;i++)
            for(int j=1;j<=C;j++)
            scanf("%d",&a[i][j]);
        for(int j=1;j<=C;j++)
        {
            int s;
            for(int i=1;i<=R;i++)
            {
                if(a[i][j]==a[i-1][j]) s++;
                else s=1;
                h[i][j]=s;
            }
        }
        long long sum=0;
        for(int i=1;i<=R;i++)
        {
            int num;
            for(int j=1;j<=C;j++)
            {
                if(a[i][j]!=a[i][j-1]){
                    num=1;  S[1]=j-1;  h[i][j-1]=0;
                    S[++num]=j;
                    pos[j]=j-1;
                    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]]!=0)
                ans[i][j]+=ans[i][pos[j]];
                sum+=(long long)ans[i][j];
            }
        }
        printf("%lld\n",sum);
    }
    return 0;
}
时间: 2024-10-07 05:01:58

Gym 101102D---Rectangles(单调栈)的相关文章

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

[Agc081F/At2699] 给出一个拥有 \(H\times W\) 个格子的棋盘,每个格子的颜色为黑色或白色. Snuke 可以进行任意次下列操作: 选择棋盘中的一行或一列,将这一行或一列的颜色翻转(黑变成白,白变成黑) Snuke 想知道,在他进行操作后,棋盘中最大的全黑矩形最大能为多少. 考虑 \(2\times 2\) 方格,当且仅当偶数个黑时,可以做成全黑 大矩形能做成全黑,当且仅当所有 \(2\times 2\) 子格都是偶数个黑 然后就是一个很朴素的单调栈求最大矩形了 注意到

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

D - Laying Cables Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u One-dimensional country has n cities, the i-th of which is located at the point xi and has population pi, and all xi, as well as all pi, are distinct. Whe

Gym 100971D Laying Cables 单调栈

Description One-dimensional country has n cities, the i-th of which is located at the point xi and has population pi, and all xi, as well as all pi, are distinct. When one-dimensional country got the Internet, it was decided to place the main server

[arc081] F - Flip and Rectangles——思维题+单调栈

题目大意: 给定一个\(n\times m\)的01矩形,每次可以翻转一行或者翻转一列. 求翻转若干次之后的最大全1子矩形. 思路: 首先我们要知道一个结论:如果一个子矩形可以被翻转成为全1矩形,那么它内部的每一个\(2\times 2\)的子矩形的1的个数为偶数. 如果存在一个\(2\times 2\)的子矩形有奇数个1,那么无论怎么操作都还是奇数. 如果所有的\(2\times 2\)的子矩形都有偶数个1,我们可以先使这个矩形的第一行第一列都变为1,根据奇偶性不难发现整个矩阵此时必定全部都变

(单调栈)poj-2559 Largest Rectangle in a Histogram

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the

HDU4252 ACM fighting(单调栈)

Description After Mr. B arrived in Warsaw, he was shocked by the skyscrapers and took several photos. But now when he looks at these photos, he finds in surprise that he isn't able to point out even the number of buildings in it. So he decides to wor

hdu1506---Largest Rectangle in a Histogram(单调栈)

Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of

HDOJ 4252 A Famous City 单调栈

单调栈: 维护一个单调栈 A Famous City Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1671    Accepted Submission(s): 644 Problem Description After Mr. B arrived in Warsaw, he was shocked by the skyscrap

POJ 2559 Largest Rectangle in a Histogram(单调栈)

Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectang