线段树+扫描线 HDOJ 5091 Beam Cannon(大炮)

 

题目链接

题意:

  给出若干个点的坐标,用一个W*H的矩形去覆盖,问最多能覆盖几个点。

思路:

  这是2014上海全国邀请赛的题目,以前写过,重新学习扫描线。首先把所有点移到第一象限([0, 40000]),每个点从左到右排序,每个点在Y轴看成[y,y+H]的线段,扫描线从左到右扫描,把这条线段在线段树上+1,表示进矩形框,然后扫到这点对应的(x+W, y)的位置就-1,表示出了这个矩形框。那么线段树维护连续区间的线段的最大重叠数,即能覆盖到点的最大数量。

#include <bits/stdc++.h>

const int N = 1e4 + 5;
struct Point {
    int x, y, z;
};
Point point[N<<1];

bool cmp(const Point &a, const Point &b) {
    return a.x == b.x ? a.z > b.z : a.x < b.x;
}

#define lson l, mid, o << 1
#define rson mid + 1, r, o << 1 | 1
const int H = 40000 + 5;
int sum[H<<2], lazy[H<<2];

void push_up(int o) {
    sum[o] = std::max (sum[o<<1], sum[o<<1|1]);
}

void push_down(int o) {
    if (lazy[o]) {
        lazy[o<<1] += lazy[o];
        lazy[o<<1|1] += lazy[o];
        sum[o<<1] += lazy[o];
        sum[o<<1|1] += lazy[o];
        lazy[o] = 0;
    }
}

void build(int l, int r, int o) {
    sum[o] = 0; lazy[o] = 0;
    if (l == r) {
        return ;
    }
    int mid = l + r >> 1;
    build (lson);
    build (rson);
}

void updata(int ql, int qr, int c, int l, int r, int o) {
    if (ql <= l && r <= qr) {
        sum[o] += c; lazy[o] += c;
        return ;
    }
    push_down (o);
    int mid = l + r >> 1;
    if (ql <= mid) {
        updata (ql, qr, c, lson);
    }
    if (qr > mid) {
        updata (ql, qr, c, rson);
    }
    push_up (o);
}

int main() {
    int n, w, h;
    while (scanf ("%d", &n) == 1 && n > 0) {
        scanf ("%d%d", &w, &h);
        int tot = 0;
        for (int i=0; i<n; ++i) {
            int x, y;
            scanf ("%d%d", &x, &y);
            x += 20000; y += 20000;
            point[tot].x = x; point[tot].y = y;
            point[tot++].z = 1;
            point[tot].x = x + w; point[tot].y = y;
            point[tot++].z = -1;
        }
        std::sort (point, point+tot, cmp);
        build (0, 40000, 1);
        int ans = 0;
        for (int i=0; i<tot; ++i) {
            int ql = point[i].y, qr = point[i].y + h;
            if (qr > 40000) qr = 40000;
            updata (ql, qr, point[i].z, 0, 40000, 1);
            ans = std::max (ans, sum[1]);
        }
        printf ("%d\n", ans);
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/Running-Time/p/4523054.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值