【HDU 5091】Beam Cannon(掃描線),hdu5091

來源:互聯網
上載者:User

【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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.