HDU5479 Colmerauer 单调栈+暴力优化
http://acm.hdu.edu.cn/showproblem.php?pid=5749
思路:
bestcoder 84

贡献:所有可能的子矩阵的面积和
//len1:子矩阵所有长的和
for(int i=;i<=L;i++){
for(int j=;j<=R;j++){
len1+=i+j-;//-1是因为1*1单元格也是鞍点
}
}
// #pragma comment(linker, "/STACK:102c000000,102c000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string>
#include <algorithm>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <conio.h>
using namespace std;
#define clc(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int N = ;
const int M = 1e6+;
const long long MOD = 1LL<<;
#define LL long long
#define LB long double
#define mi() (l+r)>>1
double const pi = acos(-);
const double eps = 1e-;
void fre() {
freopen("in.txt","r",stdin);
} int a[N][N];
int l[N][N],r[N][N],u[N][N],d[N][N];
stack<int>st;
LL ans;
int n,m;
void calc(){
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
int L=j-l[i][j],R=r[i][j]-j,U=i-u[i][j],D=d[i][j]-i;
LL tem=(LL)L*R*U*D*(L+R)*(U+D)/;
ans=(ans+(LL)a[i][j]*tem)%MOD;
}
printf("%I64d\n",ans);
} int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
scanf("%d",&a[i][j]);
}
}
while(!st.empty()) st.pop();
for(int i=;i<=n;i++){
a[i][]=a[i][m+]=-inf;
while(!st.empty()) st.pop();st.push();
for(int j=;j<=m;j++){
while(!st.empty()&&a[i][st.top()]>a[i][j]) st.pop();
l[i][j]=st.top();
st.push(j);
}
while(!st.empty()) st.pop();st.push(m+);
for(int j=m;j>=;j--){
while(!st.empty()&&a[i][st.top()]>a[i][j]) st.pop();
r[i][j]=st.top();
st.push(j);
}
}
for(int j=;j<=m;j++){
a[][j]=a[n+][j]=inf;
while(!st.empty()) st.pop();st.push();
for(int i=;i<=n;i++){
while(!st.empty()&&a[st.top()][j]<a[i][j]) st.pop();
u[i][j]=st.top();
st.push(i);
}
while(!st.empty()) st.pop();st.push(n+);
for(int i=n;i>=;i--){
while(!st.empty()&&a[st.top()][j]<a[i][j]) st.pop();
d[i][j]=st.top();
st.push(i);
}
}
ans=;
calc();
}
return ;
}
HDU5479 Colmerauer 单调栈+暴力优化的更多相关文章
- hdu-5749 Colmerauer(单调栈)
题目链接: Colmerauer Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Oth ...
- HDU 5749 Colmerauer 单调队列+暴力贡献
BestCoder Round #84 1003 分析:(先奉上zimpha巨官方题解) 感悟:看到题解单调队列,秒懂如何处理每个点的范围,但是题解的一句算贡献让我纠结半天 已知一个点的up,do ...
- 2019南昌邀请赛网络预选赛 I. Max answer(单调栈+暴力??)
传送门 题意: 给你你一序列 a,共 n 个元素,求最大的F(l,r): F(l,r) = (a[l]+a[l+1]+.....+a[r])*min(l,r); ([l,r]的区间和*区间最小值,F( ...
- Luogu2422 | 良好的感觉 (单调栈)
题目描述 kkk做了一个人体感觉分析器.每一天,人都有一个感受值Ai,Ai越大,表示人感觉越舒适.在一段时间[i, j]内,人的舒适程度定义为[i, j]中最不舒服的那一天的感受值 * [i, j]中 ...
- 「面向 offer 学算法」笔面试大杀器 -- 单调栈
目录 前言 单调栈 初入茅庐 小试牛刀 打怪升级 出师试炼 前言 单调栈是一种比较简单的数据结构.虽然简单,但在某些题目中能发挥很好的作用. 最近很多大厂的笔试.面试中都出现了单调栈的题目,而还有不少 ...
- POJ 2796:Feel Good(单调栈)
http://poj.org/problem?id=2796 题意:给出n个数,问一个区间里面最小的元素*这个区间元素的和的最大值是多少. 思路:只想到了O(n^2)的做法. 参考了http://ww ...
- 牛客小白月赛13-H(单调栈+树状数组)
题目链接:https://ac.nowcoder.com/acm/contest/549/H 题意:给一个柱状图,包括每个矩阵的宽度和高度,求能组成的最大矩阵的面积. 思路:显然最大矩阵的高一定为n个 ...
- 【P2422】良好的感觉(单调栈优化DP//奇怪的暴力)
话说正解是单调栈优化DP,然而貌似根据某种玄学的推算,这个题暴力出解貌似也是可以的.首先,我们枚举所有的点作为最小点,然后横向展开,遇到更小的就停止...然后再操作一下,看上去时间O(N^2),然而由 ...
- BZOJ1767/Gym207383I CEOI2009 Harbingers 斜率优化、可持久化单调栈、二分
传送门--BZOJCH 传送门--VJ 注:本题在BZOJ上是权限题,在Gym里面也不能直接看,所以只能在VJ上交了-- 不难考虑到这是一个\(dp\). 设\(dep_x\)表示\(x\)在树上的带 ...
随机推荐
- Android:AlertDialog对话框
1.简单的ALertDialog: Dialog alertDialog = new AlertDialog.Builder(this) .setTitle("标题") .setM ...
- 259. 3Sum Smaller
题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...
- java中文排序
对中文名称进行排序,不多说,上代码 package test; /** * @Title: Person.java * @Copyright: Copyright (c) 2012-11-19 * @ ...
- ios7 webapp touch bug
// ios7 touchstart bug if(navigator.userAgent.indexOf("iPhone OS 7") != -1){ var startX = ...
- ASCII 字符代码表
- Android开发之创建App Widget和更新Widget内容
App WidgetsApp Widgets are miniature application views that can be embedded in other applications (s ...
- Webbrowser模拟百度一下子点击事件
Webbrowser模拟百度一下点击事件新建一个form,有一个button和一个webbrowser控件.然后webbrowser一开始加载的就是百度主页.然后在文本框里输入点东西,如何做到点击bu ...
- next_permutation()函数 和 prev_permutation() 按字典序求全排列
next_permutation功能: 求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm> 与之完全相反的函数还有prev_permutation 这个 ...
- 栈中的push实现
- [转] Jquery滚动加载
原文地址:http://hi.baidu.com/vipxiaofan/item/9eb927b795671f77254b0985 另外一个asp.net的例子:http://blog.csdn.ne ...