Read problems statements in Mandarin Chineseand Russian.

A meteor fell on Andrew's house. That's why he decided to build a new home and chose a site where he wanted to build it. Let the whole area be a rectangular field of size NxM. Naturally, Andrew wanted to build a house on the entire area of the site. However, the Hazardous Construction Prevention Bureau did not let Andrew's plans come true. The Bureau said that some of the cells of the field are dangerous for the foundation. There are exactly K such cells.

Andrew's not a poor man. And as anyone who has money, he saves money. Therefore, he is willing to buy not the whole area, but only a stripe of the same width. The house will occupy a rectangular area in this stripe with sides parallel to the sides of the original site.

Andrew is asking for your help: for each LiHi - the lowest and the highest boundaries (inclusive), respectively - find the maximum area of ​​the house that Andrew can build on the relevant site.

Input

In the first line you are given three integers NM and K.
In the following K lines you are given different pairs of integers xiyi - the coordinates of the dangerous cells.
Next line contains an integer Q - the number of Andrew's queries
The last Q lines describe the queries: each line contains two integers L and H - the lowest and the highest boundaries.

Output

In the output file, print Q lines, where i-th line contains the answer for i-th query. If you cannot build the house, then output 0.

Constraints

  • 1 ≤ N, M ≤ 1000
  • 1 ≤ K ≤ N * M
  • 1 ≤ xi ≤ N
  • 1 ≤ yi ≤ M
  • 1 ≤ Q ≤ 106
  • 1 ≤ Li ≤ Hi ≤ N

Example

Input:
4 5 5
2 1
3 2
1 3
2 4
1 4
4
1 1
3 4
2 3
1 4 Output:
2
6
3
6 发现询问数过于多了,因为最多的行区间不过10^6,所以我们可以先预处理出所有区间的答案然后O(1)回答询问。
设f[i][j] 为第i~j行的最大矩阵面积,也就是题目所求。然后再设g[i][j]为 上边界是i,下边界是j的矩阵最宽可以是多少。
于是转移很好写 => f[i][j] = max{f[i+1][j] , f[i][j-1] , g[i][j] * (j-i+1)}.所以现在关键是怎么求 g[i][j]。
考虑单调栈求一个二维最大全1矩形的做法,我们可以扩展一下: 加入当前扫到的下边界在i,然后对于从左到右的每一个点j,它向上的高度是h[j],并且
向左右最多能延伸的位置是 L[j],R[j],这个时候我们就可以用 R[j]-L[j]+1去更新 g[i-h[j]+1 ~ i][i]。
当然不能暴力更新,打个标记之后就是O(N^2)的了,,再加上算f也是O(N^2)的,就可以愉快的A了本题了23333
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1005;
int F[maxn][maxn],G[maxn][maxn];
int n,m,Q,PX,PY,num,s[maxn],tp;
int L[maxn],R[maxn],h[maxn];
bool v[maxn][maxn]; inline int read(){
int x=0; char ch=getchar();
for(;!isdigit(ch);ch=getchar());
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
return x;
} inline void init(){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++)
if(v[i][j]) h[j]=0;
else h[j]++; s[tp=0]=0;
for(int j=1;j<=m;j++){
while(tp&&h[s[tp]]>=h[j]) tp--;
L[j]=s[tp],s[++tp]=j;
}
s[tp=0]=m+1;
for(int j=m;j;j--){
while(tp&&h[s[tp]]>=h[j]) tp--;
R[j]=s[tp],s[++tp]=j;
} for(int j=1;j<=m;j++) G[i][i-h[j]+1]=max(G[i][i-h[j]+1],R[j]-L[j]-1); for(int j=1;j<=i;j++) G[i][j]=max(G[i][j],G[i][j-1]);
} for(int i=1;i<=n;i++) F[i][i]=G[i][i]; for(int len=1;len<n;len++)
for(int i=1,j=i+len;j<=n;j++,i++) F[j][i]=max(max(F[j-1][i],F[j][i+1]),G[j][i]*(j-i+1));
} inline void solve(){
Q=read();
while(Q--){
PX=read(),PY=read();
printf("%d\n",F[PY][PX]);
}
} int main(){
scanf("%d%d%d",&n,&m,&num);
for(int i=1;i<=num;i++){
PX=read(),PY=read();
v[PX][PY]=1;
} init();
solve(); return 0;
}

  

 

