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. Java REST Client API

    https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.3/java-rest-high-supported-apis.htm ...

  2. 3-基于双TMS320C6678+双XC6VSX315T的6U VPX高速数据处理平台

    基于双TMS320C6678+双XC6VSX315T的6U VPX高速数据处理平台   一.板卡概述 板卡由我公司自主研发,基于VPX架构,主体芯片为两片 TI DSP TMS320C6678,两片V ...

  3. Linux telnet、nc、ping监测状态

    在工作中会遇到网络出现闪断丢包的情况,最终影响业务工常使用.可以业务服务器上发起监测. 1.通过telnet echo  -e  "\n" | telnet localhost 2 ...

  4. springboot通过继承OncePerRequestFilter,在拦截器中@Autowired 为null问题

    springboot2版本以上环境 通过继承OncePerRequestFilter类,在重写doFilterInternal方法实现拦截的具体业务逻辑, 在实现的过程中,需要注入service方法, ...

  5. php object

    一.访问控制 <?php class Computer{ public $cpu = 880; private $name = 'xiaomi'; public function getname ...

  6. macOS gcc g++ c++ cc

    安装完Xcode之后,系统中默认的编译器不再是Gcc系列,编译一些库的时候经常产生问题. 在PATH变量中设置symbol link,把gcc,g++,c++,cc全链接到Gcc系列.

  7. tensorflow函数介绍 (5)

    1.tf.ConfigProto tf.ConfigProto一般用在创建session的时候,用来对session进行参数配置: with tf.Session(config=tf.ConfigPr ...

  8. Flask学习笔记03之路由

    1. endpoint from flask import Flask, url_for # 实例化一个Flask对象 app = Flask(__name__) # 打印默认配置信息 # 引入开发环 ...

  9. BZOJ 2286: [Sdoi2011]消耗战 虚树

    Description 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战斗,我军 ...

  10. leetcode_1293. Shortest Path in a Grid with Obstacles Elimination_[dp动态规划]

    题目链接 Given a m * n grid, where each cell is either 0 (empty) or 1 (obstacle). In one step, you can m ...