#include #include #include #include #include #include #include #include #include #include //#include //#define ls (x<<1) //#define rs (x<<1|1) //#define mid ((lt[x].l+lt[x].r)/2) //#define ID(x, y) ((x)*m+(y)) #define CHECK(x, y) ((x)>=0 && (x)=0 && (y) PII; const int INF=0x3f3f3f3f; void Open() { #ifndef ONLINE_JUDGE freopen("/home/qingping/in.txt","r",stdin); // freopen("D:/my.txt","w",stdout); #endif // ONLINE_JUDGE } const int N = 1010; int L[N][N]; int R[N][N]; int U[N][N]; int D[N][N]; UINT S[N][N]; UINT sum[N][N]; int a[N][N]; int c[N], sta[N], b[N]; int UP = 1000; void addp(int x, int val, int ma) { if(x == 0) return ; for(int i = x; i <= UP; i += i & -i) if(ma) c[i] = max(c[i], val); else c[i] = min(c[i], val); } int getp(int x, int ma) { int res = 0; if(!ma) res = INF; for(int i = x; i; i -= i&-i) if(ma)res = max(res, c[i]); else res = min(res, c[i]); return res; } void adds(int x, int val, int ma) { for(int i = x; i; i -= i & -i) if(ma) c[i] = max(c[i], val); else c[i] = min(c[i], val); } int gets(int x, int ma) { int res = 0; if(!ma) res = INF; if(x == 0) return res; for(int i = x; i<=UP; i += i&-i) if(ma) res = max(res, c[i]); else res = min(res, c[i]); return res; } int sumpre(int a, int b) { if(a > b) swap(a, b); return (a+b)*(b-a+1)/2; } int main(){ // Open(); // cout<= 1; j--) { R[i][j] = getp(b[j], 0); if(R[i][j] == INF) R[i][j] = m+1; addp(b[j], j, 0); } } for(int j = 1; j <= m; j++) { int t = 0; for(int i = 1; i <= n; i++) sta[t++] = a[i][j]; sort(sta, sta+t); t = unique(sta, sta+t) - sta; memset(c, 0, sizeof(c)); for(int i = 1; i <= n; i++) { b[i] = lower_bound(sta, sta+t, a[i][j]) - sta+1; D[i][j] = gets(b[i], 1); adds(b[i], i, 1); } memset(c, 0x3f, sizeof(c)); for(int i = n; i>=1; i--) { U[i][j] = gets(b[i], 0); if(U[i][j] == INF) U[i][j] = n+1; adds(b[i], i, 0); } } UINT ans = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { int A = i - D[i][j] - 1; int b = U[i][j] - i - 1; int c = j - L[i][j] - 1; int d = R[i][j] - j - 1; ans = (ans + sum[b][A+1]*sum[d][c+1]*a[i][j]); } printf("%u\n", ans); } return 0; }