CodeChef - METEORAK Meteor
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 Li, Hi - 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 N, M and K.
In the following K lines you are given different pairs of integers xi, yi - 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的更多相关文章
- 【译】Meteor 新手教程:在排行榜上添加新特性
原文:http://danneu.com/posts/6-meteor-tutorial-for-fellow-noobs-adding-features-to-the-leaderboard-dem ...
- 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 ...
- 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1288 Solved: 490 ...
- POJ 3669 Meteor Shower【BFS】
POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最 ...
- 如何在Meteor中使用npm模块?
首先,请在AtmosphereJs上搜索有无相关的封装包.尽量采用已有的封装包,而不是自己封装. 有两种方法在项目中使用来自npm的模块. 封装为Meteor包并在项目中添加包.使用meteor cr ...
- windows下Meteor+AngularJS开发的坑
有复杂的地方我再开贴记录,这里只记录容易解决的坑. 1. windows下手工增加smart package.直接将下载下来的包扔到meteor package中.记得将文件夹名字改得和smart.j ...
- 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 以下是记录踩到的 ...
- 手工给Meteor增加smart package的方法
windows下无法装mrt(Meteor的包管理工具).不过还好smart package本身也就只是一个文件夹而已,不需要在Meteor中注册什么东西.所以直接把smart package扔到me ...
- Meteor+AngularJS:超快速Web开发
为了更好地描述Meteor和AngularJS为什么值得一谈,我先从个人角度来回顾一下这三年来WEB开发的变化: 三年前,我已经开始尝试前后端分离,后端使用php的轻量业务逻辑框架.但 ...
随机推荐
- UEditor中多图上传的bug
多图上传 预览:支持浏览器版本 IE8以上 在线管理:由于存在bug,显示不了 ueditor-1.1.1.jar解压后找到FileManager 1.修改com.baidu.ueditor.hun ...
- shell脚本,计算学生分数的题目。
1.计算学生平均分数的值是多少? 2.计算每门课都大于80分的学生姓名.3.计算每门课都小于90分的学生姓名.
- Spring框架中的aop操作之一 及aspectjweaver.jar与aopalliance-1.0.jar下载地址 包含beans 注解context 和aop的约束
(aspect oriented programming面向切面编程) 首先在原有的jar包: 需Spring压缩包中的四个核心JAR包 beans .context.core 和expression ...
- Greenplum/Deepgreen(集群/分布式)安装文档
Deepgreen分布式安装文档 环境准备 1.安装VMware虚拟机软件,然后在VMware安装三台Linux虚拟机(使用centos7版本) 2.使用的虚拟机如下: 192.168.136.155 ...
- 前端css学习记录
参考资料:CSS权威指南(第三版)中文版 核心要点: HTML负责标记文档的结构(HyperText Markup Language),结构化语言. CSS 负责表现文档的样式(Cascading S ...
- GIMP的Path用法,快速选区
1/很多的快捷键,不多就Shift Ctrl Alt可以自由选择,这里自己多试几次可以熟练掌握吧. 2/快速选区作为Path,这里颜色要纯一点好:Selection to Path Step1:选择F ...
- 蓝牙学习(4) -- L2CAP
L2CAP in protocol 首先看一下L2CAP在Bluetooth protocol architecture diagram中的位置: Features of L2CAP Logical ...
- MySQL 之视图、 触发器、事务、存储过程、内置函数、流程控制、索引
本文内容: 视图 触发器 事务 存储过程 内置函数 流程控制 索引 ------------------------------------------------------------------ ...
- 使用systemctl命令管理服务mysql
启动mysql systemctl start mysqld.service 停止mysql systemctl stop mysqld.service 重启mysql systemctl resta ...
- Android ScrollView嵌套GridView导致GridView只显示一行item
Android ScrollView嵌套GridView导致GridView只显示一行item Android ScrollView在嵌套GridView时候,会导致一个问题发生:GridView只显 ...