题目链接:

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设置TextView行间距(非行高)

    Android设置TextView行间距(非行高) Android系统中TextView默认显示中文时会比较紧凑,不是很美观. 为了让每行保持一定的行间距,可以设置属性android:lineSpac ...

  2. C#规范整理·集合和Linq

    LINQ(Language Integrated Query,语言集成查询)提供了类似于SQL的语法,能对集合进行遍历.筛选和投影.一旦掌握了LINQ,你就会发现在开发中再也离不开它.   开始! 前 ...

  3. SPFA 求带负权的单源最短路

    int spfa_bfs(int s) { ///s表示起点. queue <int> q; memset(d,0x3f,sizeof(d)); ///d数组中存下的就是最短路径(存在的话 ...

  4. ffmpeg 视频教程 添加水印附源码

    本文主要讲述如何利用Ffmpeg向视频文件 添加水印这一功能,文中最后会给出源代码下载地址以及视频 下载地址,视频除了讲述添加水印的基本原理以及代码实现,还提到了要注意的一些地方,因为直接运行 dem ...

  5. Regex 手机号 座机 正則表達式

    近期在工作中须要推断一个号码是否是手机号,是否是座机号. 在网上也搜到了大家总结的方法,没有直接使用这些方法是由于:手机号码在不断開始新的号码段(比方17x).座机号中个别区号由于行政区域的变化而废除 ...

  6. Unity光滑与粗糙的材质——相似于生锈的金属表面

    纹理是在Photoshop中制作的,终于效果则是在Unity里得到的.这样的类型的材质.在3D游戏中非经常见.

  7. HDFS源代码分析(二)-----元数据备份机制

    前言 在Hadoop中,全部的元数据的保存都是在namenode节点之中,每次又一次启动整个集群,Hadoop都须要从这些持久化了的文件里恢复数据到内存中,然后通过镜像和编辑日志文件进行定期的扫描与合 ...

  8. beifen---http://vdisk.weibo.com/s/uhCtnyUhD0Ooc

  9. Linux问题,磁盘分区打不开了

    Metadata kept in Windows cache, refused to mount. chkdsk /f http://www.bubuko.com/infodetail-1184937 ...

  10. byte数组和文件的相互转换

    /** * 获得指定文件的byte数组 */ private byte[] getBytes(String filePath){ byte[] buffer = null; try { File fi ...