【分段哈希】H. Paint the Wall
https://www.bnuoj.com/v3/contest_show.php?cid=9147#problem/H
【题意】
在一个长为H,宽为W的白墙上选一个矩形区域涂颜色,后涂的颜色会覆盖先涂的,题目给出n(n<=100)个矩形区域和对应的颜色。
求最后墙上每种颜色的面积是多少,共有多少种颜色。
【思路】
将墙分成一块一块的矩形区域,对每个区域判断最后涂的颜色是什么,将其面积加到对应的颜色上。关键是怎样将矩形分块:
将竖线(所有矩形的left和right)离散化,从左到右枚举相邻的竖线;
对固定的两条竖线,枚举覆盖这两条竖线的矩形,再离散化横线(所有合法矩形的top和bottom);
对当前的每个矩形区域,遍历所有的合法矩形,判断哪个是最后涂的。
时间复杂度是O(2n*2n*n)即O(n^3)。
【Accepted】
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm> using namespace std;
int H,W;
int n;
const int maxn=4e2+;
int cnt;
struct Rec
{
int T,L,B,R,c,o;
void input(int o_)
{
o=o_;
scanf("%d%d%d%d%d",&T,&L,&B,&R,&c);
}
}rec[maxn/];
int area[maxn];
vector<int> v;
int w[maxn];
int h[maxn];
void init()
{
cnt=;
memset(area,,sizeof(area));
}
int main()
{
int cas=;
while(~scanf("%d%d",&H,&W))
{
if(!H&&!W)
{
break;
}
init();
scanf("%d",&n);
for(int i=;i<=n;i++)
{
rec[i].input(i);
w[++cnt]=rec[i].L;
w[++cnt]=rec[i].R;
}
getchar();
sort(w+,w+cnt+);
cnt=unique(w+,w+cnt+)-(w+);
for(int i=;i<cnt;i++)
{
v.clear();
int tot=;
for(int k=;k<=n;k++)
{
if(rec[k].L<=w[i]&&w[i+]<=rec[k].R)
{
h[++tot]=rec[k].T;
h[++tot]=rec[k].B;
v.push_back(k);
}
}
sort(h+,h+tot+);
tot=unique(h+,h+tot+)-(h+);
int sz=v.size();
for(int k=;k<tot;k++)
{
int pos=-;
for(int j=;j<sz;j++)
{
if(rec[v[j]].T<=h[k]&&h[k+]<=rec[v[j]].B)
{
if(pos<rec[v[j]].o)
{
pos=rec[v[j]].o;
}
}
}
if(pos!=-)
{
area[rec[pos].c]+=(w[i+]-w[i])*(h[k+]-h[k]);
}
}
}
if(cas)
{
puts("");
}
printf("Case %d:\n",++cas);
int ans=;
for(int i=;i<=;i++)
{
if(area[i]>)
{
ans++;
printf("%d %d\n",i,area[i]);
}
}
if(ans==)
{
puts("There is 1 color left on the wall.");
}
else
{
printf("There are %d colors left on the wall.\n",ans);
}
}
return ;
}
【分段哈希】H. Paint the Wall的更多相关文章
- HDU 4391 Paint The Wall(分块+延迟标记)
Paint The Wall Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 线段树 扫描线 L - Atlantis HDU - 1542 M - City Horizon POJ - 3277 N - Paint the Wall HDU - 1543
学习博客推荐——线段树+扫描线(有关扫描线的理解) 我觉得要注意的几点 1 我的模板线段树的叶子节点存的都是 x[L]~x[L+1] 2 如果没有必要这个lazy 标志是可以不下传的 也就省了一个pu ...
- HDU 4391 - Paint The Wall - 分块哈希入门
题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=4391 题意 : 给一段区间, 有两种操作 1 : 给 x 到 y 的区间染色为 z 2 : 查询 ...
- 【HDU4391】【块状链表】Paint The Wall
Problem Description As a amateur artist, Xenocide loves painting the wall. The wall can be considere ...
- hdu 1543 Paint the Wall
http://acm.hdu.edu.cn/showproblem.php?pid=1543 #include <cstdio> #include <cstring> #inc ...
- ZOJ 2747 Paint the Wall(离散化+暴力)题解
题意:给你一个面,然后涂颜色,问你最后剩多少颜色,每种颜色面积. 思路:第一反应是二维线段树,代码又臭又长,可以做.但是这题暴力+离散化就可以过.可以看到他给的n只有100,也就是说最坏情况下会涂10 ...
- Paint the Wall ZOJ - 2747
点数很多,坐标值很大,然后离散化一下用一个点表示一小块的面积对应的颜色,然后更新的时候一块一块更新,查询的时候一块一块查询 #include<map> #include<set> ...
- HDU 4391 Paint The Wall 段树(水
意甲冠军: 特定n多头排列.m操作 以下是各点的颜色 以下m一种操纵: 1 l r col 染色 2 l r col 问间隔col色点 == 通的操作+区间内最大最小颜色数的优化,感觉非常不科学... ...
- HDU 4391 Paint The Wall(分块的区间维护)
题意:给出几个操作,把l-r赋值为z,询问l-r有几个z,其中z < INT_MAX 思路:因为z很大,所以很难直接用线段树去维护.这里可以使用分块来解决.我们可以让每个块用map去储存map[ ...
随机推荐
- 浅谈 echarts 用法
对于服务型的公司来说,需要了解用户的使用趋势,来帮助分析市场的走向,所以说统计在一个管理后台中是必不可少的. 会用到echarts插件 ,其官网网址 http://echarts.baidu.com/ ...
- postgresql版sde(10.4.1)安装说明
从ArcGIS 10.3开始,彻底没有了sde的安装包,安装sde数据库需要先安装arcgis desktop,通过arccatalog建数据库,同时也不能建sde服务,只能使用直连 以下演示在sde ...
- Android开发二维码之坑
之前一直做的是.NET开发用的是C#语言,近段时间由于做一个APP这才用上了java,在二维码扫描整合到APP里面遇到扫描二维码之后没有返回值,经过反复的尝试最后终于拿到了返回值,之后觉得很有必要记录 ...
- Eric's并发用户数估算与Little定律的等价性
在国内性能测试的领域有一篇几乎被奉为大牛之作的经典文章,一个名叫Eric Man Wong 于2004年发表了名为<Method for Estimating the Number of Con ...
- ETH Dapp 体验报告
Dapp 体验报告 Dapp是分散式的应用程序.DApp运行在去中心化的网络上,也就是区块链网络中.网络中不存在中心化的节点可以完整的控制DApp. 必须依赖合约部署,没有一个中心化的服务器托管. 对 ...
- Javaweb学习笔记3—Serverlet
今天来讲javaweb的第三个阶段学习. 老规矩,首先先用一张思维导图来展现今天的博客内容. ps:我的思维是用的xMind画的,如果你对我的思维导图感兴趣并且想看到你们跟详细的备注信息,请点击下载 ...
- pip install python-igraph 报错,C core of igraph 没有安装。
(一)问题描述 Centos7 安装python-igraph时,pip install python-igraph 报错,C core of igraph 没有安装. failure: repoda ...
- Keil Debug (printf) Viewer
Debug (printf) Viewer Home » µVision Windows » Debug (printf) Viewer The Debug (printf) Viewer windo ...
- Error:Failed to resolve: com.afollestad:material-dialogs:
http://www.chenruixuan.com/archives/1068.html 背景: 同事把Android项目直接考给了我...我在Android Studio上运行,然后提示: Err ...
- zabbix user-defined item
1.user-defind item at:/etc/zabbix/zabbix_agent.conf format: UserParameter=<key>,<command> ...