• 2016 ACM Amman Collegiate Programming Contest D Rectangles


    Rectangles
    time limit per test
    5 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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
    分析:按行处理,对每个点找到向上和向左的的最大矩形;
       然后对于左边小矩形,直接加上之前答案即可;
       向上向左预处理后RMQ+二分找最大矩形即可,复杂度O(n2logn);

    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    #define sys system("pause")
    #define intxt freopen("in.txt","r",stdin)
    const int maxn=1e3+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,a[maxn][maxn],le[maxn][maxn],up[maxn][maxn],ans[maxn],mi[10][maxn],p[maxn];
    void init(int now)
    {
        for(int i=1;i<=m;i++)mi[0][i]=up[now][i];
        for(int i=1;i<=9;i++)
            for(int j=1;j+(1<<i)-1<=m;j++)
                mi[i][j]=min(mi[i-1][j],mi[i-1][j+(1<<(i-1))]);
    }
    int query(int l,int r)
    {
        int x=p[r-l+1];
        return min(mi[x][l],mi[x][r-(1<<x)+1]);
    }
    int main()
    {
        int i,j;
        rep(i,2,1000)p[i]=1+p[i>>1];
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&n,&m);
            rep(i,1,n)rep(j,1,m)a[i][j]=read();
            rep(i,1,n)rep(j,1,m)
            {
                up[i][j]=le[i][j]=1;
                if(a[i][j]==a[i-1][j])up[i][j]=up[i-1][j]+1;
                if(a[i][j]==a[i][j-1])le[i][j]=le[i][j-1]+1;
            }
            ll ret=0;
            rep(i,1,n)
            {
                init(i);
                memset(ans,0,sizeof(ans));
                rep(j,1,m)
                {
                    int l=j-le[i][j]+1,r=j,now_ans;
                    while(l<=r)
                    {
                        int mid=l+r>>1;
                        if(query(mid,j)>=up[i][j])now_ans=mid,r=mid-1;
                        else l=mid+1;
                    }
                    ans[j]+=up[i][j]*(j-now_ans+1);
                    if(now_ans>j-le[i][j]+1)ans[j]+=ans[now_ans-1];
                }
                rep(j,1,m)ret+=ans[j];
            }
            printf("%lld
    ",ret);
        }
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    python基础代码2
    将博客搬至CSDN
    Python基础代码1
    使用函数处理数据
    创建计算字段
    用通配符进行过滤
    高级过滤数据
    过滤数据
    模式与架构
    工厂方法模式和简单工厂模式的选折
  • 原文地址:https://www.cnblogs.com/dyzll/p/6151463.html
Copyright © 2020-2023  润新知