P3097 [USACO13DEC] Optimal milking (segment tree optimization dp)

 

 Blind guess dp series. . .

The meaning of problems: in a given sequence, selected from the i and i can not be selected from the two adjacent, selecting the maximum value, with modifications

Konjac 10min kick in the examination room that only two cases of the wrong solution. . . Actually be able to cheat a little points. . .

Talk about the idea of ​​it at the time.

0/1 f [i] [] indicates the maximum value of the i-th selected from the teat is not selected, two transfer, water may not work.

After finished chiefs came to understand in demand, which is similar to a separate set of things.

But the data range is definitely not let us run the largest independent set, after all, but also to modify 233. . .

solution:

Summing .... modify a single point of maximum .... looks can think of ... what .....

Lovely tree line. . (That is not cute)

After all, the same sequence length, as long as the single point of modification on the line.

Building a segment tree, which kept f1, f2, f3, f4, l, r;

// f1 represent both ends of the selected // f2 represents the left and right do not choose the option // f3 represents the left and right do not choose the option // f4 represent about not chosen

Then transferred relatively long, there is no other (should Nengkanmingbai too lazy to write a comment)

void update(ll k)
{
    t[k].f1=max(t[lch(k)].f1+t[rch(k)].f3,t[lch(k)].f2+max(t[rch(k)].f1,t[rch(k)].f3));
    t[k].f2=max(t[lch(k)].f1+t[rch(k)].f4,t[lch(k)].f2+max(t[rch(k)].f2,t[rch(k)].f4));
    t[k].f3=max(t[lch(k)].f3+t[rch(k)].f3,t[lch(k)].f4+max(t[rch(k)].f1,t[rch(k)].f3));
    t[k].f4=max(t[lch(k)].f3+t[rch(k)].f4,t[lch(k)].f4+max(t[rch(k)].f2,t[rch(k)].f4));
}

Then is the segment tree thing

Code:

#include<bits/stdc++.h>
#define lch(x) x<<1
#define rch(x) x<<1|1
#define ll long long
using namespace std;
const ll maxn=1e6+10;
ll a[maxn];
ll n,d;
ll ans;
struct tree
{
    ll f1,f2,f3,f4;
    ll l,r;
}t[maxn];
ll p,v;
void update(ll k)
{
    t[k].f1=max(t[lch(k)].f1+t[rch(k)].f3,t[lch(k)].f2+max(t[rch(k)].f1,t[rch(k)].f3));
    t[k].f2=max(t[lch(k)].f1+t[rch(k)].f4,t[lch(k)].f2+max(t[rch(k)].f2,t[rch(k)].f4));
    t[k].f3=max(t[lch(k)].f3+t[rch(k)].f3,t[lch(k)].f4+max(t[rch(k)].f1,t[rch(k)].f3));
    t[k].f4=max(t[lch(k)].f3+t[rch(k)].f4,t[lch(k)].f4+max(t[rch(k)].f2,t[rch(k)].f4));
}
void build(ll l,ll r,ll p)
{
    t[p].l=l;
    t[p].r=r;
    if(l==r)
    {
        t[p].f1=a[l];
        return;
    }
    ll mid=l+r>>1;
    build(l,mid,p<<1);
    build(mid+1,r,p<<1|1);
    update(p);
}
void change(ll k)
{
    if(t[k].l==t[k].r)
    {
        t[k].f1=v;
        return;
    }
    ll mid=(t[k].l+t[k].r)>>1;
    if(p<=mid)change(k<<1);
    else change(k<<1|1);
    update(k);
}
int main()
{
    scanf("%lld%lld",&n,&d);
    for(ll i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    build(1,n,1);
    for(ll i=1;i<=d;i++)
    {
        scanf("%lld%lld",&p,&v);
        change(1);
        ans+=max(max(t[1].f1,t[1].f2),max(t[1].f3,t[1].f4));
    }
    printf("%lld",ans);
    return 0;
}
View Code

(Finish)

Guess you like

Origin www.cnblogs.com/ajmddzp/p/11717419.html