CodeChef - METEORAK Meteor的更多相关文章

  1. 【译】Meteor 新手教程:在排行榜上添加新特性

    原文:http://danneu.com/posts/6-meteor-tutorial-for-fellow-noobs-adding-features-to-the-leaderboard-dem ...

  2. Using View and Data API with Meteor

    By Daniel Du I have been studying Meteor these days, and find that Meteor is really a mind-blowing f ...

  3. 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树

    3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 1288  Solved: 490 ...

  4. POJ 3669 Meteor Shower【BFS】

    POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最 ...

  5. 如何在Meteor中使用npm模块?

    首先,请在AtmosphereJs上搜索有无相关的封装包.尽量采用已有的封装包,而不是自己封装. 有两种方法在项目中使用来自npm的模块. 封装为Meteor包并在项目中添加包.使用meteor cr ...

  6. windows下Meteor+AngularJS开发的坑

    有复杂的地方我再开贴记录,这里只记录容易解决的坑. 1. windows下手工增加smart package.直接将下载下来的包扔到meteor package中.记得将文件夹名字改得和smart.j ...

  7. Meteor + node-imap(nodejs) + mailparser(nodejs) 实现完整收发邮件

    版本信息: Meteor:windows MIS安装  0.6.4 node-imap:npm指定的0.8.0版,不是默认的0.7.x版. mailparser:npm安装0.3.6 以下是记录踩到的 ...

  8. 手工给Meteor增加smart package的方法

    windows下无法装mrt(Meteor的包管理工具).不过还好smart package本身也就只是一个文件夹而已,不需要在Meteor中注册什么东西.所以直接把smart package扔到me ...

  9. Meteor+AngularJS:超快速Web开发

        为了更好地描述Meteor和AngularJS为什么值得一谈,我先从个人角度来回顾一下这三年来WEB开发的变化:     三年前,我已经开始尝试前后端分离,后端使用php的轻量业务逻辑框架.但 ...

随机推荐

  1. 下载PhantomJS

    PhantomJS新手?阅读并学习快速入门指南. 视窗 下载phantomjs-2.1.1-windows.zip(17.4 MB)并解压缩(解压缩)内容. 可执行文件phantomjs.exe已准备 ...

  2. (25)zabbix事件通知

    概述 我们前面花了大量时间去讲解item.trigger.event都是为发送报警做准备的,什么是事件通知呢?简单的说故障发生了,zabbix会发邮件或者短信给你,告诉你服务器的一些状况. 如果没有通 ...

  3. Python内置方法详解

    1. 字符串内置方法详解 为何要有字符串?相对于元组.列表等,对于唯一类型的定义,字符串具有最简单的形式. 字符串往往以变量接收,变量名. 可以查看所有的字符串的内置方法,如: 1> count ...

  4. linux时钟概念CST与UTC、以及NTP简单设置

    1.世界协调时间(Universal Time Coordinated,UTC): GPS 系统中有两种时间区分,一为UTC,另一为LT(地方时)两者的区别为时区不同,UTC就是0时区的时间,地方时为 ...

  5. python中set()函数的用法

    set顾名思义是集合,里面不能包含重复的元素,接收一个list作为参数 list1=[1,2,3,4] s=set(list1) print(s) #逐个遍历 for i in s: print(i) ...

  6. SVN如何避免冲突

    在团队开发时,必然会用到代码版本控制工具,比如SVN. 但是多人共同维护同一份代码,当对同一文件进行增删时,就可能造成冲突,如何尽可能避免冲突相当重要. 首先,每次,新建任何文档,都会修改项目文件,所 ...

  7. NYOJ 104 最大和

    最大和 时间限制:1000 ms  |  内存限制:65535 KB 难度:5   描述 给定一个由整数组成二维矩阵(r*c),现在需要找出它的一个子矩阵,使得这个子矩阵内的所有元素之和最大,并把这个 ...

  8. 【bzoj3696】化合物 树形dp

    题目描述 首长NOI惨跪,于是去念文化课了.现在,他面对一道化学题.这题的来源是因为在一个奇怪的学校两个化竞党在玩一个奇怪的博弈论游戏.这个游戏很蛋疼,我相信你们也没有兴趣听.由于这个游戏涉及博弈论, ...

  9. 【bzoj4930】棋盘 费用流

    题目描述 给定一个n×n的棋盘,棋盘上每个位置要么为空要么为障碍.定义棋盘上两个位置(x,y),(u,v)能互相攻击当前仅 当满足以下两个条件: 1:x=u或y=v 2:对于(x,y)与(u,v)之间 ...

  10. 【Luogu】P3402最长公共子序列(LCS->nlognLIS)

    题目链接 SovietPower 的题解讲的很清楚.Map或Hash映射后用nlogn求出LIS.这里只给出代码. #include<cstdio> #include<cctype& ...