zoukankan      html  css  js  c++  java
  • hdu6638 线段树求最大子段和

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6638

    Problem Description
    There are pirate chests buried in Byteland, labeled by 1,2,,n. The i-th chest's location is (xi,yi) , and its value is wi , wi can be negative since the pirate can add some poisonous gases into the chest. When you open the i -th pirate chest, you will get wi value.

    You want to make money from these pirate chests. You can select a rectangle, the sides of which are all paralleled to the axes, and then all the chests inside it or on its border will be opened. Note that you must open all the chests within that range regardless of their values are positive or negative. But you can choose a rectangle with nothing in it to get a zero sum.

    Please write a program to find the best rectangle with maximum total value.
     
    Input
    The first line of the input contains an integer T(1T100), denoting the number of test cases.

    In each test case, there is one integer n(1n2000) in the first line, denoting the number of pirate chests.

    For the next n lines, each line contains three integers xi,yi,wi(109xi,yi,wi109) , denoting each pirate chest.

    It is guaranteed that n10000 .
     
    Output
    For each test case, print a single line containing an integer, denoting the maximum total value.
     
    Sample Input
    2
    4
    1 1 50
    2 1 50
    1 2 50
    2 2 -500
    2
    -1 1 5
    -1 1 1
     
    Sample Output
    100
    6
     
     
    将点按纵坐标从小到大排序并将点离散化,枚举纵坐标确定矩形下边界,依次往后向线段树加入纵坐标相同的点,每加完纵坐标相同的点(确认矩形上边界)就求一次x轴的最大字段和并更新答案
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define maxn 2005
    #define ll long long
    #define ls l,mid,rt<<1
    #define rs mid+1,r,rt<<1|1
    struct node{
        ll sum,lsum,rsum,msum;
    }tr[maxn<<2];
    struct ww{
        ll x,y,val;
        bool operator <(const ww &w)const{
            if(y==w.y)return x<w.x;
            return y<w.y;
        }
    }a[maxn];
    inline void pushup(int rt)
    {
        tr[rt].sum=tr[rt<<1].sum+tr[rt<<1|1].sum;
        tr[rt].lsum=max(tr[rt<<1].lsum,tr[rt<<1].sum+tr[rt<<1|1].lsum);
        tr[rt].rsum=max(tr[rt<<1|1].rsum,tr[rt<<1|1].sum+tr[rt<<1].rsum);
        tr[rt].msum=max(max(tr[rt<<1].msum,tr[rt<<1|1].msum),tr[rt<<1|1].lsum+tr[rt<<1].rsum);
    }
    inline void build(int l,int r,int rt)
    {
        if(l==r)
        {
            tr[rt].sum=tr[rt].msum=tr[rt].lsum=tr[rt].rsum=0;
            return ;
        }
        int mid=l+r>>1;
        build(ls);build(rs);
        pushup(rt);
    }
    inline void update(int L,int c,int l,int r,int rt)
    {
        if(l==r)
        {
            tr[rt].sum=tr[rt].msum=tr[rt].lsum=tr[rt].rsum+=1ll*c;
            return ;
        }
        int mid=l+r>>1;
        if(L<=mid)update(L,c,ls);
        else update(L,c,rs);
        pushup(rt);
    }
    ll x[maxn],y[maxn];
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            int n;
            cin>>n;
            for(int i=1;i<=n;i++)
            {
                cin>>a[i].x>>a[i].y>>a[i].val;
                x[i]=a[i].x;y[i]=a[i].y;
            }
            sort(x+1,x+1+n);
            sort(y+1,y+1+n);
            int lenx=unique(x+1,x+1+n)-x-1;
            int leny=unique(y+1,y+1+n)-y-1;
            for(int i=1;i<=n;i++)
            {
                a[i].x=lower_bound(x+1,x+1+lenx,a[i].x)-x;
                a[i].y=lower_bound(y+1,y+1+leny,a[i].y)-y;
            }
            sort(a+1,a+1+n);
            ll ans=0;
            for(int i=1;i<=leny;i++)//确定矩形下边界 
            {
                build(1,lenx,1);
                int pos=1;
                while(a[pos].y<i)pos++;
                for(int j=i;j<=leny;j++)//确定矩形上边界 
                {
                    for(;a[pos].y==j;pos++)//插入纵坐标相同的点 
                    {
                        update(a[pos].x,a[pos].val,1,lenx,1);
                    }
                    ans=max(ans,tr[1].msum);//更新答案 
                }
            }
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    [javascript] vuejs为输入框增加回车事件
    iview上的兼容性问题
    python+vscode安装与插件配置
    Chrome浏览器获取XPATH的方法----通过开发者工具获取
    使用谷歌浏览器定位xpath是否准确
    [PHP] xpath提取网页数据内容
    PHP中preg_match正则匹配的/u /i /s是什么意思
    Flink connect 算子实践
    DataStreamUtils 连续keyBy 优化
    Heartbeat原理及部署
  • 原文地址:https://www.cnblogs.com/chen99/p/11394884.html
Copyright © 2011-2022 走看看