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 ...
随机推荐
- 解决ubuntu无法进入unity模式
终端输入如下命令: 1.sudo add-apt-repository ppa:gnome3-team/gnome3 2.sudo apt-get update 3.sudo apt-get inst ...
- 在Linux先显示文件
cat:从第一行开始显示文件内容. tac:从最后一行开始显示内容. nl:显示的时候带行号. more:一页一页显示. less:同more,可以向上翻页. head:显示文件前几行. head - ...
- 打字母的游戏&Java入门
目标: 在一个窗体上随机掉落字母,通过键盘输入,敲对后消除并且累计积分,否则扣除一定积分. 具体内容: 画一个窗体——>产生随机字母——>接受键盘输入——>显示分数 代码: pack ...
- 【BZOJ4205】卡牌配对 最大流
[BZOJ4205]卡牌配对 Description 现在有一种卡牌游戏,每张卡牌上有三个属性值:A,B,C.把卡牌分为X,Y两类,分别有n1,n2张. 两张卡牌能够配对,当且仅当,存在至多一项属性值 ...
- api签名认证
参数列表: data: { sign, uid或是openId, version, timestamp, param } sign 签名一般情况下,根据如下几项生成,通过md5或是aes加密: 接口 ...
- linux c编程:信号(一)
信号是软件中断,很多比较重要的应用程序都需要处理信号.并且信号提供了一种处理异步事件的方法.如终端用户键入中断键,会通过信号机制停止一个程序,或及早终止管道中的下一个程序 很多条件都可以产生信号,比如 ...
- java访问微信接口发送消息
最近在开发activiti流程的时候有个需求:流程到达每个审批节点后,需要向该节点的审批人发送一个消息,提示有审批需要处理. 参考了一下微信的开发者文档和网络上的一些技术博客,现在记录一下.以便后续继 ...
- node-sass 安装失败的解决措施[转]
转自:http://blog.csdn.net/nzb329/article/details/51935236 在编译一个项目的时候,一直报错 后来发现是因为node-sass没有装成功, 最终的解决 ...
- rails 常用方法
bundle install --without production 不安装production中的gem ./configure && make && sudo m ...
- pandas.resample()
http://www.cnblogs.com/hhh5460/p/5596340.html resample与groupby的区别:resample:在给定的时间单位内重取样groupby:对给定的数 ...