题目链接:

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. 第八讲_图像问答Image Question Answering

    第八讲_图像问答Image Question Answering 课程结构 图像问答的描述 具备一系列AI能力:细分识别,物体检测,动作识别,常识推理,知识库推理..... 先要根据问题,判断什么任务 ...

  2. java 发送微信客服消息

    package com.baosight.wechat.service; import net.sf.json.JSONObject; import org.apache.commons.httpcl ...

  3. MRP routing设置释疑

    Jeffer9@gmail.com         工艺是指在不同工作中心执行的作业序列         作业的详细信息 Number of cycles 在该工作中心操作几个循环 Number of ...

  4. javascript之Ajax获取和设置标头

    XMLHttpRequest对象中与标头有关的方法: setRequestHeader(<header>,<value>)--用指定值设置标头. getResponseHead ...

  5. vs2012 MinGW 编译ffmeg 引用外部库libx264,librtmp

    VS2012如何编译ffmpeg前面已经有文章讲过,本来主要讲述如何引用外部库libx264,librtmp, ffmpeg版本是3.0.2. 1. 下载x264源文件并编译 源码地址是http:// ...

  6. Laravel建站02--配置Laravel

    Laravel项目的根目录下有.env文件,如果没有可以把.env.example改名为.env 这个文件是配置文件,可以把app_key.数据库.redis缓存等配置信息写在这个文件里. 目前5.4 ...

  7. 无线(仅WIFI)攻击思路总结

    从事信息安全相关工作5年了,虽然主要工作是安全产品售前.安全服务等方向,但既然选择了安全,想必都是有点黑客情节的,因此也前前后后杂七杂八的学了点东西.最近在研究无线(主要是WIFI)安全,相关书籍看了 ...

  8. mysql导入数据库_仅仅用frm向mysql导入表结构

    网上一个连接mysql的jsp代码段,给了数据库的备份文件.可是仅仅有frm, mysql的每张表有三个文件.各自是,*.frm是描写叙述了表的结构.*.MYD保存了表的数据记录.*.MYI则是表的索 ...

  9. 基于TCP的通信程序设计

    套接字(Socket)是一种跨主机进程之间的双向通信接口,每个打开的套接字都可以通过一个套接字描述符来描述,因此可以使用低级文件编程库操作套接字. TCP是一中面向连接的网络传输控制协议.它每发送一个 ...

  10. 用js判断文本框中的是不是空,是否有空格

    <script type="text/javascript"> function checkRoleName(){ var userName=document.getE ...