分析:如题解所说求出每个元素的a,b,c,d之后计算贡献的系数即可。。规律好难找,其实系数为a*b*c*d*(a+b)*(c+d)/4。
代码:
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=1010;
const int mod=100000000;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const int MAX=1000000010;
const ll INF=1ll<<55;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int ui;
typedef unsigned long long ull;
unsigned int a[N][N],mi[N];
int u[N][N],d[N][N],l[N][N],r[N][N];
ui get(ui a,ui b,ui c,ui d) {
ui ret=0;//a*b*c*d*(a+b)*(c+d)/4
if (a%2==b%2) ret=(a+b)/2*a*b;
else if (a%2==0) ret=a/2*b*(a+b);
else ret=b/2*a*(a+b);
if (c%2==d%2) ret*=(c+d)/2*c*d;
else if (c%2==0) ret*=c/2*d*(c+d);
else ret*=d/2*c*(c+d);
return ret;
}
int main()
{
int i,j,n,m,t;
unsigned int ans;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &m);
for (i=1;i<=n;i++)
for (j=1;j<=m;j++) scanf("%u", &a[i][j]);
for (i=1;i<=n;i++) a[i][0]=a[i][m+1]=0;
for (i=1;i<=m;i++) a[0][i]=a[n+1][i]=1000010;
for (i=1;i<=n;i++) {
l[i][1]=0;
for (j=2;j<=m;j++)
if (a[i][j]>=a[i][j-1]) l[i][j]=j-1;
else {
l[i][j]=l[i][j-1];
while (a[i][j]<a[i][l[i][j]]) l[i][j]=l[i][l[i][j]];
}
r[i][m]=m+1;
for (j=m-1;j;j--)
if (a[i][j]>=a[i][j+1]) r[i][j]=j+1;
else {
r[i][j]=r[i][j+1];
while (a[i][j]<a[i][r[i][j]]) r[i][j]=r[i][r[i][j]];
}
}
for (i=1;i<=m;i++) {
u[1][i]=0;
for (j=2;j<=n;j++)
if (a[j][i]<=a[j-1][i]) u[j][i]=j-1;
else {
u[j][i]=u[j-1][i];
while (a[j][i]>a[u[j][i]][i]) u[j][i]=u[u[j][i]][i];
}
d[n][i]=n+1;
for (j=n-1;j;j--)
if (a[j][i]<=a[j+1][i]) d[j][i]=j+1;
else {
d[j][i]=d[j+1][i];
while (a[j][i]>a[d[j][i]][i]) d[j][i]=d[d[j][i]][i];
}
}
ans=0;
for (i=1;i<=n;i++)
for (j=1;j<=m;j++) ans+=a[i][j]*get(i-u[i][j],d[i][j]-i,j-l[i][j],r[i][j]-j);
printf("%u\n", ans);
}
return 0;
}