poj2482(扫描线)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

typedef long long ll;

struct my{
   ll x,y,c;
};

const int maxn=20000+10;

my l[maxn];
ll n,Rn,w,h;
ll Rank[maxn];
ll add[maxn<<2],tree[maxn<<2];
ll o[maxn];

void lisan(){
    sort(Rank+1,Rank+1+Rn);
    int I=1;
    for(int i=2;i<=Rn;++i) if(Rank[i]!=Rank[i-1]) Rank[++I]=Rank[i];
    Rn=I;
}

bool cmp(const my &a,const my &b){
    if(a.x==b.x) return a.y<b.y;
    return a.x<b.x;
}

void up(int x){
     if(add[x]){
        add[x<<1]+=add[x];
        add[x<<1|1]+=add[x];
        tree[x<<1]+=add[x];
        tree[x<<1|1]+=add[x];
        add[x]=0;
     }
}

void pushup(int x){
     tree[x]=max(tree[x<<1],tree[x<<1|1]);
}

void update(ll L,ll R,ll l,ll r,ll rt,ll c){
     if(l>=L&&r<=R){
        add[rt]+=c;
        tree[rt]+=c;
        return ;
     }
     ll mid=(l+r)>>1;
     up(rt);
     if(L<=mid) update(L,R,l,mid,rt<<1,c);
     if(R>mid) update(L,R,mid+1,r,rt<<1|1,c);
     pushup(rt);
}

ll get(ll x){
   int L=1,R=Rn,M;//[L,R] first >=x
    while(L!=R){
        M=(L+R)>>1;
        if(Rank[M]<x) L=M+1;
        else R=M;
    }
    return L;
}

int main(){
    while(scanf("%lld%lld%lld",&n,&w,&h)!=EOF){
        memset(add,0,sizeof(add));
        memset(tree,0,sizeof(tree));
        Rn=0;
        ll ans=0;
        for (int i=1;i<=n;i++){
            scanf("%lld%lld%lld",&l[i].x,&l[i].y,&l[i].c);
            Rank[++Rn]=l[i].y;
            Rank[++Rn]=l[i].y+h;
        }
        lisan();
        sort(l+1,l+1+n,cmp);
        int X=1;
        int p=1;
        for (int i=1;i<=n;i++){
            update(get(l[i].y),get(l[i].y+h)-1,1,Rn,1,l[i].c);
            X=l[i].x;
            while(l[p].x+w<=X){
                update(get(l[p].y),get(l[p].y+h)-1,1,Rn,1,-l[p].c);
                ++p;
            }
           // printf("%lld ",tree[1]);
           ans=max(ans,tree[1]);
        }
        printf("%lld ",ans);
        //for (int i=1;i<=Rn;i++) printf("%lld ",get(Rank[i]));
    }
return 0;
}
玄学离散化错误

原文地址:https://www.cnblogs.com/lmjer/p/9208005.html