hdu5091(线段树+扫描线)

题目连接:HDU - 5091

比较少写这类问题,要多练习。。,

参考:http://www.cnblogs.com/tsw123/p/4470115.html

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #define lson l,m,rt<<1
  5 #define rson m+1,r,rt<<1|1
  6 const int maxn=40010;
  7 using namespace std;
  8
  9 struct LINE
 10 {
 11     int x,y1,y2,v;
 12     LINE(){}
 13     LINE(int x,int y1,int y2,int v):
 14         x(x),y1(y1),y2(y2),v(v){}
 15     bool operator<(const LINE&a)const
 16     {
 17         return x<a.x||x==a.x&&v>a.v;
 18     }
 19 }line[maxn];
 20
 21 int Y[maxn];//离散
 22 int ct;
 23 int n,w,h;
 24
 25 int sum[maxn<<2],add[maxn<<2];
 26
 27 void pushup(int rt)
 28 {
 29     sum[rt]=max(sum[rt<<1],sum[rt<<1|1]);
 30 }
 31 void build(int l,int r,int rt)
 32 {
 33     sum[rt]=add[rt]=0;
 34     if(l==r) return ;//
 35     int m=(l+r)>>1;
 36     build(lson);
 37     build(rson);
 38     pushup(rt);
 39 }
 40 void pushdown(int rt)
 41 {
 42     if(add[rt])
 43     {
 44         add[rt<<1]+=add[rt];
 45         add[rt<<1|1]+=add[rt];
 46         sum[rt<<1]+=add[rt];
 47         sum[rt<<1|1]+=add[rt];
 48         add[rt]=0;
 49     }
 50 }
 51 void update(int L,int R,int v,int l,int r,int rt)
 52 {
 53     if(L<=l&&r<=R)
 54     {
 55         add[rt]+=v;
 56         sum[rt]+=v;
 57         return ;//
 58     }
 59     pushdown(rt);
 60     int m=(l+r)>>1;
 61     if(L<=m) update(L,R,v,lson);
 62     if(R>m) update(L,R,v,rson);
 63     pushup(rt);
 64 }
 65 int query(int L,int R,int l,int r,int rt)
 66 {
 67     if(L<=l&&r<=R)
 68     {
 69         return sum[rt];
 70     }
 71     pushdown(rt);
 72     int m=(l+r)>>1;
 73     int ans=0;
 74     if(L<=m) ans+=query(L,R,lson);
 75     if(R>m) ans+=query(L,R,rson);
 76     return ans;
 77 }
 78 int main()
 79 {
 80     while(scanf("%d",&n)&&n!=-1)
 81     {
 82         ct=0;
 83         scanf("%d%d",&w,&h);
 84         for(int i=0;i<n;i++)
 85         {
 86             int x,y;
 87             scanf("%d%d",&x,&y);
 88             x+=20001;//
 89             y+=20001;//
 90             Y[ct]=y;
 91             line[ct++]=LINE(x,y,y+h,1);
 92             Y[ct]=y+h;
 93             line[ct++]=LINE(x+w,y,y+h,-1);
 94         }
 95         sort(Y,Y+ct);
 96         sort(line,line+ct);
 97         int num=unique(Y,Y+ct)-Y;//
 98         build(0,num-1,1);
 99         int ans=0;
100         for(int i=0;i<ct;i++)
101         {
102             int l=lower_bound(Y,Y+num,line[i].y1)-Y;//
103             int r=lower_bound(Y,Y+num,line[i].y2)-Y;//
104             update(l,r,line[i].v,0,num-1,1);
105             ans=max(ans,query(0,num-1,0,num-1,1));//此处可以省去query,直接写ans=max(ans,sum[1]);也行
106         }
107         printf("%d\n",ans);
108     }
109 }
时间: 2024-08-20 05:20:51

hdu5091(线段树+扫描线)的相关文章

【BZOJ】1382: [Baltic2001]Mars Maps (线段树+扫描线)

1382: [Baltic2001]Mars Maps Time Limit: 5 Sec  Memory Limit: 64 MB Description 给出N个矩形,N<=10000.其坐标不超过10^9.求其面积并 Input 先给出一个数字N,代表有N个矩形. 接下来N行,每行四个数,代表矩形的坐标. Output 输出面积并 Sample Input 2 10 10 20 20 15 15 25 30 Sample Output 225 本以为是傻逼题,没想到不容易啊- 线段树+扫描

