Gym - 100570C: Subrect Query (巧妙的单调队列)
De Prezer loves rectangles.He has a n × m rectangle which there is a number in each of its cells. We show the number in the j - th column of the i - th row by ai, j.
De Prezer also loves query. So he gives you q queries. In each query, he gives you number k and asks you to print the number of subrectangles of this rectangle that the difference between the maximum element and the minimum element in them is at most k .
Input
The first line of input contains 3 integers, n, m and q .
In the next n lines, there are informations of the rectangle. i - th line among them, contains m space separated integers, ai, 1, ai, 2, ..., ai, m .
The next q lines, each line contains a single integer k (for that query).
1 ≤ n, m ≤ 400
1 ≤ q ≤ 10
1 ≤ ai, j ≤ 109 (for each 1 ≤ i ≤ n and 1 ≤ j ≤ m)
0 ≤ k ≤ 109 (for each query)
Output
For each query, print the answer in a single line.
Examples
5 4 6
451 451 452 452
452 452 452 452
451 452 450 450
451 451 451 451
452 452 450 450
0
2
773726
724963313
1
1
42
150
150
150
88
88
4 5 8
1314 1287 1286 1290 1295
1278 1271 1324 1317 1289
1305 1305 1284 1300 1309
1318 1296 1301 1274 1315
976296835
12
13
38
16
40
665711658
35
150
34
35
82
37
92
150
77
题意:给定矩阵,问有多少个非空子矩阵满足矩阵中最大值-最小值<=K。
思路:不难想到要压缩矩阵,即枚举枚举矩阵的上边界和下边界,然后压缩,看成一维的,如果可以线性解决一维的,则单次询问的复杂度为O(N^2*M)。
那么现在给定数组a[],我们用一个单增队列保存最小值的位置,用一个单减队列保存最大值的位置。对于R,如果二队首对应的值之差>K,则L++,并且维护队首>=L;
(主要是两个单调队列,我们移动的不是队首,而且L,在L>队首时,弹出队首。妙啊。VJ一血?
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
using namespace std;
const int maxn=;
const int inf=<<;
int a[maxn][maxn],Mx[maxn],Mn[maxn],q[maxn][],N,M,Q;
void solve(int K)
{
ll res=;
rep(i,,N){
rep(p,,M) Mx[p]=-inf,Mn[p]=inf;
rep(j,i,N){
rep(p,,M) Mx[p]=max(Mx[p],a[j][p]);
rep(p,,M) Mn[p]=min(Mn[p],a[j][p]);
int l=,h0=,t0=,h1=,t1=;
rep(r,,M){
while(t0<=h0&&Mx[r]>=Mx[q[h0][]]) h0--;
while(t1<=h1&&Mn[r]<=Mn[q[h1][]]) h1--;
q[++h0][]=r; q[++h1][]=r;
while(l<=r&&Mx[q[t0][]]-Mn[q[t1][]]>K){
l++; //移动得很巧妙。
if(q[t0][]<l) t0++;
if(q[t1][]<l) t1++;
}
res+=r-l+;
}
}
}
printf("%I64d\n",res);
}
int main()
{
scanf("%d%d%d",&N,&M,&Q);
rep(i,,N) rep(j,,M) scanf("%d",&a[i][j]);
while(Q--){
int k; scanf("%d",&k);
solve(k);
}
return ;
}
Gym - 100570C: Subrect Query (巧妙的单调队列)的更多相关文章
- Gym 100342E Minima (暴力,单调队列)
3e7暴力,800ms+过,单调队列维护区间最小值. #include<bits/stdc++.h> using namespace std; typedef long long ll; ...
- Gym 100712L Alternating Strings II(单调队列)
题目链接 Alternating Strings II 题意是指给出一个长度为n的01串,和一个整数k,要求将这个01串划分为很多子串(切很多刀),使得每个子串长度不超过k,且每个字串不是01交替出现 ...
- 单调队列 + 组合数统计 Gym 101102D
题目链接:http://codeforces.com/gym/101102/problem/D 题目大意:给你一个n*m的矩阵,矩阵里面的数值范围为[1,1e9].这个矩阵有一个值,如果相邻的多个数字 ...
- Gym 100801 J. Journey to the “The World’s Start” DP+单调队列优化+二分
http://codeforces.com/gym/100801 题目大意:有从左到右有n个车站,有n-1种车票,第i种车票一次最多可以坐 i 站(1<=i<=n) 每种票有固定的价钱 ...
- Gym 100801J Journey to the "The World's Start"(二分+单调队列)
题意: 现在有1,2,3...N这N个站, 给定限定时间Limt, N-1种票的价格, 分别对应一个最远距离, 叫你选择一种票, 满足可以在规定时间到达N站台,而且价格最低 思路: 如果买距离为L ...
- Gym - 101234J Zero Game (单调队列)
题意:有一个长度为n的01序列,你可以移动k次,每次将一个数移到任意一个位置,求经过操作后区间连续最大的连续0的个数. “移动”操作看似情况很复杂,不好讨论,但其实无非就两种情况: 一.移动的是1:显 ...
- 【BZOJ-2892&1171】强袭作战&大sz的游戏 权值线段树+单调队列+标记永久化+DP
2892: 强袭作战 Time Limit: 50 Sec Memory Limit: 512 MBSubmit: 45 Solved: 30[Submit][Status][Discuss] D ...
- HDU 4123(树的直径+单调队列)
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- POJ 2823 Sliding Window (线段树/单调队列)
题目不说了,可以用线段树或者单调队列,下面附上代码. 线段树: #include <iostream> #include <stdio.h> #include <algo ...
随机推荐
- 转载 iOS js oc相互调用(JavaScriptCore) --iOS调用js
iOS js oc相互调用(JavaScriptCore) 从iOS7开始 苹果公布了JavaScriptCore.framework 它使得JS与OC的交互更加方便了. 下面我们就简单了解一下这 ...
- 【BZOJ3316】JC loves Mkk 分数规划+单调队列
[BZOJ3316]JC loves Mkk Description Input 第1行,包含三个整数.n,L,R.第2行n个数,代表a[1..n]. Output 仅1行,表示询问答案.如果答案是整 ...
- 【BZOJ2530】[Poi2011]Party (xia)构造
[BZOJ2530][Poi2011]Party Description 给定一张N(保证N是3的倍数)个节点M条边的图,并且保证该图存在一个大小至少为2N/3的团. 请输出该图的任意一个大小为N/3 ...
- asp.net mvc 反射应用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- QQpet exploratory park(DP)
QQpet exploratory park Today, more and more people begin to raise a QQpet. You can get a lot of plea ...
- [原创]将本地代码共享到github的操作步骤
将本地代码共享到github的操作步骤 本地代码目录执行如下命令,初始化为git仓库. git init 到github上新建一个仓库,假设为https://github.com/sky0014/sk ...
- AFN多文件进度下载
AFN参考资料 http://www.jianshu.com/p/c36159094e24 http://blog.cnbang.net/tech/2320/http://blog.cnbang.ne ...
- ElasticSearch(二十七)type的数据结构
1.什么是type? type,是一个index中用来区分类似的数据的,类似的数据,但是可能有不同的fields,而且有不同的属性来控制索引建立.分词器field的value,在底层的lucene中建 ...
- 如何更改CSDN博客高亮代码皮肤的样式,使博客看起来更有范(推荐)
由于本人写博客的时候,也没有配置博客的相关属性,因此贴出来的代码块都是CSDN默认的,因此代码背景色都是白色的,如下所示: 但是本人在浏览他人博客的时候,发现有些博客的代码块看起来比较有范,整个代码库 ...
- ExtASPNet web.config
[转CSDN]:http://download.csdn.net/download/mcqq123321/4607708 修改 Web.config 打开 web.config,在 configura ...