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轴扫过去

每次线段树区间更新就好了

代码:

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<vector>
  6. #include<map>
  7. using namespace std;
  8. #define maxn 5000000
  9. struct line
  10. {
  11. int x,y1,y2,flag;
  12. };
  13. bool cmp(line A,line B)
  14. {
  15. return A.x<B.x;
  16. }
  17. int n,k;
  18. typedef long long SgTreeDataType;
  19. struct treenode
  20. {
  21. int L , R ;
  22. SgTreeDataType sum[] , lazy;
  23. void init()
  24. {
  25. memset(sum,,sizeof(sum));
  26. }
  27. };
  28. map<int,int> H;
  29. vector<int> Y;
  30. treenode tree[maxn];
  31. line p[maxn];
  32. int tot = ;
  33. long long ans;
  34. inline void push_up(int o)
  35. {
  36. tree[o].init();
  37. if(tree[o].L==tree[o].R)
  38. {
  39. int T = min(tree[o].lazy,k*1LL);
  40. tree[o].sum[T]=Y[tree[o].R]-Y[tree[o].L-];
  41. }
  42. else
  43. {
  44. for(int i=;i<=k;i++)
  45. {
  46. int T = min(i+tree[o].lazy,k*1LL);
  47. tree[o].sum[T]+=tree[o*].sum[i]+tree[o*+].sum[i];
  48. }
  49. }
  50. }
  51.  
  52. inline void build_tree(int L , int R , int o)
  53. {
  54. tree[o].L = L , tree[o].R = R, tree[o].lazy = ;
  55. tree[o].init();
  56. if (R > L)
  57. {
  58. int mid = (L+R) >> ;
  59. build_tree(L,mid,o*);
  60. build_tree(mid+,R,o*+);
  61. }
  62. push_up(o);
  63. }
  64.  
  65. inline void updata(int QL,int QR,SgTreeDataType v,int o)
  66. {
  67. int L = tree[o].L , R = tree[o].R;
  68. if (QL <= L && R <= QR) tree[o].lazy+=v;
  69. else
  70. {
  71. //push_down(o);
  72. int mid = (L+R)>>;
  73. if (QL <= mid) updata(QL,QR,v,o*);
  74. if (QR > mid) updata(QL,QR,v,o*+);
  75. }
  76. push_up(o);
  77. }
  78.  
  79. int main()
  80. {
  81. int t;scanf("%d",&t);
  82. for(int i=;i<=t;i++)
  83. {
  84. tot = ;ans=;
  85. memset(p,,sizeof(p));
  86. Y.clear();H.clear();
  87. scanf("%d%d",&n,&k);
  88. for(int i=;i<=n;i++)
  89. {
  90. int x1,y1,x2,y2;
  91. scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
  92. p[tot].x = x1,p[tot].y1=y1,p[tot].y2=y2+,p[tot].flag=;tot++;
  93. p[tot].x = x2+,p[tot].y1=y1,p[tot].y2=y2+,p[tot].flag=-;tot++;
  94. Y.push_back(y1);
  95. Y.push_back(y2+);
  96. }
  97. sort(p,p+tot,cmp);
  98. sort(Y.begin(),Y.end());
  99. Y.erase(unique(Y.begin(),Y.end()),Y.end());
  100. for(int i=;i<Y.size();i++)
  101. H[Y[i]]=i+;
  102. build_tree(,Y.size()+,);
  103. for(int i=;i<tot-;i++)
  104. {
  105. updata(H[p[i].y1],H[p[i].y2]-,p[i].flag,);
  106. ans+=tree[].sum[k]*(p[i+].x-p[i].x);
  107. //cout<<p[i].y1<<" "<<p[i].y2<<" "<<p[i].flag<<endl;
  108. //cout<<tree[1].sum[k]<<" "<<(p[i+1].x-p[i].x)<<endl;
  109. }
  110. printf("Case %d: %lld\n",i,ans);
  111. }
  112. }

