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. numpy中tile的用法

    a=arange(1,3) #a的结果是: array([1,2]) 1,当 tile(a,1) 时: tile(a,1) #结果是 array([1,2]) tile(a,2) #结果是 array ...

  2. ghci对haskell的类型推导

    今天这篇文章分析一下ghci交互解释器对类型的推导. 假设有函数fn定义如下: let fn = map map 现在fn的类型是: map map :: [a -> b] -> [[a] ...

  3. mysql 备份和还原

    1.使用mysqldump命令 备份:mysqldump -u username -p dbname table1 table2 ...> BackupName.sql 还原:mysql -u ...

  4. bzoj1969 [Ahoi2005]LANE 航线规划 树链剖分

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=1969 题解 如果我们把整个图边双联通地缩点,那么最终会形成一棵树的样子. 那么在这棵树上,\( ...

  5. Task3.特征选择

    参考:https://www.jianshu.com/p/f3b92124cd2b 互信息 衡量两个随机变量之间的相关性,两个随机变量相关信息的多少. 随机变量就是随机试验结果的量的表示,可以理解为按 ...

  6. LeetCode--045--跳跃游戏II(java)

    给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [2,3,1,1,4] 输 ...

  7. pip安装包出现timeout的解决办法

    今天安装django时老是出现timeout WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, sta ...

  8. Flink 实战:如何解决生产环境中的技术难题?

    大数据作为未来技术的基石已成为国家基础性战略资源,挖掘数据无穷潜力,将算力推至极致是整个社会面临的挑战与难题. Apache Flink 作为业界公认为最好的流计算引擎,不仅仅局限于做流处理,而是一套 ...

  9. CoreData新增字段

    1. 在模型文件的Entity里面增加字段名 2. Xcode工具栏选择Edtor->Creat NSManagerObject SubClass->...->生成新的关联文件 3. ...

  10. 监听UDP端口

    功能:监听服务器的UDP端口,输出端口接收的数据 #encoding:utf-8 import socket global udp global ip global port def listen_p ...