【HDU 5091】Beam Cannon(掃描線),hdu5091
按照x軸建樹,求線段樹的區間最值。
模型轉化就是: 矩陣最大交
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define lson (pos<<1)#define rson (pos<<1|1)const int ADD = 25555;const int maxn = 80005;int n,w,h,cnt;struct Seg{ int l,r,h,c; Seg(int l = 0,int r = 0,int h = 0,int c = 0):l(l),r(r),h(h),c(c){}; friend bool operator < (Seg p,Seg q){ return p.h < q.h; }}ss[maxn];int col[maxn << 2],max_value[maxn << 2];void pushdown(int pos){ if(col[pos]){ col[lson] += col[pos]; max_value[lson] += col[pos]; col[rson] += col[pos]; max_value[rson] += col[pos]; col[pos] = 0; }}void pushup(int pos){ max_value[pos] = max(max_value[lson],max_value[rson]);}void update(int L,int R,int l,int r,int pos,int v){ if(l <= L && R <= r){ col[pos] += v; max_value[pos] += v; return; } pushdown(pos); int mid = (L + R) >> 1; if(l <= mid) update(L,mid,l,r,lson,v); if(r > mid) update(mid + 1,R,l,r,rson,v); pushup(pos);}int main(){ while(scanf("%d",&n) && n > 0){ scanf("%d%d",&w,&h); cnt = 0; memset(col,0,sizeof(col)); memset(max_value,0,sizeof(max_value)); int x,y; for(int i = 0; i < n; i++){ scanf("%d%d",&x,&y); x += ADD; y += ADD; ss[cnt++] = Seg(x,x + w,y,1); ss[cnt++] = Seg(x,x + w,y + h,-1); } sort(ss,ss + cnt); int ans = 0; for(int i = 0; i < cnt; i++){ int l = ss[i].l,r = ss[i].r,c = ss[i].c; update(0,maxn,l,r,1,c); ans = max(ans,max_value[1]); } printf("%d\n",ans); } return 0;}