题目链接:

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. Android 自定义录音、播放动画View,让你的录音浪起来

    最近公司项目有一个录音的录制和播放动画需求,然后时间是那么紧,那么赶紧开撸. 先看效果图 嗯,然后大致就是这样,按住录音,然后有一个倒计时,最外层一个进度条,还有一个类似模拟声波的动画效果(其实中间的 ...

  2. webpack+vue起步

    本文基于vue1.x 基于vue2.x&webpack2.x请移步至 Vue2.x踩坑与总结Webpack2.x踩坑与总结 记得第一次知道Vue.js是在勾三股四大大的微博,那时候他开始翻译v ...

  3. svn hooks 实现自动更新

    搞来搞去,原来是hooks 下面的脚本名称必须是post-commit才可以, 写成fly-commit一直不行.晕死~~~ https://serverfault.com/questions/144 ...

  4. FreeBSD 8

    FreeBSD 8.0的安装过程和7.2区别不大.先在FreeBSD官方网站上下载安装镜像,我一般都下载DVD的ISO,也有人爱好下最小的安装包,然后通过FTP或HTTP方式从网上下载各个程序包. 这 ...

  5. 【windows socket+UDPserverclient】

    Windows Socket+UDPserverclient      Winsock是 Windows下套接字标准.                    1.UDP socket编程:       ...

  6. cocosStudio中使用PageView,ListView和ScrollView

    晚上吃东西好像吃坏肚子了,.但是技术还要继续研究.最近工作中要使用CocosStudio做界面,好吧,不管对他有什么偏见,学习一下吧.这里主要记录一下三个控件的使用和说明.就是ScrollView,L ...

  7. MFC小程序02————— 不规则窗体小应用程序

    什么不说了.先上程序截图: 执行结果是有一棵有星星在闪烁的圣诞树.还会循环播放背景音乐. 之前也是在网上看到类似的一个程序.然后自己近期也在学MFC.所以就模仿着写了一个, 当中使用的是GDI+来显示 ...

  8. 为了发布博客方便,现从CSDN的naedzq整体迁移到cnblogs

    为了发布博客方便,现从CSDN的naedzq整体迁移到cnblogs

  9. 错误 1 error C1083: 无法打开包括文件: “numpy/arrayobject.h”: No such file

    问题:错误 1 error C1083: 无法打开包括文件: “numpy/arrayobject.h”: No such file 解答:加入include路径:E:\env\Anaconda2x6 ...

  10. php中屏蔽date的错误

    php中添加  date_default_timezone_set('asia/shanghai'); 可以屏蔽 <?php echo date('Y-m-d',$row3['time']); ...