Codeforces Round #574 (Div. 2) : E - OpenStreetMap(单调队列维护矩形滑块最小值)

题目大意:给你一个 n ∗ m n * m nm的矩阵,让你求里面所有的 a ∗ b a * b ab的子矩阵的最小值的和。

题解:实际上就要维护矩形滑块的最小值,最后把每个起点的滑块内的答案加起来,维护线性序列内滑块的最小值可以用单调队列,维护矩形滑块的最小值是单独队列的变形。

对于每一个滑块可以用a个单调队列维护a行最小值,再用一个单调队列维护这些最小值的最小值,这个最小值就是一个起点的答案,然后移动滑块维护单调队列。滑块水平移动时没有什么问题,但当滑块要换一行时,会发现许多行已经处理过了,这里又要重复处理,相当于每一行的数据要在单调队列里维护两次。

还可以优化,为了让每一行只处理一遍,可以每次维护n个滑块大小为 a a a的行单调队列,每次就有n行的最小值,在这些最小值的基础上在维护一个滑块大小为 b b b列单调队列,用来维护这些行最小值的最小值,显然这个列单调队列里的最小值就是矩形滑块内的最小值。行滑块每移动一个单位时,将列滑块从上移动到下,最后可以得出每一个起点滑块的最小值

#include<stdio.h>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn = 3e3 + 10;
int n,m,a,b;
int h[maxn][maxn];
ll g[maxn * maxn],x,y,z;
struct ss{
	int v,x,y;
	ss(int vi = 0,int xi = 0,int yi = 0) {
		v = vi;
		x = xi;
		y = yi;
	}
	bool operator < (const ss & rhs) {
		return v < rhs.v;
	}
};
ss row[maxn][maxn];
int top[maxn],rear[maxn];	
int ans[maxn][maxn];
ss col[maxn];
int ctop,crear;
ll res;
int main() {
	scanf("%d%d%d%d",&n,&m,&a,&b);
	scanf("%lld%lld%lld%lld",&g[0],&x,&y,&z);
	for(int i = 1; i <= (n + 1) * m; i++)
	 	g[i] = (g[i - 1] * x + y) % z;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			h[i][j] = g[(i - 1) * m + j - 1];
	int cur = 1;
	for(int i = 1; i <= m; i++) {
		ctop = crear = 0;
		for(int j = 1; j <= n; j++) {
			while(top[j] > rear[j] && row[j][top[j]].v >= h[j][i]) top[j]--;
			row[j][++top[j]] = ss(h[j][i],j,i);
			if(i >= b)
				while(rear[j] < top[j] && row[j][rear[j] + 1].y < (i - b + 1)) rear[j]++;
		}
		if(i >= b) {
			for(int j = 1; j <= n; j++) {
				while(ctop > crear && col[ctop].v >= row[j][rear[j] + 1].v) ctop--;
				col[++ctop] = row[j][rear[j] + 1];
				if(j >= a)
					while(crear < ctop && col[crear + 1].x < j - a + 1) crear++;
				if(j >= a)
					ans[j - a + 1][i - b + 1] = col[crear + 1].v;
			}
		}
	}
	for(int i = 1; i <= n - a + 1; i++)
		for(int j = 1; j <= m - b + 1; j++)
			res += ans[i][j];
	printf("%lld\n",res);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值