cdoj 长使英雄泪满襟(线段树)

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <memory.h>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <ctime>
#include <cassert>

using namespace std;
int Y[200005];
int n,w,h;
struct Point
{
    int x,y,val;
    int flag;
}point[100005],ex_point[200005] ;
struct node
{
   int left,right;
   int lazy;
   int sum;
}tree[800005];

void build(int rt,int l,int r)
{
    tree[rt].left=l;tree[rt].right=r;
    if(r==l)  tree[rt].lazy=0,tree[rt].sum=0;
    else
    {
        int mid=(l+r)/2;
        build(rt*2,l,mid);
        build(rt*2+1,mid+1,r);
        tree[rt].lazy=0,tree[rt].sum=0;
        tree[rt].sum=tree[2*rt].sum+tree[2*rt+1].sum;
    }
}
void pushdown(int rt,int lazy)
{
    tree[2*rt].lazy+=lazy;
    tree[2*rt].sum+=lazy;
    tree[2*rt+1].lazy+=lazy;
    tree[2*rt+1].sum+=lazy;
    tree[rt].lazy=0;
}
void update(int rt,int l,int r,int va)
{
    if(tree[rt].lazy!=0)
    {
      if(tree[rt].left!=tree[rt].right)
       pushdown(rt,tree[rt].lazy);
    }
    if(tree[rt].left==l && tree[rt].right==r)
    {
        tree[rt].sum+=va;
        tree[rt].lazy+=va;
    }
    else
    {
        int mid=(tree[rt].right+tree[rt].left)/2;
        if(mid<l)
        {
           update(rt*2+1,l,r,va);
           tree[rt].sum=max(tree[rt*2].sum,tree[rt*2+1].sum);
        }
        else if(mid>=r)
        {
           update(rt*2,l,r,va);
           tree[rt].sum=max(tree[rt*2].sum,tree[rt*2+1].sum);
        }
        else
        {
           update(rt*2,l,mid,va);
           update(rt*2+1,mid+1,r,va);
           tree[rt].sum=max(tree[rt*2].sum,tree[rt*2+1].sum);
        }
    }
}
int cmp(const Point & a,const Point & b)
{
    if(a.x<b.x) return 1;
    else return 0;
}
int cmp_y(const int &a,const int &b)
{
    if(a<b)  return 1;
    else  return 0;
}
int b_search(int key,int n)
{
    int m,l=1,r=n;
    while(l<=r)
    {
        m=(l+r)/2;
        if(Y[m]==key)
            return m;
        else if(Y[m]>key)
             r=m-1;
        else l=m+1;
    }
    return -1;
}
int main()
{
    int i,t;
    scanf("%d%d%d",&n,&w,&h);
    for(i=1;i<=n;i++)
    {
        scanf("%d%d%d",&point[i].x,&point[i].y,&point[i].val);
        ex_point[i]=point[i];ex_point[i].flag=0;//原值为出边
        ex_point[i+n]=point[i];ex_point[i+n].x-=w;ex_point[i+n].flag=1;//减W后的值为入边
        Y[i]=point[i].y;
        Y[i+n]=point[i].y-h;
    }
    sort(ex_point+1,ex_point+1+2*n,cmp);
    sort(Y+1,Y+1+2*n,cmp_y);
    int m=1;
    for(i=1;i<=2*n;i++)//离散化Y坐标
      if(Y[i]!=Y[m])
         Y[++m]=Y[i];
    for(i=1;i<=m;i++)
        printf("%d  ",Y[i]);
    build(1,1,m);
    int ans=0;
    for(i=1;i<=2*n;i++)
       {
            int l=b_search(ex_point[i].y-h,m);
            int r=b_search(ex_point[i].y,m);
            if(ex_point[i].flag==1)
                update(1,l,r,ex_point[i].val);
            else
                update(1,l,r,-ex_point[i].val);
            printf("%d  ",tree[1].sum);
            ans=max(ans,tree[1].sum);
        }
    printf("%d\n",ans);
   return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值