BZOJ 4059 Cerc2012 Non-boring sequences 线段树+扫描线

题目大意:定义一个序列为[不无聊的]当且仅当这个序列的任意一个区间都存在一个数只出现过一次,给定一个序列,要求判断这个序列是否是[不无聊的] 定义lasti表示第i个元素上一次出现的位置(第一次出现则为0),nexti表示第i个元素下一次出现的位置(最后一次出现则为n+1),那么这个元素能成为某个区间仅出现一次的数,当且仅当这个区间的左端点在[lasti+1,i]之间,右端点在[i,nexti?1]之间 我们可以将区间的左右端点放在二维平面上,那么一个元素产生的贡献是一个矩形,我们要确定的是所有

HDU 4419 Colourful Rectangle --离散化+线段树扫描线

题意: 有三种颜色的矩形n个,不同颜色的矩形重叠会生成不同的颜色,总共有R,G,B,RG,RB,GB,RGB 7种颜色,问7种颜色每种颜色的面积. 解法: 很容易想到线段树扫描线求矩形面积并,但是如何维护每种颜色的长度着实让我伤透了脑筋.后来看了一位朋友的题解,才幡然醒悟. 开始想到了用二进制表示颜色,R用001表示,G用010表示,B用100表示.那么就可以用十进制1~7表示7种不同颜色了. 维护 cov[rt][1~3] 表示此区间内3种原色各有多少个, Len[rt][i]表示每种颜色的长

sgu316Kalevich Strikes Back(线段树+扫描线)

做法:总体想法是求出一个矩形的面积以及它所包含的矩形,然后用自己的面积减掉所包含的.主要问题是怎样求解它所包含的矩形. 因为是没有相交点的,可以利用扫描线的方法去做,类似染色,当前段如果是x色,也就是第x个矩形,那么再把他染成y颜色时,说明x包含y,而当扫到y的上边时,这一段又恢复到x色.标记一下被包含的矩形,记录所包含的矩形. 因为会有恢复染色操作,up需要时时更新,左儿子和右儿子一样颜色时就可以合并为一段. 1 ; 2 } 3 void down(int w) 4 { 5 if(s[w]!=

hdu1255 覆盖的面积 线段树-扫描线

矩形面积并 线段树-扫描线裸题 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include<math.h> 5 using namespace std; 6 const int maxm=2e3+5; 7 const double eps=1e-5; 8 9 struct seg{ 10 double x,y1,y2; 11 int c; 12 bool operator

线段树+扫描线 HDOJ 5091 Beam Cannon

题目传送门 1 /* 2 题意:给出若干个点的坐标,用一个矩形去覆盖,问最多能覆盖几个点 3 线段树+扫描线:思路是先建一棵以[y, y + h]的树,左右儿子[x, x + w] 4 以这棵树为范围,从左到右扫描,更新点数,x的+1, x+w的-1(超过矩形范围) 5 ans = 每次更新时所覆盖点数的最大值 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <iostream> 10 #includ

POJ 3277 City Horizon(线段树+扫描线+离散化)

题目地址:POJ 3277 水题..稍微处理一下然后用求面积并的方法求即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include <

[POI 2001+2014acm上海邀请赛]Gold Mine/Beam Cannon 线段树+扫描线

Description Byteman, one of the most deserving employee of The Goldmine of Byteland, is about to retire by the end of the year. The Goldmine management would like to reward him in acknowledgment of his conscientious work. As a reward Byteman may rece

hdu1542 Atlantis (线段树+扫描线+离散化)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9032    Accepted Submission(s): 3873 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

HDU 1828 Picture 线段树+扫描线

题意:给你一些矩形的左上角点的坐标和右下角点的坐标,求周长并 最显而易见的思路就是对于x轴和y轴做两次扫描线,对于负数的坐标进行离散化.每次增加的值是线段变化量的绝对值.具体写法和求面积并的差不多. #include <cstdio> #include <algorithm> #include <cstring> #include <vector> using namespace std; #define lson rt << 1 , l , m