【ZOJ3955 The 17th Zhejiang University Programming Contest H】【二分 贡献计数】Saddle Point 鞍点贡献

来源:互联网 发布:开罗旅游自助攻略知乎 编辑:程序博客网 时间:2024/06/06 07:14

Saddle Point

Time Limit: 1 Second      Memory Limit: 131072 KB

Chiaki has an n × m matrix A. Rows are numbered from 1 to n from top to bottom and columns are numbered from 1 to m from left to right. The element in the i-th row and the j-th column is Aij.

Let M({i1i2, ..., is}, {j1j2, ..., jt}) be the matrix that results from deleting row i1i2, ..., is and column j1j2, ..., jt of A and f({i1i2, ..., is}, {j1j2, ..., jt}) be the number of saddle points in matrix M({i1i2, ..., is}, {j1j2, ..., jt}).

Chiaki would like to find all the value of f({i1i2, ..., is}, {j1j2, ..., jt}). As the output may be very large ((2n - 1)(2m - 1) matrix in total), she is only interested in the value

$$\left(\sum_{\begin{array}{r} 1 \le i_1 < \dots < i_s \le n \\ 1 \le j_1 < \dots < j_t \le m \\ 0 \le s < n \\ 0 \le t < m\end{array}} f(\{i_1,i_2,\dots,i_s\},\{j_1,j_2,\dots,j_t\})\right) \bmod (10^9+7).$$

Note that 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.

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 line contains four integers n and m (1 ≤ nm ≤ 1000) -- the number of rows and the number of columns.

Each of the next n lines contains m integer Ai, 1Ai, 2, ..., Aim (1 ≤ Aij ≤ 106), where Aij is the integer in the i-th row and the j-th column.

It is guaranteed that neither the sum of all n nor the sum of all m exceeds 5000.

Output

For each test case, output an integer denoting the answer.

Sample Input

22 21 11 14 51 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 20

Sample Output

4465

Author: LIN, Xi
Source: The 17th Zhejiang University Programming Contest Sponsored by TuSimple

#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }#define MS(x, y) memset(x, y, sizeof(x))#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }const int N = 1010, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }int casenum, casei;int n, m;vector<int>line[N], list[N];int a[N][N];int bit[N * 2];int main(){bit[0] = 1; for (int i = 1; i <= 2000; ++i)bit[i] = bit[i - 1] * 2 % Z;scanf("%d", &casenum);for (casei = 1; casei <= casenum; ++casei){scanf("%d%d", &n, &m);for (int i = 1; i <= n; ++i)line[i].clear();for (int i = 1; i <= m; ++i)list[i].clear();for (int i = 1; i <= n; ++i){for (int j = 1; j <= m; ++j){scanf("%d", &a[i][j]);list[j].push_back(a[i][j]);line[i].push_back(a[i][j]);}}for (int i = 1; i <= n; ++i)sort(line[i].begin(), line[i].end());for (int i = 1; i <= m; ++i)sort(list[i].begin(), list[i].end());int ans = 0;for (int i = 1; i <= n; ++i){for (int j = 1; j <= m; ++j){//行最小,大的随便删int line_can_del = line[i].size() - (upper_bound(line[i].begin(), line[i].end(), a[i][j]) - line[i].begin());//列最大,小的随便删int list_can_del = (lower_bound(list[j].begin(), list[j].end(), a[i][j]) - list[j].begin());gadd(ans, bit[line_can_del + list_can_del]);}}printf("%d\n", ans);}return 0;}/*【题意】找{列严格最大,行严格最小}的节点贡献之和【分析】按照贡献思想直接算即可*/


0 0
原创粉丝点击