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);
}
}

最新文章

  1. Android Canvas绘图详解(图文)
  2. CreateFeatureClass COM异常
  3. Spring Boot 环境变量读取 和 属性对象的绑定
  4. 6.1.1Linux下Socket编程
  5. 2013年7月份第1周51Aspx源码发布详情
  6. 《剑指Offer》之二维数组中的查找
  7. IO - IOUtils
  8. css 内联元素inline 行框全解
  9. 用OpenGL简单编写的一个最简单贪吃蛇游戏
  10. 用Response对象的write方法和&lt;%%&gt;及&lt;%=%&gt;输出同样效果的乘法表格
  11. os.path.exists(path) 和 os.path.lexists(path) 的区别
  12. how to write a struct to a file directly?
  13. phpcms v9文章页调用点击量方法
  14. Java课程设计——计算器
  15. TFTP通信原理
  16. Unity进阶----AssetBundle_02(加载依赖关系及网络资源)(2018/10/31)
  17. keeplived工作原理及配置
  18. 递归算法,如何把list中父子类对象递归成树
  19. JavaScript修改CSS属性的实例代码
  20. Angular面试题三

热门文章

  1. 【错误】mysql 出现 &quot;1067 - Invalid default value for &#39;UPDATE_TIME&#39; &quot; 错误提示的解决办法
  2. nodejs 更新代码自动刷新页面
  3. Read-Only Tables 只读表
  4. django之子应用中开发视图函数
  5. 配置 Kibana
  6. hdu 6045: Is Derek lying? (2017 多校第二场 1001)【找规律】
  7. MaxCompute按量计费计算任务消费监控告警
  8. Linux内核设计与实现 总结笔记(第六章)内核数据结构
  9. Linux内核设计与实现 总结笔记(第三章)进程
  10. nginx proxy大文件上传失败问题总结