题目链接:

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 中的Canvas画图

    Android中有一个Canvas类,Canvas类就是表示一块画布,你可以在上面画你想画的东西.当然,你还可以设置画布的属性,如画布的颜色/尺寸等.Canvas提供了如下一些方法: Canvas() ...

  2. iOS -- SKTexture类

    SKTexture类 继承自 NSObject 符合 NSCodingNSCopyingNSObject(NSObject) 框架  /System/Library/Frameworks/Sprite ...

  3. 修改ViewPager调用setCurrentItem时,滑屏的速度 ,解决滑动之间切换动画难看

    在使用ViewPager的过程中,有需要直接跳转到某一个页面的情况,这个时候就需要用到ViewPager的setCurrentItem方法了,它的意思是跳转到ViewPager的指定页面,但在使用这个 ...

  4. Windows系统文件详解【大全】

    这是网络上转载的一篇文章,找不到原创的出处了--详细的介绍了WINDOWS系统文件的用途,我想各位保存一份以后说不定会有用吧,呵呵..这里按A到Z为大家分好类了,查询的话可以按键盘的Ctrl+F进行搜 ...

  5. 判断用户Input输入的事件来进行登陆

    我们是通过键盘按的object.keyCode获取的 Html <input onkeydown="keydownMsg(event)" type="text&qu ...

  6. 直接返回list不封装的结果集

    直接返回list不封装的结果集,在Jsp访问方式: 1.封装成map访问 2.用jstl: <c:forEach var="images" items="${lis ...

  7. Android开发——进程间通信之AIDL(二)

    0.  前言 不论是Android还是其它操作系统.都会有自己的IPC机制.所谓IPC(Inter-Process Communication)即进程间通信.首先线程和进程是非常不同的概念,线程是CP ...

  8. 【西祠日志】【07】努力努力,找资料,思考,怎么做asp图片上传

    [西祠日志][07]努力努力,找资料.思考.怎么做asp图片上传  (2015.07.23周四) 今天忘了带本子.直接写在书上了笔记,晚点还是夹在本子里. 学了这么久的web应用,一直都没时间去做一点 ...

  9. Python+Selenium框架设计--- Page Object Model

    POM(Page Object Model):页面对象模型,POM是一种最近几年非常流行的自动化测试模型,或者思想,POM不是一个框架,就是一个解决问题的思想.采用POM的目的,是为了解决前端中UI变 ...

  10. __del__删除方法

    class dog: def __del__(self): print("删除机制被调用了...") dog1 = dog() del dog1 #在这里是先删除掉了,所以就去上面 ...