`
Simone_chou
  • 浏览: 184648 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Beam Cannon(线段树 + 扫描线)

    博客分类:
  • HDOJ
阅读更多

Beam Cannon

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 225    Accepted Submission(s): 87


Problem Description
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,W,H,代表有 N 个点还有一个 W 长 H 高的矩形。现用这个矩形覆盖,问最多能覆盖给出的点中有多少个。

 

       思路:

       线段树 + 扫描线。以每个点作为中心,建立一个矩形范围,说明这个矩形的中心放在这个区域中,就能够覆盖这个点。在建完所有的矩形后,求某个区域被矩形覆盖最多,这个区域被矩形覆盖数即为所求。用扫描线即可求出。

 

        AC:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX = 10005 * 2;

typedef struct {
    double y1, y2, x;
    int temp;
} node;

int n, m, ans;
double yy[MAX];
node no[MAX];

int cover[MAX * 5], Max[MAX * 5];

bool cmp (node a, node b) {
    if (a.x != b.x) return a.x < b.x;
    return a.temp > b.temp;
}

void push_down (int node) {
    cover[node << 1] += cover[node];
    cover[node << 1 | 1] += cover[node];
    Max[node << 1] += cover[node];
    Max[node << 1 | 1] += cover[node];
    cover[node] = 0;
}

void push_up (int node) {
    Max[node] = max(Max[node << 1], Max[node << 1 | 1]);
}

void updata (int node, int l, int r, int cl, int cr, int c) {
    if (cl > r || cr < l) return;
    if (cl <= l && cr >= r) {
        cover[node] += c;
        Max[node] += c;
        return;
    }

    push_down(node);
    int mid = (r + l) >> 1;
    if (cl <= mid) updata(node << 1, l, mid, cl, cr, c);
    if (cr > mid) updata(node << 1 | 1, mid + 1, r, cl, cr, c);
    push_up(node);
}

int main() {

    double w, h;
    while (~scanf("%d", &n) && n != -1) {
        scanf("%lf%lf", &w, &h);

        m = 0, ans = 0;
        for (int i = 1; i <= n; ++i) {
            double x, y;
            scanf("%lf%lf", &x, &y);
            yy[m++] = y + h / 2;
            yy[m++] = y - h / 2;

            no[ans].y1 = y - h / 2;
            no[ans].y2 = y + h / 2;
            no[ans].x = x - w / 2;
            no[ans++].temp = 1;

            no[ans].y1 = y - h / 2;
            no[ans].y2 = y + h / 2;
            no[ans].x = x + w / 2;
            no[ans++].temp = -1;
        }

        sort(yy, yy + m);
        m = unique(yy, yy + m) - yy;

        sort(no, no + ans, cmp);
        memset(cover, 0, sizeof(cover));
        memset(Max, 0, sizeof(Max));

        int res = 0;
        for (int i = 0; i < ans; ++i) {
            int cl = lower_bound(yy, yy + m, no[i].y1) - yy;
            int cr = lower_bound(yy, yy + m, no[i].y2) - yy;

            updata(1, 0, m - 1, cl, cr, no[i].temp);

            res = max(res, Max[1]);
        }

        printf("%d\n", res);

    }

    return 0;
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics