Beam CannonTime

** limit:3000/1500 MS (java/others) Memory limit:65536/65536 K (java/others)**

Total submission (s): 457 Accepted Submission (s): 175

Problem descriptionrecently, Theγgalaxies broke out Star Wars. Each planet are warring for resources. In the Star Wars, Planet X are under attack by other planets. Now, the a large wave of enemy spaceships is approaching. There is a very large Beam Cannon on the Planet X, and it's very powerful, which can destroy all the spaceships Tack 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 is considered as a point projected to the attack plane. If the point was in the rectangular attack area of the Beam Cannon (including border), the spaceship would be destroyed.

Inputinput contains multiple test cases. Each test case contains three integers N (1<=n<=10000, the number of enemy spaceships), W (1<=w<=40000, the Widt H 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 the integers x, y ( -20000<=x,y<=20000, the coordinates of an enemy spaceship).

A test case is starting with a negative integer terminates the input and this test case should not to be processed.

Outputoutput the maximum number of enemy spaceships the Beam Cannon can destroy in a single shot for each case.

Sample Input

2 3 40 11 03 1 1-1 00 11 0-1

Sample Output

22

Test instructions: In a plane there are n individuals, with a w*h rectangle to surround these people (also counted on the side), to find the maximum number.

Ideas: From small to large order x, Y value discretization, projected on the Y axis, then for everyone's ordinate, y,y+h is

Each star can affect the rectangle and then x,x+w+1 is an entry event and a go-out event, with values that are mutually

For the opposite number. Node[1].val saves the current maximum worth all the rectangles are traversed once to take the maximum value of which is ans.

#include <iostream> #include <cstdio> #include <algorithm>using namespace std;const int maxn=20005; struct node{int x,y1,y2,val; void Fun (int xx,int yy1,int Yy2,int v) {x=xx,y1=yy1,y2=yy2; Val=v; }}b[maxn];struct tree{int l,r,add,val;} A[maxn*4];int N,w,h,cnt,y[maxn];bool CMP (node P,node q) {return p.x<q.x;} void build (int l,int r,int k) {a[k].l=l,a[k].r=r; a[k].add=a[k].val=0; if (l==r) return; int mid= (L+R)/2; Build (L,mid,2*k); Build (mid+1,r,2*k+1);} void pushdown (int k) {a[2*k].val+=a[k].add; A[2*k].add+=a[k].add; A[2*k+1].val+=a[k].add; A[2*k+1].add+=a[k].add; A[k].add=0;} void Insert (int l,int r,int c,int k) {if (y[a[k].l]==l && y[a[k].r]==r) {a[k].val+=c; A[k].add+=c; } else {pushdown (k); int mid= (A[K].L+A[K].R)/2; if (y[mid]>=r) insert (L,R,C,2*K); else if (y[mid]<l) insert (l,r,c,2*k+1); else { Insert (L,Y[MID],C,2*K); Insert (y[mid+1],r,c,2*k+1); } a[k].val=max (A[2*k].val,a[2*k+1].val); }}void input () {cnt=0; int x, y; scanf ("%d%d", &w,&h); for (int i=0;i<n;i++) {scanf ("%d%d", &x,&y); y+=20000; Y[cnt]=y; B[cnt++].fun (x,y,y+h,1); Y[cnt]=y+h; B[cnt++].fun (x+w+1,y,y+h,-1); } sort (y,y+cnt); Sort (b,b+cnt,cmp); Cnt=unique (y,y+cnt)-y; void Solve () {build (0,cnt-1,1); int ans=0; for (int i=0;i<2*n;i++) {insert (b[i].y1,b[i].y2,b[i].val,1); Ans=max (Ans,a[1].val); } printf ("%d\n", ans);} int main () {while (scanf ("%d", &n)!=eof) {if (n<0) break; Input (); Solve (); } return 0;}

HDU 5091 Beam Cannon (segment tree + scan line + discretization)