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. javascript设计模式(张容铭)学习笔记 - 照猫画虎-模板方法模式

    模板方法模式(Template Method):父类中定义一组操作算法骨架,而降一些实现步骤延迟到子类中,使得子类可以不改变父类的算法结构的同时可重新定义算法中某些实现步骤. 项目经理体验了各个页面的 ...

  2. noip_最后一遍_2-图论部分

    大体按照 数学 图论 dp 数据结构 这样的顺序 模板集 这个真的只有模板了……………… ·spfa #include<bits/stdc++.h> using namespace std ...

  3. centOS下SVN安装和配置

    1>SVN服务器端文件可见问题 在平时使用SVN时候,一直以为在客户提交文件,那么在服务器对应的版本库下面就会有相同文件.在自己搭建后,发现提交到服务器端文件完全看不见.... 这是由于SVN服 ...

  4. RN原生的安卓UI组件

    https://facebook.github.io/react-native/docs/native-components-android.html 这里有一大堆的原生组件可以用,一些是平台自带的, ...

  5. C/SV/VERILOG语句块界定符不一样

    C是一对大括号{} SV /VERILOG 是begin...end

  6. 强制停止及删除(卸载)Windows服务

    1. 安装服务: CMD 打开命令行窗口:C:\> 运行:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe& ...

  7. ORACLE 查询所有用户调度作业

    --查询所有用户调度作业:select * from ALL_SCHEDULER_JOBS; --查询当前用户调度作业:select * from USER_SCHEDULER_JOBS; --查询当 ...

  8. Hibernate框架的主键生成策略

    在Hibernate中,id元素的<generator>子元素用于生成持久化类的对象的唯一标识符,也就是主键.Hibernate框架中定义了许多主键生成策略类,也叫生成器类.所有的生成器类 ...

  9. Hive中文注释乱码解决方案(2)

    本文来自网易云社区 作者:王潘安 执行阶段 launchTask    回到Driver类的runInternal方法,看以下执行过程.在runInternal方法中,执行过程调用了execute方法 ...

  10. 【MVC 1】MVC+EF实体框架—原理解析

    导读:在之前,我们学过了三层框架,即:UI.BLL.DAL.我们将页面显示.逻辑处理和数据访问进行分层,避免了一层.两层的混乱.而后,我们又在经典三层的基础上,应用设计模式:外观.抽象工厂+反射,使得 ...