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.
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.
A test case starting with a negative integer terminates the input and this test case should not to be processed.
2 3 4 0 1 1 0 3 1 1 -1 0 0 1 1 0 -1
2 2
题意:平面上有n个点,给你一个长宽确定的长方形,让你把这个长方形放在平面上,能让这个长方形盖住尽可能多的点(包括边界)
勇敢教练之前和我们讲的线段树扫描线的思想,首先,对x轴建线段树,然后对n个点,把它当作一条[x, x+w]的线段,同时它还要有另外一条线段,这个线段的在y + h处,长度也是[x,x+w]。接着我们就像做那道算矩阵面积的题目一样,原点的线段+1,出点的线段算-1(对于这棵线段树)。在y轴从下而上地扫描每一个点,用整棵线段树上记录的最大值来更新最终答案。
还有一个要考虑的点就是如果有一个出点和原点在同一个y轴上怎么办,如果先算出点的话我们的最大值就不对了。所以在扫描每个点的之前要做一遍排序,保证在y轴相同时让原点排在前面。
下面代码:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int maxn = 20010;
typedef long long ll;
const ll inf = (1 << 17) + 10;
int D[4 * maxn];
long long col[4*maxn];
struct unit{
pair<int,int > point;
int val;
}save[maxn];
bool compare(unit &a, unit &b){
if(a.point.second == b.point.second) return a.val > b.val;
return a.point.second < b.point.second;
}
void pushup(int rt){
D[rt]=max(D[rt<<1],D[rt<<1|1]);
}
void pushdown(int rt){
if(col[rt]){
col[rt<<1] += col[rt];
col[rt<<1|1] += col[rt];
D[rt<<1]+=col[rt];
D[rt<<1|1]+=col[rt];
col[rt]=0;
}
}
void update(int L,int R,int c,int l,int r,int rt){
if(L<=l&&R>=r){
col[rt]+=c;
D[rt]+=c;
return;
}
pushdown(rt);
int m=(l+r)>>1;
if(L<=m)update(L,R,c,lson);
if(R>m) update(L,R,c,rson);
pushup(rt);
}
int main(){
int n,w,h,i,j;
while(~scanf("%d",&n)&&n!=-1){
scanf("%d%d",&w,&h);
int left = inf, right = -inf;
for(i=1;i<=n;i++){
scanf("%d%d",&save[i].point.first, &save[i].point.second);
save[i].val = 1;
save[i+n].point.first = save[i].point.first;
save[i+n].point.second = save[i].point.second + h;
save[i+n].val = -1;
left = min(left, save[i].point.first);
right = max(right, save[i].point.first);
}
sort(save+1, save+1+2*n, compare);
int res = 0;
for(i=1;i<=2*n;i++){
update(save[i].point.first, save[i].point.first + w, save[i].val, left, right, 1);
res = max(res, D[1]);
}
printf("%d\n",res);
}
return 0;
}