HDU5479 Colmerauer 单调栈+暴力优化
http://acm.hdu.edu.cn/showproblem.php?pid=5749
思路:
bestcoder 84
贡献:所有可能的子矩阵的面积和
1 //len1:子矩阵所有长的和 2 for(int i=1;i<=L;i++){ 3 for(int j=1;j<=R;j++){ 4 len1+=i+j-1;//-1是因为1*1单元格也是鞍点 5 } 6 }
1 // #pragma comment(linker, "/STACK:102c000000,102c000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #include <list> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <cstdlib> 15 #include <conio.h> 16 using namespace std; 17 #define clc(a,b) memset(a,b,sizeof(a)) 18 #define inf 0x3f3f3f3f 19 #define lson l,mid,rt<<1 20 #define rson mid+1,r,rt<<1|1 21 const int N = 1010; 22 const int M = 1e6+10; 23 const long long MOD = 1LL<<32; 24 #define LL long long 25 #define LB long double 26 #define mi() (l+r)>>1 27 double const pi = acos(-1); 28 const double eps = 1e-8; 29 void fre() { 30 freopen("in.txt","r",stdin); 31 } 32 33 int a[N][N]; 34 int l[N][N],r[N][N],u[N][N],d[N][N]; 35 stack<int>st; 36 LL ans; 37 int n,m; 38 void calc(){ 39 for(int i=1;i<=n;i++) 40 for(int j=1;j<=m;j++){ 41 int L=j-l[i][j],R=r[i][j]-j,U=i-u[i][j],D=d[i][j]-i; 42 LL tem=(LL)L*R*U*D*(L+R)*(U+D)/4; 43 ans=(ans+(LL)a[i][j]*tem)%MOD; 44 } 45 printf("%I64d\n",ans); 46 } 47 48 int main(){ 49 int T; 50 scanf("%d",&T); 51 while(T--){ 52 scanf("%d%d",&n,&m); 53 for(int i=1;i<=n;i++){ 54 for(int j=1;j<=m;j++){ 55 scanf("%d",&a[i][j]); 56 } 57 } 58 while(!st.empty()) st.pop(); 59 for(int i=1;i<=n;i++){ 60 a[i][0]=a[i][m+1]=-inf; 61 while(!st.empty()) st.pop();st.push(0); 62 for(int j=1;j<=m;j++){ 63 while(!st.empty()&&a[i][st.top()]>a[i][j]) st.pop(); 64 l[i][j]=st.top(); 65 st.push(j); 66 } 67 while(!st.empty()) st.pop();st.push(m+1); 68 for(int j=m;j>=1;j--){ 69 while(!st.empty()&&a[i][st.top()]>a[i][j]) st.pop(); 70 r[i][j]=st.top(); 71 st.push(j); 72 } 73 } 74 for(int j=1;j<=m;j++){ 75 a[0][j]=a[n+1][j]=inf; 76 while(!st.empty()) st.pop();st.push(0); 77 for(int i=1;i<=n;i++){ 78 while(!st.empty()&&a[st.top()][j]<a[i][j]) st.pop(); 79 u[i][j]=st.top(); 80 st.push(i); 81 } 82 while(!st.empty()) st.pop();st.push(n+1); 83 for(int i=n;i>=1;i--){ 84 while(!st.empty()&&a[st.top()][j]<a[i][j]) st.pop(); 85 d[i][j]=st.top(); 86 st.push(i); 87 } 88 } 89 ans=0; 90 calc(); 91 } 92 return 0; 93 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!