HDU - 5091 Beam Cannon (线段树+扫描线)

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,y)我们可以转化成两条边(x,y,y+H)和(x+W,y,y+H)权值分别为1和-1。然后我们从左往右扫一遍,做区间更新并维护区间最大值就行。过程中的最大值就是答案。

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

const int MAXN = 20000;

struct T{
	int value,lazy;
}Tree[MAXN*2*4+50];

struct Edge{
	int x,y,value;
	Edge(){}
	Edge(int a,int b,int c):x(a),y(b),value(c){}
	bool operator < (const struct Edge &b)const{   
        if(x != b.x)return x < b.x;
		return y < b.y;  
    }  
}E[MAXN+50];

void Build(int temp,int left,int right){
	Tree[temp].value = Tree[temp].lazy = 0;
	if(left == right)return ;
	int m = left + (right-left)/2;
	Build(temp<<1,left,m);
	Build(temp<<1|1,m+1,right);
}

void Up(int temp){
	Tree[temp].value = max(Tree[temp<<1].value,Tree[temp<<1|1].value);
}

void PushDown(int temp){
	if(Tree[temp].lazy){
		Tree[temp<<1].lazy += Tree[temp].lazy;
		Tree[temp<<1|1].lazy += Tree[temp].lazy;
		Tree[temp<<1].value += Tree[temp].lazy;
		Tree[temp<<1|1].value += Tree[temp].lazy;
		Tree[temp].lazy = 0;
	}
}

void Updata(int temp,int left,int right,int ql,int qr,int value){
	if(left>qr || right<ql)return ;
	if(ql<=left && qr>=right){
		Tree[temp].value += value;
		Tree[temp].lazy += value;
		return ;
	}
	PushDown(temp);
	int m = left + (right-left)/2;
	if(ql<=m)Updata(temp<<1,left,m,ql,qr,value);
	if(qr>m)Updata(temp<<1|1,m+1,right,ql,qr,value);
	Up(temp);
}

int main(){
	
	int N,W,H;
	while(scanf("%d",&N) && N>0){
		scanf("%d %d",&W,&H);
		int ma = 0;
		for(int i=0 ; i<N ; ++i){
			int x,y;
			x += MAXN;//解决负数问题 
			y += MAXN;//解决负数问题 
			scanf("%d %d",&x,&y);
			E[i<<1] = Edge(x,y,1);
			E[i<<1|1] = Edge(x+W,y,-1);
		}
		sort(E,E+N*2);
		Build(1,1,MAXN*2);
		for(int i=0 ; i<N*2 ; ++i){
			Updata(1,1,MAXN*2,E[i].y,E[i].y+H,E[i].value);
			ma = max(ma,Tree[1].value);
		}
		printf("%d\n",ma);
	}
	
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值