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. group_concat函数导致的主从同步异常

    group_concat函数导致的主从同步异常的问题总结 今天在处理一个group_concat函数导致的主从异常的问题,排查过程比较简单,不过第一次遇到这个问题记录一下排查的思路,后面如果再遇到其他 ...

  2. struts2一个实例中遇到的问题

    今天实现了一个登录功能的Struts2小程序. 期间遇到了许多问题,记忆犹新的是 (1)新版本的tomcat9和eclipse Neon Release (4.6.0) 发生了冲突,启动服务器的时候老 ...

  3. GitHub 小试牛刀(踩坑记录)

    首先要在GitHub上创建好远程仓库,把README,LISCENCE,.gitignore三个文件在远程仓库初始化好. 然后在创建本地仓库,先要cd到自己的项目目录下,然后: $ git init ...

  4. J - Max Sum

    J - Max Sum Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descrip ...

  5. POJ 1068 Parencodings【水模拟--数括号】

    链接: http://poj.org/problem?id=1068 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27454#probl ...

  6. 九度OJ 1336:液晶屏裁剪 (GCD)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:983 解决:228 题目描述: 苏州某液晶厂一直生产a * b大小规格的液晶屏幕,由于该厂的加工工艺限制,液晶屏的边长都为整数.最近由于市场 ...

  7. php, tp5, 选中导航菜单

    1. 首先定义一个函数: function nav_select($navindex){ $nav_arr = [ 1 => ['index',], 2 => ['mei',], 3 =& ...

  8. vue表单输入的绑定

    vue的核心:声明式的指令和数据的双向绑定. 那么声明式的指令,已经给大家介绍完了.接下来我们来研究一下什么是数据的双向绑定? 另外,大家一定要知道vue的设计模式:MVVM M是Model的简写,V ...

  9. 程序运行之ELF文件结构

    ELF目标文件格式的最前部是ELF文件头.包含了整个文件的基本属性.比如ELF文件版本,目标机器型号,程序入口地址等.然后是ELF的各个段,其中ELF文件中与段有关的重要结构就是段表.段表描述了ELF ...

  10. HTTPS与HTTP

    HTTP HyperText Transfer Protocol超文本传输协议 HTTPS HyperText Transfer Protocol over Secure Socket Layer 基 ...