hdu 5091 (扫描线思维+ 线段树维护)

Beam Cannon

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


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
思路: 为每个节点建立一个虚拟的节点,x为x+w y不变,那么从左边扫描x 扫到右边 建立一颗以y为节点的线段树 动态更新线段树

代码:

#include<bits/stdc++.h>
#define lson (i<<1)
#define rson (i<<1|1)

using namespace std;
typedef long long ll;
const int N =1e4+5;
const int lim=2e4+5;

struct node
{
    int x,y;
    int val;
}a[2*N];
int n;
int h,w;

struct Tnode
{
    int l,r;
    int lazy;
    int val;
}tree[(4*N+25)<<2];

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

void push_up(int i)
{
    tree[i].val=max(tree[lson].val,tree[rson].val);
}

void push_down(int i)
{
    tree[lson].lazy+=tree[i].lazy;
    tree[rson].lazy+=tree[i].lazy;
    tree[lson].val+=tree[i].lazy;
    tree[rson].val+=tree[i].lazy;
    tree[i].lazy=0;
}

void build(int i,int l,int r)
{
    tree[i].lazy=0; tree[i].val=0;
    tree[i].l=l;  tree[i].r=r;
    if(l==r) return ;
    int mid=(l+r)>>1;
    build(lson,l,mid);
    build(rson,mid+1,r);
    push_up(i);
}

void update(int i,int aim)
{
    if(a[aim].y<=tree[i].l&&a[aim].y+h>=tree[i].r)
    {
        tree[i].lazy+=a[aim].val;
        tree[i].val+=a[aim].val;
        return ;
    }
    if(tree[i].lazy!=0) push_down(i);
    int mid=(tree[i].l+tree[i].r)>>1;
    if(a[aim].y<=mid) update(lson,aim);
    if(a[aim].y+h>mid) update(rson,aim);
    push_up(i);
}

int main()
{
    while(cin>>n)
    {
        if(n==-1) break;
        cin>>w>>h;

        for(int i=1;i<=n;i++){
            scanf("%d %d",&a[2*i-1].x ,&a[2*i-1].y);
            a[2*i-1].val=1;
            a[2*i-1].y+=lim;
            a[2*i].x=a[2*i-1].x+w;
            a[2*i].y=a[2*i-1].y;
            a[2*i].val=-1;
        }

        sort(a+1,a+2*n+1,cmp);
        build(1,1,4*N+25);
        int maxx=0;
        for(int i=1;i<=2*n;i++)
        {
            update(1,i);
            maxx=max(maxx,tree[1].val);
        }

        cout<<maxx<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yjt9299/article/details/80541695