hdu5091 线段树扫描线

Recently, the γ galaxies broke out Star Wars. Each planet is warring for resources. In the Star Wars, Planet X is under attack by other planets. Now, a large wave of enemy spaceships is approaching. There is a very large Beam Cannon on the Planet X, and it is very powerful, which can destroy all the spaceships in its attack range in a second. However, it takes a long time to fill the energy of the Beam Cannon after each shot. So, you should make sure each shot can destroy the enemy spaceships as many as possible. 

To simplify the problem, the Beam Cannon can shot at any area in the space, and the attack area is rectangular. The rectangle parallels to the coordinate axes and cannot rotate. It can only move horizontally or vertically. The enemy spaceship in the space can be considered as a point projected to the attack plane. If the point is in the rectangular attack area of the Beam Cannon(including border), the spaceship will be destroyed.
Input
Input contains multiple test cases. Each test case contains three integers N(1<=N<=10000, the number of enemy spaceships), W(1<=W<=40000, the width of the Beam Cannon’s attack area), H(1<=H<=40000, the height of the Beam Cannon’s attack area) in the first line, and then N lines follow. Each line contains two integers x,y (-20000<=x,y<=20000, the coordinates of an enemy spaceship). 

A test case starting with a negative integer terminates the input and this test case should not to be processed.
Output
Output the maximum number of enemy spaceships the Beam Cannon can destroy in a single shot for each case.
Sample Input
2 3 4
0 1
1 0
3 1 1
-1 0
0 1
1 0
-1
Sample Output
2
2

题意:平面上有n个点,给你一个长宽确定的长方形,让你把这个长方形放在平面上,能让这个长方形盖住尽可能多的点(包括边界)

勇敢教练之前和我们讲的线段树扫描线的思想,首先,对x轴建线段树,然后对n个点,把它当作一条[x, x+w]的线段,同时它还要有另外一条线段,这个线段的在y + h处,长度也是[x,x+w]。接着我们就像做那道算矩阵面积的题目一样,原点的线段+1,出点的线段算-1(对于这棵线段树)。在y轴从下而上地扫描每一个点,用整棵线段树上记录的最大值来更新最终答案。

还有一个要考虑的点就是如果有一个出点和原点在同一个y轴上怎么办,如果先算出点的话我们的最大值就不对了。所以在扫描每个点的之前要做一遍排序,保证在y轴相同时让原点排在前面。

下面代码:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int maxn = 20010;
typedef long long ll;
const ll inf = (1 << 17) + 10;
int D[4 * maxn];
long long col[4*maxn];
struct unit{
    pair<int,int > point;
    int val;
}save[maxn];
bool compare(unit &a, unit &b){
    if(a.point.second == b.point.second) return a.val > b.val;
    return a.point.second < b.point.second;
}
void pushup(int rt){
    D[rt]=max(D[rt<<1],D[rt<<1|1]);
}
void pushdown(int rt){
    if(col[rt]){
        col[rt<<1] += col[rt];
        col[rt<<1|1] += col[rt];
        D[rt<<1]+=col[rt];
        D[rt<<1|1]+=col[rt];
        col[rt]=0;
    }
}
void update(int L,int R,int c,int l,int r,int rt){
    if(L<=l&&R>=r){
        col[rt]+=c;
        D[rt]+=c;
        return;
    }
    pushdown(rt);
    int m=(l+r)>>1;
    if(L<=m)update(L,R,c,lson);
    if(R>m) update(L,R,c,rson);
    pushup(rt);
}
int main(){
    int n,w,h,i,j;
    while(~scanf("%d",&n)&&n!=-1){
        scanf("%d%d",&w,&h);
        int left = inf, right = -inf;
        for(i=1;i<=n;i++){
            scanf("%d%d",&save[i].point.first, &save[i].point.second);
            save[i].val = 1;
            save[i+n].point.first = save[i].point.first;
            save[i+n].point.second = save[i].point.second + h;
            save[i+n].val = -1;
            left = min(left, save[i].point.first);
            right = max(right, save[i].point.first);
        }
        sort(save+1, save+1+2*n, compare);
        int res = 0;
        for(i=1;i<=2*n;i++){
            update(save[i].point.first, save[i].point.first + w, save[i].val, left, right, 1);
            res = max(res, D[1]);
        }
        printf("%d\n",res);
    }
    return 0;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值