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.
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 integersn and m (1≤n,m≤1000) -- the dimensions of the matrix.
The nextn lines each contain m non-negative integers separated by spaces describing rows of matrix M (each element of M is no greater than 106 ).
The first contains two integers
The next
Output
For each test case, output an integer W=(∑a=1n∑b=1ma⋅b⋅S(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
- Hdu 5749 Colmerauer
- hdu 5749 Colmerauer
- HDU 5749 Colmerauer
- HDU 5749 Colmerauer(单调栈+推公式)
- HDU 5749 Colmerauer(单调栈)
- HDU 5749 Colmerauer(单调栈、思维、组合)
- 【HDU 5749】Colmerauer(单调栈+组合数学)
- HDU 5749 BestCoder Round #84 Colmerauer(暴力贡献)
- HDU 5749 Colmerauer(单调栈或单调队列求解固定区间最大或最小值)
- 【HDU5749 BestCoder Round 84C】【ST-RMQ?NO!暴力跳法or单调栈法 + 贡献思维】Colmerauer 所有子矩阵size乘鞍点权值和
- hdu 5749 公式
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- Umbraco 官方技术文档 翻译 八、Templates 模板
- spring-session使用教程(一):redis共享session
- vc2015 编译libcurl带openssl
- MySQL导入大批量数据时报错:MySQL server has gone away
- JavaScript实现拖拽预览,AJAX小文件上传
- HDU 5749 Colmerauer
- 点击屏幕键盘退出
- POJ 1915 Knight Moves
- SQL SERVER 连接几个知识点
- 半平面交
- 三维旋转:旋转矩阵,欧拉角,四元数
- LZH.T
- 快捷键与加速键的区别
- 使用xib开发界面