题目链接:

Colmerauer

Time Limit: 10000/5000 MS (Java/Others)  

  Memory Limit: 131072/131072 K (Java/Others)

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 (1≤n,m≤1000) -- 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=1n∑b=1ma⋅b⋅S(a,b)) mod 232.
 
Sample Input
 
3
2 2
1 1
1 1
3 3
1 2 3
4 5 6
7 8 9
3 3
1 2 1
2 3 1
4 5 2
 
Sample Output
 
4
600
215
 
 
题意:
 
http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=718&pid=1003
 
思路:
 
把每个矩阵的值单独找出来复杂度太高,就考虑每个元素对答案的贡献,找出这个元素在
 
对于矩阵种一个元素M(x,y)考虑他可以成为那些子矩阵的鞍点, 用单调队列之类的东西处理出a,b,c,d分别表示在第x行中, 这个元素在第y-a列到y+b列中都是唯一最小值; 第y列中, 这个元素在第x-c到x+d行中都是唯一最大值.
然后知道了这个范围就是找出有多少个矩阵包含这个元素以及这些矩阵的a*b;然后就是找到了一个公式,(a+b)*a*b*c*d*(c+d)/4;
 
AC代码:
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef unsigned long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1LL<<32;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e7+10;
const int maxn=1000+10;
const double eps=1e-8; int up[maxn][maxn],down[maxn][maxn],le[maxn][maxn],ri[maxn][maxn],a[maxn][maxn],pos[maxn]; inline LL cal(int a,int b,int c,int d)
{
LL ans=1;
ans=((LL)a*b*(a+b)/2)%mod;
ans=((LL)c*d*(c+d)/2)%mod*ans%mod;
return ans;
} int main()
{
int t;
read(t);
while(t--)
{
mst(pos,0);
int n,m;
read(n);read(m);
For(i,1,n)For(j,1,m)read(a[i][j]);
For(i,1,n)
{
int l=1,r=0;
For(j,1,m)
{
while(r>=l&&a[i][pos[r]]>a[i][j])r--;
le[i][j]=pos[r]+1;
pos[++r]=j;
}
l=m+1,r=m;
pos[m+1]=m+1;
for(int j=m;j>0;j--)
{
while(l<=r&&a[i][pos[l]]>a[i][j])l++;
ri[i][j]=pos[l]-1;
pos[--l]=j;
}
} For(i,1,m)
{
int l=1,r=0;
For(j,1,n)
{
while(r>=l&&a[pos[r]][i]<a[j][i])r--;
up[j][i]=pos[r]+1;
pos[++r]=j;
}
l=n+1,r=n;
pos[n+1]=n+1;
for(int j=n;j>0;j--)
{
while(l<=r&&a[pos[l]][i]<a[j][i])l++;
down[j][i]=pos[l]-1;
pos[--l]=j;
}
}
LL ans=0;
For(i,1,n)
{
For(j,1,m)
{
le[i][j]=j-le[i][j]+1;
ri[i][j]=ri[i][j]-j+1;
up[i][j]=i-up[i][j]+1;
down[i][j]=down[i][j]-i+1;
ans=(ans+cal(up[i][j],down[i][j],le[i][j],ri[i][j])*a[i][j])%mod;
}
}
print(ans);
}
return 0;
}

  

 

