Hdu--5092(扫描线)

2014-11-02 19:46:17

思路:POJ有类似的题目,是用窗户框星星。这题的话简单不少,不用离散化,不用long long。

  需要注意的是输入的坐标如果是负数要转化成正数,然后数组范围开足即可。

 1 /*************************************************************************
 2     > File Name: b.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Sun 02 Nov 2014 12:04:21 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <vector>
13 #include <map>
14 #include <set>
15 #include <stack>
16 #include <queue>
17 #include <iostream>
18 #include <algorithm>
19 using namespace std;
20 #define lp (p << 1)
21 #define rp (p << 1|1)
22 #define getmid(l,r) (l + (r - l) / 2)
23 #define MP(a,b) make_pair(a,b)
24 typedef long long ll;
25 const int INF = 1 << 30;
26 const int maxn = 20010;
27 const int Maxn = 80000;
28 
29 int N,H,W;
30 struct node{
31     int x,y;
32 }nd[maxn];
33 
34 struct seg_tree{
35     int tmax;
36     int cover;
37 }t[(Maxn << 2) + 10];
38 
39 bool cmp(node a,node b){
40     return a.y < b.y;
41 }
42 
43 void Init(){
44     memset(t,0,sizeof(t));
45 }
46 
47 void Push_down(int p){
48     if(t[p].cover){
49         t[lp].tmax += t[p].cover;
50         t[rp].tmax += t[p].cover;
51         t[lp].cover += t[p].cover;
52         t[rp].cover += t[p].cover;
53         t[p].cover = 0;
54     }
55 }
56 
57 void Update(int a,int b,int c,int p,int l,int r){
58     if(a <= l && r <= b){
59         t[p].tmax += c;
60         t[p].cover += c;
61         return;
62     }
63     Push_down(p);
64     int mid = getmid(l,r);
65     if(a <= mid) Update(a,b,c,lp,l,mid);
66     if(b > mid) Update(a,b,c,rp,mid + 1,r);
67     t[p].tmax = max(t[lp].tmax,t[rp].tmax);
68 }
69 
70 int main(){
71     while(scanf("%d",&N) != EOF){
72         if(N < 0) break;
73         scanf("%d%d",&W,&H);
74         Init();
75         for(int i = 1; i <= N; ++i){
76             scanf("%d%d",&nd[i].x,&nd[i].y);
77             nd[i].x += 20001;
78         }
79         sort(nd + 1,nd + N + 1,cmp);
80         int pos = 1,ans = 0;
81         for(int i = 1; i <= N; ++i){
82             Update(nd[i].x,nd[i].x + W,1,1,1,Maxn);
83             while(nd[i].y - nd[pos].y > H){
84                 Update(nd[pos].x,nd[pos].x + W,-1,1,1,Maxn);
85                 ++pos;
86             }
87             ans = max(ans,t[1].tmax);
88         }
89         printf("%d\n",ans);
90     }
91     return 0;
92 }

 

posted @ 2014-11-02 21:45  Naturain  阅读(134)  评论(0编辑  收藏  举报