Main topic:
There are n points (n<=10000), the coordinates of the point are not more than 20000, and then ask you to use a w*h (1<=w,h<=40000) Rectangle, the edge of the rectangle parallel to the axis, the maximum number of points can be covered.
Rujia Black Book has the original problem
The code below is discretized, scanned with a straight line perpendicular to the x axis, and a line tree on the y axis, so for each point (x, y), give the weight of 1, and then add a new negative point (x+w,y), giving the weight-1. When the scan line sweeps through each point, the segment tree is updated with the weighted value of the point. Maintain the sum value for each interval, because it is an interval update, so you have to make a lazy tag.
1#include <stdio.h>2#include <stdlib.h>3#include <math.h>4#include <string.h>5#include <iostream>6#include <string>7#include <map>8#include <Set>9#include <algorithm>Ten#include <queue> One#include <vector> A #defineN 40000 - using namespacestd; - the structNode - { - intL,r,lazy; - intsum; +} node[n<<2]; - struct Line + { A intx,y1,y2,v; at Line () {} -Line (intAintBintCintd) - { -x=a,y1=b,y2=c,v=D; - } - BOOL operator< (ConstLine&a)Const in { - if(x==a.x) to returnV>A.V; + returnx<a.x; - } the } Line[n]; * intLisan[n]; $ intCnt=0;Panax Notoginseng voidPushup (intRT) - { theNode[rt].sum=max (node[rt<<1].sum,node[rt<<1|1].sum); + } A voidPushdown (intRT) the { + if(Node[rt].lazy) - { $node[rt<<1].lazy+=Node[rt].lazy; $node[rt<<1|1].lazy+=Node[rt].lazy; -node[rt<<1].sum+=Node[rt].lazy; -node[rt<<1|1].sum+=Node[rt].lazy; thenode[rt].lazy=0; - }Wuyi } the voidBuildintRtintLintR) - { Wunode[rt].sum=0, node[rt].lazy=0; -Node[rt].l=l,node[rt].r=R; About if(l==R) $ return; - intMid=l+r>>1; -Build (rt<<1, l,mid); -Build (rt<<1|1, mid+1, R); A pushup (RT); + } the voidUpdateintRtintLintRintv) - { $ if(l<=node[rt].l&&node[rt].r<=R) the { thenode[rt].lazy+=v; thenode[rt].sum+=v; the return; - } in pushdown (RT); the intMid=node[rt].l+node[rt].r>>1; the if(l<=mid) AboutUpdate (rt<<1, l,r,v); the if(r>mid) theUpdate (rt<<1|1, l,r,v); the pushup (RT); + } - intQueryintRtintLintR) the {Bayi if(l<=node[rt].l&&node[rt].r<=R) the { the returnnode[rt].sum; - } - pushdown (RT); the intMid=node[rt].l+node[rt].r>>1, ans=0; the if(l<=mid) theAns+=query (rt<<1, l,r); the if(r>mid) -Ans+=query (rt<<1|1, l,r); the returnans; the } the intMain ()94 { the intn,w,h; the while(SCANF ("%d",&N)) the {98Cnt=0; About if(n<0) - Break;101scanf"%d%d",&w,&h);102 for(intI=0; i<n;++i)103 {104 intx, y; thescanf"%d%d",&x,&y);106x+=20001;107y+=20001;108lisan[cnt]=y;109Line[cnt++]=line (X,y,y+h,1); thelisan[cnt]=y+h;111Line[cnt++]=line (x+w,y,y+h,-1); the }113Sort (line,line+CNT); theSort (lisan,lisan+CNT); the intNum=unique (LISAN,LISAN+CNT)-Lisan; theBuild1,0, num-1);117 intans=0;118 for(intI=0; i<cnt;++i)119 { - intL=lower_bound (LISAN,LISAN+NUM,LINE[I].Y1)-Lisan;121 intR=lower_bound (Lisan,lisan+num,line[i].y2)-Lisan;122Update1, L,R,LINE[I].V);123Ans=max (ans,node[1].sum);124 } theprintf"%d\n", ans);126 }127 -}
HDU 5091 Beam Cannon