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

Beam Cannon

HDU - 5091

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

题目详解

code:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 1e4+5;
int n,w,h;
struct Node{
	int x,y,v;
	bool operator < (const Node &a)const{
		if(x == a.x)
			return v > a.v;
		return x < a.x;
	}
}node[2*N];
struct TNode{
	int lazy;
	int num;
}tre[16*N];
void build(int l,int r,int i){
	tre[i].lazy = tre[i].num = 0;
	if(l == r) return;
	int mid = l + r >> 1;
	build(l,mid,i<<1);
	build(mid+1,r,i<<1|1);
}
void pushdown(int i){
	tre[i<<1].lazy += tre[i].lazy;
	tre[i<<1|1].lazy += tre[i].lazy;
	tre[i<<1].num += tre[i].lazy;
	tre[i<<1|1].num += tre[i].lazy;
	tre[i].lazy = 0;
}
void update(int l,int r,int i,int t){
	if(node[t].y <= l && r <= node[t].y+h){
		tre[i].lazy += node[t].v;
		tre[i].num += node[t].v;
		return;
	}
	if(tre[i].lazy != 0) pushdown(i);
	int mid = l + r >> 1;
	if(node[t].y <= mid) update(l,mid,i<<1,t);
	if(node[t].y+h > mid) update(mid+1,r,i<<1|1,t);
	tre[i].num = max(tre[i<<1].num,tre[i<<1|1].num);
}
int main(){
	while(scanf("%d",&n) != EOF && n > 0){
		scanf("%d%d",&w,&h);
		for(int i = 1; i <= n; i++){
			int x,y;
			scanf("%d%d",&x,&y);
			node[2*i-1].x = x;
			node[2*i-1].y = y + 2 * N;
			node[2*i-1].v = 1;
			node[2*i].x = x + w;
			node[2*i].y = y + 2 * N;
			node[2*i].v = -1;
		}
		sort(node+1,node+1+2*n);
		build(1,4*N,1);
		int sum = 0;
		for(int i = 1; i <= 2*n; i++){
			update(1,4*N,1,i);
			sum = max(sum,tre[1].num);
		}
		printf("%d\n",sum);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/codeswarrior/article/details/81084449