hdu-5749 Colmerauer(单调栈)的更多相关文章

  1. HDU 5749 Colmerauer 单调队列+暴力贡献

    BestCoder Round #84   1003 分析:(先奉上zimpha巨官方题解) 感悟:看到题解单调队列,秒懂如何处理每个点的范围,但是题解的一句算贡献让我纠结半天 已知一个点的up,do ...

  2. HDU 5033 Building(单调栈)

    HDU 5033 Building(单调栈) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5033 Description Once upon a ti ...

  3. hdu 5033 Building (单调栈 或 暴力枚举 )

    Description Once upon a time Matt went to a small town. The town was so small and narrow that he can ...

  4. Largest Rectangle in a Histogram HDU - 1506 (单调栈)

    A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...

  5. HDU 1506【单调栈】

    思路: 转化成对于某一位置为最小值求向两边最远>=他的位置,用单调栈就能轻易完成. 那么ans=(left+right)*h[i]; 维护单调递增还是递减呢? 我们能很快反应到,一旦碰到一个比他 ...

  6. HDU 3410【单调栈】

    思路: 单调栈. 鄙人的记忆:按当前为最大值的两边延伸就是维护单调递减栈. //#include <bits/stdc++.h> #include <iostream> #in ...

  7. HDU - 5033 Building (单调栈+倍增)

    题意:有一排建筑,每座建筑有一定的高度,宽度可以忽略,求在某点的平地上能看到天空的最大角度. 网上的做法基本都是离线的...其实这道题是可以在线做的. 对于向右能看到的最大角度,从右往左倍增维护每个时 ...

  8. HDU5479 Colmerauer 单调栈+暴力优化

    http://acm.hdu.edu.cn/showproblem.php?pid=5749 思路: bestcoder 84 贡献:所有可能的子矩阵的面积和 //len1:子矩阵所有长的和 ;i&l ...

  9. hdu 5749 Colmerauer

    题意:对于给定的$n \times m$矩阵$M$,定义$S(a,b)$为$M$的所有$a \times b$子矩阵的权重之和.一个矩阵的权重是指矩阵中所有马鞍点权值之和,在一个矩阵中某点是马鞍点当且 ...

随机推荐

  1. openfire Android学习(一)----实现用户注册、登录、修改密码和注销等

    以前学习过用Scoket 建立聊天,简单的建立聊天是没问题的,但如果要实现多人复杂的聊天,后台服务器代码就比较复杂,对于我这新手来讲就比较难了.后来在网上看到用openfire做服务器,利用强大的Sm ...

  2. xgboost的SparkWithDataFrame版本实现

    再xgboost的源码中有xgboost的SparkWithDataFrame的实现,如下:https://github.com/dmlc/xgboost/tree/master/jvm-packag ...

  3. loarocks install loadcaffe 失败

    loarocks install loadcaffe 失败 1.Error: Your user does not have write permissions in /home/zhangliang ...

  4. Resharper 8.2的“安装”问题

    概述 完美解决Resharper 8.2的“安装”问题和VS2012写Javascript语句无法智能提示的问题: 目录 引言——Resharper 简介——安装——VS2012智能提示测试 引言 最 ...

  5. C语言-回溯例3

    排列问题 1.实现排列A(n,m)对指定的正整数m,n(约定1<m<=n),具体实现排列A(n,m).2. 回溯算法设计设置一维数组a,a(i)(i=1,2,…,m)在1—n中取值.首先从 ...

  6. window 平台搭建简单的直播点播系统

    Windows平台如何搭建简单的直播系统前文已经有介绍,今天介绍下如何搭建简单的点播系统. 同样还是利用crtmpServer,crtmpServer可以从github中下载,可以从群里下载(群里有修 ...

  7. 通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core? .Net Web开发技术栈

    通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core?   什么是.NET?什么是.NET Framework?本文将从上往下,循序渐进的介绍一系列相关.NET的概念 ...

  8. web前端面试系列 一 js闭包

    一.什么是闭包? JavaScript高级程序设计第三版: 闭包是指有权访问另一个函数作用域中的变量的函数. 在js中定义在函数内部的子函数能够访问外部函数定义的变量,因此js内部函数就是一个闭包. ...

  9. windows下搭建hadoopproject(一)

    这里是接着之前的一篇 <hadoop在windows下的环境搭建 >来的~~~ 一.安装文件准备 1:下载好hadoop-1.0.0.tar.gz, 下载地址是https://archiv ...

  10. python--简易员工信息系统编写

    补充内容:eval 将字符串变成变量名locals   看输入的是否是字典中的一个keyfunc.__name____怎么看变量名的数据类型斐波那契数列 li=[1,1] while li[-1]&l ...