HDU 5749 Colmerauer

来源:互联网 发布:秦美人天神进阶数据 编辑:程序博客网 时间:2024/06/05 13:18


Problem Description
Peter has an n×m matrix M. Let S(a,b) be the sum of the weight all a×b submatrices of M. The weight of matrix is the sum of the value of all the saddle points in the matrix. A saddle point of a matrix is an element which is both the only largest element in its column and the only smallest element in its row. Help Peter find out all the value of S(a,b).

Note: the definition of saddle point in this problem may be different with the definition you knew before.
 

Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first contains two integers n and m (1n,m1000) -- the dimensions of the matrix.

The next n lines each contain m non-negative integers separated by spaces describing rows of matrix M (each element of M is no greater than 106).
 

Output
For each test case, output an integer W=(a=1nb=1mabS(a,b)) mod 232.
 

Sample Input
32 21 11 13 31 2 34 5 67 8 93 31 2 12 3 14 5 2
 

Sample Output
4600215
计算出每个点向上下左右扩展的距离,这一步可以使用单调栈完成。
然后就是推一波公式了,可以知道任意一个点a[i][j]在答案里贡献了多少。
#include<set>#include<map>#include<cmath>#include<stack>#include<queue>#include<bitset>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>#include<functional>#define rep(i,j,k) for (int i = j; i <= k; i++)#define per(i,j,k) for (int i = j; i >= k; i--)using namespace std;typedef unsigned int LL;const int low(int x) { return x&-x; }const int N = 1e3 + 10;const int INF = 0x7FFFFFFF;int T, n, m, a[N][N];LL L[N][N], R[N][N], U[N][N], D[N][N];stack<int> p;int main(){    scanf("%d", &T);    while (T--)    {        scanf("%d%d", &n, &m);        rep(i, 1, n) rep(j, 1, m) scanf("%d", &a[i][j]);        rep(i, 1, n)        {            for (int j = 1; j <= m; p.push(j++))            {                for (; !p.empty() && a[i][p.top()] >= a[i][j]; p.pop()) R[i][p.top()] = j - 1;            }            for (; !p.empty(); p.pop()) R[i][p.top()] = m;            for (int j = m; j; p.push(j--))            {                for (; !p.empty() && a[i][p.top()] >= a[i][j]; p.pop()) L[i][p.top()] = j + 1;            }            for (; !p.empty(); p.pop()) L[i][p.top()] = 1;        }        rep(i, 1, m)        {            for (int j = 1; j <= n; p.push(j++))            {                for (; !p.empty() && a[p.top()][i] <= a[j][i]; p.pop()) D[p.top()][i] = j - 1;            }            for (; !p.empty(); p.pop()) D[p.top()][i] = n;            for (int j = n; j; p.push(j--))            {                for (; !p.empty() && a[p.top()][i] <= a[j][i]; p.pop()) U[p.top()][i] = j + 1;            }            for (; !p.empty(); p.pop()) U[p.top()][i] = 1;        }        LL ans = 0;        rep(i, 1, n)        {            rep(j, 1, m)            {                LL l = (R[i][j] - j + 1) * (j - L[i][j] + 1) * (R[i][j] - L[i][j] + 2) / 2;                LL r = (D[i][j] - i + 1) * (i - U[i][j] + 1) * (D[i][j] - U[i][j] + 2) / 2;                ans += l * r * a[i][j];            }        }        printf("%u\n", ans);    }    return 0;}


0 0
原创粉丝点击