Segment tree optimization dp-- off more cattle first school I (good title)

 And two days to do a two optimized data structures dp title, or about the same routine

Solution to a problem link! https://www.cnblogs.com/kls123/p/11221471.html 

Some supplements

In fact, this question of dp [i] maintained that not every point, but each of the discrete y, can be understood as the polyline is currently staying at the ordinate is the answer of y

From left to right, from proceeding further traverse point, for each point p [i] consider three cases:

1. polyline place after this point, then this must be polylines from less than equal to p [i] .y folded up, so you can query for a range of extremes

2. At this point above the fold lines, then by this point to update those values ​​on which the broken line can be

3. point below the fold line, and empathy 2

Further, since the fold line may be directly folded up from y = 0, it is necessary to increase the y = 0 point of the first type to be transferred, without wrong, this case is equal to less!

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid ll m = (l + r) >> 1

const int M = 1e5+10;
ll mx[M<<2],lazy[M<<2];
void up(ll rt){
    mx[rt] = max(mx[rt<<1],mx[rt<<1|1]);
}

void pushdown(ll rt){
    if(lazy[rt]){
        lazy[rt<<1] += lazy[rt];
        lazy[rt<<1|1] += lazy[rt];
        mx[rt<<1] += lazy[rt];
        mx[rt<<1|1] += lazy[rt];
        lazy[rt] = 0;
    }
}

void build(ll l,ll r,ll rt){
    lazy[rt] = 0; mx[rt] = 0;
    if(l == r){
        return ;
    }
    mid;
    build(lson); build(rson);
}

void update(ll p,ll c,ll l,ll r,ll rt){
    if(l == r){
        mx[rt] = max(mx[rt],c);
        return ;
    }
    pushdown(rt);
    mid;
    if(p <= m) update(p,c,lson);
    else update(p,c,rson);
    up(rt);
}

void update1(ll L,ll R,ll c,ll l,ll r,ll rt){
    if(L > R) return ;  //会出现L > R的情况,需要判下
    if(L <= l&&R >= r){
        mx[rt] += c;
        lazy[rt] += c;
        return ;
    }
    pushdown(rt);
    mid;
    if(L <= m) update1(L,R,c,lson);
    if(R > m) update1(L,R,c,rson);
    up(rt);
}

ll query(ll L,ll R,ll l,ll r,ll rt){
    if(L > R) return 0;
    if(L <= l&&R >= r){
        return mx[rt];
    }
    pushdown(rt);
    mid;
    ll ret = 0;
    if(L <= m) ret = max(ret,query(L,R,lson));
    if(R > m) ret = max(ret,query(L,R,rson));
    return ret;
}

struct node{
    ll x,y,a,b;
}v[M];
bool cmp(node aa,node bb){
    if(aa.x == bb.x) return aa.y > bb.y;
    return aa.x < bb.x;
}
ll t[M];
int main()
{
    ll n;
    while(scanf("%lld",&n)!=EOF){
        ll cnt = 0;
        for(ll i = 1;i <= n;i ++){
            scanf("%lld%lld%lld%lld",&v[i].x,&v[i].y,&v[i].a,&v[i].b);
            t[++cnt] = v[i].y;
        }
        sort(t+1,t+1+cnt);
        sort(v+1,v+1+n,cmp);
        ll m = unique(t+1,t+1+cnt)-t-1;
        for(ll i = 1;i <= n;i ++)
            v[i].y = lower_bound(t+1,t+1+m,v[i].y)-t+1; //离散化时点都向后移一位
        m ++;  //点后移了一位,长度要+1;
        build(1,m,1);
        for(ll i = 1;i <= n;i ++){
            ll ans = query(1,v[i].y,1,m,1);
            update1(v[i].y+1,m,v[i].b,1,m,1);
            update1(1,v[i].y-1,v[i].a,1,m,1);
            update(v[i].y,ans+v[i].b,1,m,1);
        }
        printf("%lld\n",mx[1]);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/zsben991126/p/11285257.html