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, nm 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

Input
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
Output
42
150
150
150
88
88
Input
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
Output
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 (巧妙的单调队列)的更多相关文章

  1. Gym 100342E Minima (暴力,单调队列)

    3e7暴力,800ms+过,单调队列维护区间最小值. #include<bits/stdc++.h> using namespace std; typedef long long ll; ...

  2. Gym 100712L Alternating Strings II(单调队列)

    题目链接 Alternating Strings II 题意是指给出一个长度为n的01串,和一个整数k,要求将这个01串划分为很多子串(切很多刀),使得每个子串长度不超过k,且每个字串不是01交替出现 ...

  3. 单调队列 + 组合数统计 Gym 101102D

    题目链接:http://codeforces.com/gym/101102/problem/D 题目大意:给你一个n*m的矩阵,矩阵里面的数值范围为[1,1e9].这个矩阵有一个值,如果相邻的多个数字 ...

  4. Gym 100801 J. Journey to the “The World’s Start” DP+单调队列优化+二分

    http://codeforces.com/gym/100801 题目大意:有从左到右有n个车站,有n-1种车票,第i种车票一次最多可以坐 i 站(1<=i<=n)   每种票有固定的价钱 ...

  5. Gym 100801J Journey to the "The World's Start"(二分+单调队列)

    题意: 现在有1,2,3...N这N个站, 给定限定时间Limt,  N-1种票的价格, 分别对应一个最远距离,  叫你选择一种票, 满足可以在规定时间到达N站台,而且价格最低 思路: 如果买距离为L ...

  6. Gym - 101234J Zero Game (单调队列)

    题意:有一个长度为n的01序列,你可以移动k次,每次将一个数移到任意一个位置,求经过操作后区间连续最大的连续0的个数. “移动”操作看似情况很复杂,不好讨论,但其实无非就两种情况: 一.移动的是1:显 ...

  7. 【BZOJ-2892&1171】强袭作战&大sz的游戏 权值线段树+单调队列+标记永久化+DP

    2892: 强袭作战 Time Limit: 50 Sec  Memory Limit: 512 MBSubmit: 45  Solved: 30[Submit][Status][Discuss] D ...

  8. HDU 4123(树的直径+单调队列)

    Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. POJ 2823 Sliding Window (线段树/单调队列)

    题目不说了,可以用线段树或者单调队列,下面附上代码. 线段树: #include <iostream> #include <stdio.h> #include <algo ...

随机推荐

  1. php单元测试标注(注解)

    @after 用于指明此方法应当在测试用例类中的每个测试方法运行完成之后调用. /** * @before1 */ public function bbb() { $this->assertTr ...

  2. os模块,sys模块

    # os模块 # os模块是与操作系统交互的一个接口 ''' 和工作目录相关: os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径(在什么地方执行就是那个文件的路径) os ...

  3. 【BZOJ2407/4398】探险/福慧双修 最短路建模

    [BZOJ2407]探险 Description 探险家小T好高兴!X国要举办一次溶洞探险比赛,获奖者将得到丰厚奖品哦!小T虽然对奖品不感兴趣,但是这个大振名声的机会当然不能错过! 比赛即将开始,工作 ...

  4. 使用tomcat7-maven-plugin部署Web项目

      一.环境准备 我使用的环境是:Window 10.Tomcat 8.0.36.maven3.tomcat7-maven-plugin 2.2版本. 二.设置环境变量 安装Tomcat8.0.36和 ...

  5. hdu 4414 Finding crosses【简单模拟】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4414 CSUST:点击打开链接 Finding crosses Time Limit: 2000/1000 ...

  6. 【python】-- web开发之jQuery

    jQuery jQuery 是一个 JavaScript 函数库,jQuery库包含以下特性(HTML 元素选取.HTML 元素操作.CSS 操作.HTML 事件函数.JavaScript 特效和动画 ...

  7. PHP开发环境搭建(转载)

    转载自:http://blog.csdn.net/rosetta/article/details/53967215 前言   最近学了n种语言,学每种语言的套路无非就是先搭建一个开发环境,再找本书或者 ...

  8. [转载] 把Nutch爬虫部署到Hadoop集群上

    http://f.dataguru.cn/thread-240156-1-1.html 软件版本:Nutch 1.7, Hadoop 1.2.1, CentOS 6.5, JDK 1.7 前面的3篇文 ...

  9. Java语言实现简单FTP软件------>辅助功能模块FTP站点管理的实现(十二)

    1.FTP站点管理 点击"FTP站点管理"按钮,弹出对话框"FTP站点管理",如下图 1) 连接站点 在FTP站点管理面板上选好要连接的站点,点击"连 ...

  10. 教你使用SQL查询(1-12)

    教你使用 Select 查询语句 (1) SELECT 语句基本语法简介 http://jimshu.blog.51cto.com/3171847/1363101(2) TOP 和 OFFSET 筛选 ...