https://codeforc.es/contest/1195/problem/E

一个能运行但是会T的版本,因为本质上还是\(O(nmab)\)的算法。每次\(O(ab)\)初始化矩阵中的可能有用的点,然后\(O(n-a)\)往下推。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll; #define ERR(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string> it) {cerr << "\n";}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << "=" << a << ", ";
err(++it, args...);
} #define ERR1(arg,n) { cerr<<""<<#arg<<"=\n "; for(int i=1;i<=n;i++) cerr<<arg[i]<<" "; cerr<<"\n"; }
#define ERR2(arg,n,m) { cerr<<""<<#arg<<"=\n"; for(int i=1;i<=n;i++) { cerr<<" "; for(int j=1;j<=m;j++)cerr<<arg[i][j]<<" "; cerr<<"\n"; } } int n, m, a, b;
int x, y, z; int g[3000 * 3000 + 5];
int h[3005][3005]; struct node {
int v, x;
node(int vv, int xx) {
v = vv;
x = xx;
}
}; int curx, cury; deque<node> dq; void calc(int x, int y) {
curx = x, cury = y;
while(!dq.empty())
dq.pop_back();
for(int i = 1; i <= a; i++) {
int minline = h[x + i - 1][y];
for(int j = 2; j <= b; j++) {
minline = min(minline, h[x + i - 1][y + j - 1]);
}
while(!dq.empty() && minline <= dq.back().v) {
dq.pop_back();
}
if(dq.empty() || minline > dq.back().v) {
dq.push_back(node(minline, x + i - 1));
}
}
} void move_to_nextline() {
curx++;
if(dq.front().x < curx)
dq.pop_front();
int minline = h[curx + a - 1][cury];
for(int j = 2; j <= b; j++) {
minline = min(minline, h[curx + a - 1][cury + j - 1]);
}
while(!dq.empty() && minline <= dq.back().v) {
dq.pop_back();
}
if(dq.empty() || minline > dq.back().v) {
dq.push_back(node(minline, curx + a - 1));
}
} ll ans = 0; int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
//freopen("Yinku.out", "w", stdout);
#endif // Yinku
while(~scanf("%d%d%d%d", &n, &m, &a, &b)) {
scanf("%d%d%d%d", &g[1], &x, &y, &z);
for(int i = 2; i <= n * m; i++)
g[i] = (1ll * g[i - 1] * x % z + y) % z;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
h[i][j] = g[(i - 1) * m + j];
//ERR2(h, n, m);
ans = 0;
for(int i = 1; i + a - 1 <= n; i++) {
for(int j = 1; j + b - 1 <= m; j++) {
calc(i, j);
ans += dq.front().v;
for(int di = 1; di <= a; di++) {
move_to_nextline();
}
//ERR(ans);
}
}
printf("%lld\n", ans);
}
}

其实不需要重复计算这么多的单调队列。

具体的思路是这样:

先把左侧的n行b列插入各行的单调队列dq[i],然后取出各个队列的队首竖着组成单调队列dq2,这个单调队列dq2就可以回答左侧n行b列的所有的最小值,复杂度O(nb)。

向右移动n个dq[i],再回答左侧n行,[2,b+1]列的所有的最小值,复杂度O(n)。

总体复杂度O(nm)。

先用STL写了一个

#include<bits/stdc++.h>
using namespace std;
typedef long long ll; namespace Debug {
#define ERR(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string> it) {cerr << "\n";}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << "=" << a << ", ";
err(++it, args...);
} #define ERR1(arg,n) { cerr<<""<<#arg<<"=\n "; for(int i=1;i<=n;i++) cerr<<arg[i]<<" "; cerr<<"\n"; }
#define ERR2(arg,n,m) { cerr<<""<<#arg<<"=\n"; for(int i=1;i<=n;i++) { cerr<<" "; for(int j=1;j<=m;j++)cerr<<arg[i][j]<<" "; cerr<<"\n"; } }
} int n, m, a, b;
int x, y, z;
int g[3005 * 3005];
int h[3005][3005]; ll ans; struct Node {
int val;
int id;
Node() {}
Node(int val, int id): val(val), id(id) {} friend bool operator>=(const Node& n, const int& v) {
return n.val >= v;
}
friend bool operator>=(const Node& n1, const Node& n2) {
return n1.val >= n2.val;
}
}; deque<Node> dq[3005];
void init_deque(int i) {
dq[i].clear();
for(int j = 1; j <= b; j++) {
while(!dq[i].empty() && dq[i].back() >= h[i][j]) {
dq[i].pop_back();
}
dq[i].push_back({h[i][j], j});
}
} void move_deque(int j) {
for(int i = 1; i <= n; i++) {
if(dq[i].front().id < j - b + 1) {
dq[i].pop_front();
}
while(!dq[i].empty() && dq[i].back() >= h[i][j]) {
dq[i].pop_back();
}
dq[i].push_back({h[i][j], j});
}
} deque<Node> dq2;
void calc_ans(int oj) {
dq2.clear();
for(int i = 1; i <= a; i++) {
while(!dq2.empty() && dq2.back() >= dq[i].front())
dq2.pop_back();
dq2.push_back({dq[i].front().val, i});
}
ans += dq2.front().val;
for(int i = a + 1; i <= n; i++) {
if(dq2.front().id < i - a + 1)
dq2.pop_front();
while(!dq2.empty() && dq2.back() >= dq[i].front())
dq2.pop_back();
dq2.push_back({dq[i].front().val, i});
ans += dq2.front().val;
}
}
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
//freopen("Yinku.out", "w", stdout);
#endif // Yinku
while(~scanf("%d%d%d%d", &n, &m, &a, &b)) {
scanf("%d%d%d%d", &g[1], &x, &y, &z);
for(int i = 2; i <= n * m; i++)
g[i] = (1ll * g[i - 1] * x % z + y) % z;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
h[i][j] = g[(i - 1) * m + j];
for(int i = 1; i <= n; i++) {
init_deque(i);
}
ans = 0;
calc_ans(b);
for(int nj = b + 1; nj <= m; nj++) {
move_deque(nj);
calc_ans(nj);
}
printf("%lld\n", ans);
}
}

