uva 11983 Weird Advertisement 扫描线
Weird Advertisement
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=18802
Description
2DPlaneLand is a land just like a huge 2D plane. The range of X axis is 0 to 109 and the range of
Y axis is also 0 to 109
. People built houses only in integer co-ordinates and there is exactly one house
in each integer co-ordinate.
Now UseAndSmile Soap Company is launching a new soap. That's why they want to advertise
this product as much as possible. So, they selected n persons for this task. Each person will be given
a rectangular region. He will advertise the product to all the houses that lie in his region. Each
rectangular region is identied by 4 integers x1, y1, x2 and y2. That means this person will advertise
in all the houses whose x co-ordinate is between x1 and x2 (inclusive) and y co-ordinate is between y1
and y2 (inclusive).
Now after a while they realized that some houses are being advertised by more than one person.
So, they want to nd the number of houses that are advertised by at least k persons. Since you are
one of the best programmers in the city; they asked you to solve this problem.
Input
Input starts with an integer T ( 13), denoting the number of test cases.
Each case starts with a line containing two integers n (1 n 30000), k (1 k 10). Each of the
next n lines will contain 4 integers x1, y1, x2, y2 (0 x1; y1; x2; y2 109
, x1 < x2, y1 < y2) denoting a
rectangular region for a person.
Output
For each case, print the case number and the total number of houses that are advertised by at least k
people.
Renat Mullakhanov (rem. See http://www.topcoder.com/tc?module=MemberProle.cr=8394868),
one of the most talented programmers in the world, passed away on March 11, 2011. This is very
sad news for all of us. His team went to ACM ICPC World Finals - 2004, placed 4th and won gold
medals. He really was a great programmer. May he rest in peace. This problem is dedicated to him.
Sample Input
2
2 1
0 0 4 4
1 1 2 5
2 2
0 0 4 4
1 1 2 5
Sample Output
Case 1: 27
Case 2: 8
HINT
题意
给你n个矩形,然后问你整个平面上被覆盖k次及以上的面积总和是多少
题解:
扫描线,我们线段树记录一下sum[k]表示区间被覆盖k次的长度是多少
先离散化,然后按照y轴建树
然后用x轴扫过去
每次线段树区间更新就好了
代码:
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
#define maxn 5000000
struct line
{
int x,y1,y2,flag;
};
bool cmp(line A,line B)
{
return A.x<B.x;
}
int n,k;
typedef long long SgTreeDataType;
struct treenode
{
int L , R ;
SgTreeDataType sum[] , lazy;
void init()
{
memset(sum,,sizeof(sum));
}
};
map<int,int> H;
vector<int> Y;
treenode tree[maxn];
line p[maxn];
int tot = ;
long long ans;
inline void push_up(int o)
{
tree[o].init();
if(tree[o].L==tree[o].R)
{
int T = min(tree[o].lazy,k*1LL);
tree[o].sum[T]=Y[tree[o].R]-Y[tree[o].L-];
}
else
{
for(int i=;i<=k;i++)
{
int T = min(i+tree[o].lazy,k*1LL);
tree[o].sum[T]+=tree[o*].sum[i]+tree[o*+].sum[i];
}
}
} inline void build_tree(int L , int R , int o)
{
tree[o].L = L , tree[o].R = R, tree[o].lazy = ;
tree[o].init();
if (R > L)
{
int mid = (L+R) >> ;
build_tree(L,mid,o*);
build_tree(mid+,R,o*+);
}
push_up(o);
} inline void updata(int QL,int QR,SgTreeDataType v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) tree[o].lazy+=v;
else
{
//push_down(o);
int mid = (L+R)>>;
if (QL <= mid) updata(QL,QR,v,o*);
if (QR > mid) updata(QL,QR,v,o*+);
}
push_up(o);
} int main()
{
int t;scanf("%d",&t);
for(int i=;i<=t;i++)
{
tot = ;ans=;
memset(p,,sizeof(p));
Y.clear();H.clear();
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
p[tot].x = x1,p[tot].y1=y1,p[tot].y2=y2+,p[tot].flag=;tot++;
p[tot].x = x2+,p[tot].y1=y1,p[tot].y2=y2+,p[tot].flag=-;tot++;
Y.push_back(y1);
Y.push_back(y2+);
}
sort(p,p+tot,cmp);
sort(Y.begin(),Y.end());
Y.erase(unique(Y.begin(),Y.end()),Y.end());
for(int i=;i<Y.size();i++)
H[Y[i]]=i+;
build_tree(,Y.size()+,);
for(int i=;i<tot-;i++)
{
updata(H[p[i].y1],H[p[i].y2]-,p[i].flag,);
ans+=tree[].sum[k]*(p[i+].x-p[i].x);
//cout<<p[i].y1<<" "<<p[i].y2<<" "<<p[i].flag<<endl;
//cout<<tree[1].sum[k]<<" "<<(p[i+1].x-p[i].x)<<endl;
}
printf("Case %d: %lld\n",i,ans);
}
}
uva 11983 Weird Advertisement 扫描线的更多相关文章
- UVA 11983 Weird Advertisement(线段树求矩形并的面积)
UVA 11983 题目大意是说给你N个矩形,让你求被覆盖k次以上的点的总个数(x,y<1e9) 首先这个题有一个转化,吧每个矩形的x2,y2+1这样就转化为了求N个矩形被覆盖k次以上的区域的面 ...
- UVA 11983 Weird Advertisement
题意:求矩形覆盖k次以上的区域总面积. 因为k≤10,可以在线段树上维护覆盖次数为0,...,k, ≥k的长度数量. 然后就是一个离散化以后扫描线的问题了. 离散化用的是半开半闭区间,以方便表示没有被 ...
- UVA 11983 Weird Advertisement --线段树求矩形问题
题意:给出n个矩形,求矩形中被覆盖K次以上的面积的和. 解法:整体与求矩形面积并差不多,不过在更新pushup改变len的时候,要有一层循环,来更新tree[rt].len[i],其中tree[rt] ...
- UVA11983 - Weird Advertisement(扫描线)
UVA11983 - Weird Advertisement(扫描线) 题目链接 题目大意:给你n个覆盖矩形,问哪些整数点是被覆盖了k次. 题目大意:这题和hdu1542是一个题型.可是这题求的是覆盖 ...
- 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)
转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnl ...
- [转载]完全版线段树 by notonlysuccess大牛
原文出处:http://www.notonlysuccess.com/ (好像现在这个博客已经挂掉了,在网上找到的全部都是转载) 今天在清北学堂听课,听到了一些很令人吃惊的消息.至于这消息具体是啥,等 ...
- 【转】线段树完全版~by NotOnlySuccess
线段树完全版 ~by NotOnlySuccess 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章了,觉 ...
- 《完全版线段树》——notonlysuccess
转载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文 ...
- 【转】 线段树完全版 ~by NotOnlySuccess
载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章 ...
随机推荐
- IE下JS接受ActiveX控件方法
1.常规写法 <SCRIPT type="text/javascript" FOR="activexID" EVENT="onXXXevent( ...
- 漂亮灵活设置的jquery通知提示插件toastr
toastr是一款非常棒的基于jquery库的非阻塞通知提示插件,toastr可设定四种通知模式:成功,出错,警告,提示,而提示窗口的位置,动画效果都可以通过能数来设置,在官方站可以通过勾选参数来生成 ...
- Eclipse “Invalid Project Description” when creating new project from existing source
1) File>Import>General>Existing Project into Workspace2) File>Import>Android>Exist ...
- Apache benchmark对网站进行压力测试
Apache Benchmark下载:http://down.tech.sina.com.cn/page/3132.html ab 的全称是 ApacheBench , 是 Apache 附带的一个小 ...
- CSS_网站配色参考方案
http://www.cnblogs.com/QLeelulu/archive/2008/04/04/1136974.html Shiny silver [#EEEEEE] Reddi ...
- Linux中MySQL5.5解压版普通用户安装
#查看本机mysql 安装路径 [hadoop@SY-0134 toolkit]$ rpm -qa|grep -i mysql [hadoop@SY-0134 toolkit]$ whereis my ...
- 虚拟化技术对比:Xen vs KVM
恒天云:http://www.hengtianyun.com/download-show-id-68.html 一.说明 本文主要从功能方面和性能方面对Xen和KVM对比分析,分析出其优缺点指导我们恒 ...
- Hadoop 2.6.0 POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 2048-AI程序算法分析
转自:CodingLabs 针对目前火爆的2048游戏,有人实现了一个AI程序,可以以较大概率(高于90%)赢得游戏,并且作者在stackoverflow上简要介绍了AI的算法框架和实现思路.但是这个 ...
- JDBC学习笔记(2)——Statement和ResultSet
Statement执行更新操作 Statement:Statement 是 Java 执行数据库操作的一个重要方法,用于在已经建立数据库连接的基础上,向数据库发送要执行的SQL语句.Statement ...