uva 11983 Weird Advertisement 扫描线的更多相关文章

  1. UVA 11983 Weird Advertisement(线段树求矩形并的面积)

    UVA 11983 题目大意是说给你N个矩形,让你求被覆盖k次以上的点的总个数(x,y<1e9) 首先这个题有一个转化,吧每个矩形的x2,y2+1这样就转化为了求N个矩形被覆盖k次以上的区域的面 ...

  2. UVA 11983 Weird Advertisement

    题意:求矩形覆盖k次以上的区域总面积. 因为k≤10,可以在线段树上维护覆盖次数为0,...,k, ≥k的长度数量. 然后就是一个离散化以后扫描线的问题了. 离散化用的是半开半闭区间,以方便表示没有被 ...

  3. UVA 11983 Weird Advertisement --线段树求矩形问题

    题意:给出n个矩形,求矩形中被覆盖K次以上的面积的和. 解法:整体与求矩形面积并差不多,不过在更新pushup改变len的时候,要有一层循环,来更新tree[rt].len[i],其中tree[rt] ...

  4. UVA11983 - Weird Advertisement(扫描线)

    UVA11983 - Weird Advertisement(扫描线) 题目链接 题目大意:给你n个覆盖矩形,问哪些整数点是被覆盖了k次. 题目大意:这题和hdu1542是一个题型.可是这题求的是覆盖 ...

  5. 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)

    转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnl ...

  6. [转载]完全版线段树 by notonlysuccess大牛

    原文出处:http://www.notonlysuccess.com/ (好像现在这个博客已经挂掉了,在网上找到的全部都是转载) 今天在清北学堂听课,听到了一些很令人吃惊的消息.至于这消息具体是啥,等 ...

  7. 【转】线段树完全版~by NotOnlySuccess

    线段树完全版  ~by NotOnlySuccess 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章了,觉 ...

  8. 《完全版线段树》——notonlysuccess

    转载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文 ...

  9. 【转】 线段树完全版 ~by NotOnlySuccess

    载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章 ...

随机推荐

  1. 一、oracle 高水位线详解

    一.什么是水线(High Water Mark)? 所有的oracle段(segments,在此,为了理解方便,建议把segment作为表的一个同义词) 都有一个在段内容纳数据的上限,我们把这个上限称 ...

  2. Windows Phone 离主流系统还很远

    调查机构 Kantar Worldpanel 在本月发布全球智能手机份额报告.报告显示,五月份除德国和澳大利亚出现下滑,Windows Phone 的市场份额在不少国家都实现增长. 英国,4.1% 升 ...

  3. 字符集编码Unicode ,gb2312 cp936

    这是一篇程序员写给程序员的趣味读物.所谓趣味是指可以比较轻松地了解一些原来不清楚的概念,增进知识,类似于打RPG游戏的升级.整理这篇文章的动机是两个问题: 问题一:使用Windows记事本的“另存为” ...

  4. asp.net MVC 应用程序的生命周期(下)

    看看上面的UrlRoutingModule源码里面是怎么实现Init方法的,Init()方法里面我标注红色的地方: application.PostResolveRequestCache += new ...

  5. IOS 通知 alarm 记录

    所有的内容融为一体,去除某一个项不知道结果如何. 最主要的前提:APP 会长期保留在后台 1.在info.plist 文件里面,加入 audio 后台请求 2.当APP 点击home进入后台之后,请求 ...

  6. <转>Python 参数知识(变量前加星号的意义)

    csdn上的牛人就是多,加油 —————————————————————————— 过量的参数 在运行时知道一个函数有什么参数,通常是不可能的.另一个情况是一个函数能操作很多对象.更有甚者,调用自身的 ...

  7. CodeForce---Educational Codeforces Round 3 D. Gadgets for dollars and pounds 正题

    对于这题笔者无解,只有手抄一份正解过来了: 基本思想就是 : 二分答案,对于第x天,计算它最少的花费f(x),<=s就是可行的,这是一个单调的函数,所以可以二分. 对于f(x)的计算,我用了nl ...

  8. [Hive - LanguageManual] Describe

    Describe Describe Database Describe Table/View/Column Display Column Statistics Describe Partition D ...

  9. ACM竞赛 Java编程小结

    1.字符串的长度 String str = new String(" abcd"); int length = str.length(); 2.数组的长度.排序 2.1对于 a[] ...

  10. CDH版HDFS Block Balancer方法

    命令: sudo -u hdfs hdfs balancer 默认会检查每个datanode的磁盘使用情况,对磁盘使用超过整个集群10%的datanode移动block到其他datanode达到均衡作 ...