Codeforces - 1195E - OpenStreetMap - 单调队列的更多相关文章

  1. Codeforces 1195E OpenStreetMap 单调队列套单调队列

    题意:给你一个n * m的矩阵,问所有的a * b的子矩阵的最小的元素的和是多少.题目给出了矩阵中的数的数据生成器. 思路:如果这个问题是1维的,即求所有区间的最小元素的和,用单调队列O(n)就可以做 ...

  2. Codeforces 1195E. OpenStreetMap (单调队列)

    题意:给出一个n*m的矩形.询问矩形上所有的a*b的小矩形的最小值之和. 解法:我们先对每一行用单调栈维护c[i][j]代表从原数组的mp[i][j]到mp[i][j+b-1]的最小值(具体维护方法是 ...

  3. CodeForces 602D 【单调队列】【简单数学】

    题意: 给你n个数,m次询问,每次询问给l和r代表l和r中间所有子区间中特征值的和. 特征值的定义是在这个区间中找i和j使得|tmp[i]-tmp[j]|/|j-i|最大. 思路: 首先是特征值的定义 ...

  4. Codeforces Round #574 (Div. 2) E. OpenStreetMap 【单调队列】

    一.题目 OpenStreetMap 二.分析 对于二维空间找区间最小值,那么一维的很多好用的都无法用了,这里可以用单调队列进行查找. 先固定一个坐标,然后进行一维的单调队列操作,维护一个区间长度为$ ...

  5. CodeForces 164 B. Ancient Berland Hieroglyphs 单调队列

    B. Ancient Berland Hieroglyphs 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co ...

  6. Codeforces Round #189 (Div. 1) B. Psychos in a Line 单调队列

    B. Psychos in a Line Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/p ...

  7. Codeforces Beta Round #6 (Div. 2 Only) 单调队列

    题目链接: http://codeforces.com/contest/6/problem/E E. Exposition time limit per test 1.5 secondsmemory ...

  8. Codeforces 445A Boredom(DP+单调队列优化)

    题目链接:http://codeforces.com/problemset/problem/455/A 题目大意:有n个数,每次可以选择删除一个值为x的数,然后值为x-1,x+1的数也都会被删除,你可 ...

  9. Codeforces 1029B. Creating the Contest 动态规划O(nlogn)解法 及 单调队列O(n)解法

    题目链接:http://codeforces.com/problemset/problem/1029/B 题目大意:从数组a中选出一些数组成数组b,要求 b[i+1]<=b[i]*2 . 一开始 ...

随机推荐

  1. 自定义、操作cookie

    /** * 读取所有cookie * 注意二.从客户端读取Cookie时,包括maxAge在内的其他属性都是不可读的,也不会被提交.浏览器提交Cookie时只会提交name与value属性.maxAg ...

  2. xxPay搭建步骤

    随着电子商务的发展,移动支付也走进我们的日常生活,就像很多基础业务,如物流.云平台一样,我们现在做什么都离不开她.正因为如此,支付是当下最活跃最前沿的话题,很多流行的技术最早用来实现支付,很多新的业务 ...

  3. WEB服务动静结合

    基本介绍 1)WEB服务仅能处理静态请求,如果处理动态请求则需要对应的动态资源服务软件,即:应用程序服务软件 2)常见的应用服务软件有:PHP.Java.Python等 3)问题:WEB服务如何与外部 ...

  4. NTP时间服务器构建

    搭建一个NTP服务器,为整个网络环境中的所有主机提供时间校准服务,具体如下: - 部署一台NTP时间服务器 - 设置时间服务器上层与0.centos.pool.ntp.org同步 - 设置本地服务器层 ...

  5. SQL 1 数据库 表的操作

    数据库:是按照数据结构来组织.存储和管理数据的建立在计算机存储设备上的仓库.一句话就是存储数据的仓库 数据库的分类:网络数据库.层级数据库.关系结构数据库. 倘若按照数据库的存储介质来分:关系型数据库 ...

  6. SpringBoot application.proerties基本配置

    #设置日志输出路径,日志文件名称,输出日志级别 默认日志文件超过10M会切分成多个文件 最新的日志文件就是设置的日志文件 logging.level.root=INFOlogging.level.or ...

  7. zabbix邮件报警通过脚本来发送邮件

    zabbix默认邮件报警会将各个报警接收人单独发送邮件,为了使邮件能以群发的方式统一一封邮件发送所有接收人,需要改成脚本的形式: sendemail.py: #!/usr/bin/python imp ...

  8. 【leetcode】1028. Recover a Tree From Preorder Traversal

    题目如下: We run a preorder depth first search on the root of a binary tree. At each node in this traver ...

  9. SpringBoot整合MyBatis-Plus实现快速业务功能开发

    概览:使用MybatisPlus和它的代码生成整合SpringBoot可以实现快速的业务功能开发,具体步骤如下 一.添加依赖 <dependency> <groupId>org ...

  10. spring boot 1.视图解析器,2.开启静态资源访问

    1.spring boot 视图解析器 #视图解析器 #前缀spring.mvc.view.prefix=/pages/ #后缀..jsp.dospring.mvc.view.suffix=.jsp ...