51nod 1208&& POJ 2482

51nod点击打开链接

题意:中文

思路:将二维转化成一维来做,对于当前点来说它可以产生结果的区间是Y到Y+H,然后停止产生结果是在X+W+1处,所以在X+W处标记一下即可,用线段树边扫描边更新最大值

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3fll;
const int maxn=100010;
int num[maxn<<2],flag[maxn<<2];
ll tmp[maxn];
void pushdown(int node){
    if(flag[node]!=0){
        flag[node<<1]+=flag[node];
        flag[node<<1|1]+=flag[node];
        num[node<<1]+=flag[node];
        num[node<<1|1]+=flag[node];
        flag[node]=0;
    }
}
void update(int l,int r,int val,int le,int ri,int node){
    if(l<=le&&ri<=r){
        num[node]+=val;
        flag[node]+=val;
        return ;
    }
    pushdown(node);
    int t=(le+ri)>>1;
    if(l<=t) update(l,r,val,le,t,node<<1);
    if(r>t) update(l,r,val,t+1,ri,node<<1|1);
    num[node]=max(num[node<<1],num[node<<1|1]);
}
struct node{
    ll x,y,L,op;
}A[maxn];
bool cmp(const node &a,const node &b){
    if(a.x==b.x) return a.op<b.op;
    return a.x<b.x;
}
int main(){
    int n,w,h;
    ll xx,yy,LL;
    while(scanf("%d%d%d",&n,&w,&h)!=-1){
        int len=0,len2=0;
        for(int i=0;i<n;i++){
            scanf("%I64d%I64d%I64d",&xx,&yy,&LL);
            tmp[len++]=yy;tmp[len++]=yy+h;
            A[len2].x=xx;A[len2].y=yy;A[len2].L=LL;A[len2++].op=0;
            A[len2].x=xx+w;A[len2].y=yy;A[len2].L=LL;A[len2++].op=1;
        }
        sort(A,A+len2,cmp);
        sort(tmp,tmp+len);
        int len1=unique(tmp,tmp+len)-tmp;
        memset(flag,0,sizeof(flag));
        memset(num,0,sizeof(num));
        int ans=0;
        for(int i=0;i<len2;i++){
            int tt1=lower_bound(tmp,tmp+len1,A[i].y)-tmp+1;
            int tt2=lower_bound(tmp,tmp+len1,A[i].y+h)-tmp+1;
            if(A[i].op==0) update(tt1,tt2,A[i].L,1,len1,1);
            else update(tt1,tt2,-A[i].L,1,len1,1);
            ans=max(ans,num[1]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

POJ点击打开链接

题意:与前一个类似,就是在边界的不算进结果中

思路:与上一个想法一样,代码也没什么变化,就是它能产生结果的范围减少1就可以了

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3fll;
const int maxn=100010;
ll num[maxn<<2],flag[maxn<<2];
ll tmp[maxn];
void pushdown(int node){
    if(flag[node]!=0){
        flag[node<<1]+=flag[node];
        flag[node<<1|1]+=flag[node];
        num[node<<1]+=flag[node];
        num[node<<1|1]+=flag[node];
        flag[node]=0;
    }
}
void update(int l,int r,int val,int le,int ri,int node){
    if(l<=le&&ri<=r){
        num[node]+=val;
        flag[node]+=val;
        return ;
    }
    pushdown(node);
    int t=(le+ri)>>1;
    if(l<=t) update(l,r,val,le,t,node<<1);
    if(r>t) update(l,r,val,t+1,ri,node<<1|1);
    num[node]=max(num[node<<1],num[node<<1|1]);
}
struct node{
    ll x,y,L,op;
}A[maxn];
bool cmp(const node &a,const node &b){
    if(a.x==b.x) return a.op<b.op;
    return a.x<b.x;
}
int main(){
    ll n,w,h;
    ll xx,yy,LL;
    while(scanf("%lld%lld%lld",&n,&w,&h)!=-1){
        int len=0,len2=0;
        for(int i=0;i<n;i++){
            scanf("%lld%lld%lld",&xx,&yy,&LL);
            tmp[len++]=yy;tmp[len++]=yy+h-1;
            A[len2].x=xx;A[len2].y=yy;A[len2].L=LL;A[len2++].op=0;
            A[len2].x=xx+w-1;A[len2].y=yy;A[len2].L=LL;A[len2++].op=1;
        }
        sort(A,A+len2,cmp);
        sort(tmp,tmp+len);
        int len1=unique(tmp,tmp+len)-tmp;
        memset(flag,0,sizeof(flag));
        memset(num,0,sizeof(num));
        ll ans=0;
        for(int i=0;i<len2;i++){
            int tt1=lower_bound(tmp,tmp+len1,A[i].y)-tmp+1;
            int tt2=lower_bound(tmp,tmp+len1,A[i].y+h-1)-tmp+1;
            if(A[i].op==0) update(tt1,tt2,A[i].L,1,len1,1);
            else update(tt1,tt2,-A[i].L,1,len1,1);
            ans=max(ans,num[1]);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值