HDU 5052 Yaoge’s maximum profit 树链剖分+线段树(考虑方向的合并)

http://acm.hdu.edu.cn/showproblem.php?pid=5052
Yaoge likes to eat chicken chops late at night. Yaoge has eaten too many chicken chops, so that Yaoge knows the pattern in the world of chicken chops. There are N cities in the world numbered from 1 to N . There are some roads between some cities, and there is one and only one simple path between each pair of cities, i.e. the cities are connected like a tree. When Yaoge moves along a path, Yaoge can choose one city to buy ONE chicken chop and sell it in a city after the city Yaoge buy it. So Yaoge can get profit if Yaoge sell the chicken chop with higher price. Yaoge is famous in the world. AFTER Yaoge has completed one travel, the price of the chicken chop in each city on that travel path will be increased by V .
Input
The first line contains an integer T (0 < T ≤ 10), the number of test cases you need to solve. For each test case, the first line contains an integer N (0 < N ≤ 50000), the number of cities. For each of the next N lines, the i-th line contains an integer W i(0 < W i ≤ 10000), the price of the chicken chop in city i. Each of the next N - 1 lines contains two integers X Y (1 ≤ X, Y ≤ N ), describing a road between city X and city Y . The next line contains an integer Q(0 ≤ Q ≤ 50000), the number of queries. Each of the next Q lines contains three integer X Y V(1 ≤ X, Y ≤ N ; 0 < V ≤ 10000), meaning that Yaoge moves along the path from city X to city Y , and the price of the chicken chop in each city on the path will be increased by V AFTER Yaoge has completed this travel.
Output
For each query, output the maximum profit Yaoge can get. If no positive profit can be earned, output 0 instead.
Sample Input

1
5
1
2
3
4
5
1 2
2 3
3 4
4 5
5
1 5 1
5 1 1
1 1 2
5 1 1
1 2 1

Sample Output

4
0
0
1
0

题目大意::给出一颗树,每个节点的商品价值,有Q个查询,每个查询(u,v,val)表示一个人从u走到v,在路径的某个节点购买一件商品,然后在该节点之后的某个节点把这个商品卖掉,求能获得的最大利润。走完路径后,这条路径的商品价值都 加上val。

思路:树链剖分+线段树,路径商品价值更新就不用多说了,区间更新很简单。主要看一下怎么计算这个最大利润,这个问题可以分治的来考虑,[l,r]的最大利润一定由[l,mid]的最大利润和[mid+1,r]的最大利润和Rmax-Lmin这三者的某一个转移而来。因此我们在建线段树的时候就可以维护出这些信息。下一个问题就是如何合并链,因此树链剖分得到的是很多条链,每条链对应一个区间,我们计算u到v的路径其实就是得到很多分散的区间,在块与块合并的时候,方向是一个很重要的问题。在树中从节点u到节点v的路径要么是一条先升后降的路径,要么是一条下降的路径,节点u向上爬的时候,是逆序访问区间的,而节点v向上爬的时候是正序访问区间的,因为从u到v的下降过程是顺序的。因此线段树中要维护两个利润的最大值,一个正序,一个逆序。链与链如何合并呢?下面看一张图:(此时为节点u向上爬的过程)

设 maxu、minu分别存储u经过的链中的最大值和最小值,maxv、minv同理,假设我们当前查询出了第k块的Maxprice、MAX、MIN,那么最优解 a n s = m a x ( M a x p r i c e , M A X − m i n u , m a x v − M I N ) ans=max(Maxprice,MAX-minu,maxv-MIN) ans=max(MaxpriceMAXminumaxvMIN)
节点v向上爬的过程与这个类似。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#define INF 1e16
using namespace std;
typedef long long ll;
const int maxn=5e4+5;

struct node
{
    int l,r;
    ll MAX,MIN,val1,val2,lazy;
}tree[maxn<<2]; //线段树

int siz[maxn];//子树大小
int son[maxn];//重儿子
int fa[maxn];//父节点
int deep[maxn];//深度
int top[maxn];//所在链链顶
int pos[maxn];//dfs序编号
ll v[maxn];//映射到序列后的值 线段树依照此序列建树
ll a[maxn];//树上各节点的值
vector<int> vec[maxn];//存边
int n,m,tot=0;

void dfs1(int u,int f)//处理出重儿子 深度 父亲 子树大小等信息
{
    siz[u]=1;
    son[u]=0;
    fa[u]=f;
    deep[u]=deep[f]+1;
    int to;
    for(int i=0;i<vec[u].size();i++)
    {
        to=vec[u][i];
        if(to!=f)
        {
            dfs1(to,u);
            siz[u]+=siz[to];
            if(siz[son[u]]<siz[to])
                son[u]=to;
        }
    }
}

void dfs2(int u,int f,int k)//处理出 链顶 dfs序 映射后的值 等信息
{
    top[u]=k;
    pos[u]=++tot;
    v[tot]=a[u];
    if(son[u])
        dfs2(son[u],u,k);
    int to;
    for(int i=0;i<vec[u].size();i++)
    {
        to=vec[u][i];
        if(to!=f&&to!=son[u])
            dfs2(to,u,to);
    }
}

inline void down(int i)
{
    int l=i<<1,r=i<<1|1;
    tree[l].lazy+=tree[i].lazy;
    tree[r].lazy+=tree[i].lazy;
    tree[l].MAX+=tree[i].lazy;
    tree[l].MIN+=tree[i].lazy;
    tree[r].MAX+=tree[i].lazy;
    tree[r].MIN+=tree[i].lazy;
    tree[i].lazy=0;
}

inline void up(int i)
{
    int l=i<<1,r=i<<1|1;
    tree[i].val1=max(tree[l].val1,tree[r].val1);
    tree[i].val1=max(tree[i].val1,tree[r].MAX-tree[l].MIN);
    tree[i].val2=max(tree[l].val2,tree[r].val2);
    tree[i].val2=max(tree[i].val2,tree[l].MAX-tree[r].MIN);
    tree[i].MAX=max(tree[l].MAX,tree[r].MAX);
    tree[i].MIN=min(tree[l].MIN,tree[r].MIN);
}

void build(int i,int l,int r)
{
    tree[i].l=l,tree[i].r=r,tree[i].lazy=0;
    if(l==r)
    {
        tree[i].MAX=tree[i].MIN=v[l];
        tree[i].val1=tree[i].val2=0;
        return ;
    }
    int mid=(l+r)>>1;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
    up(i);
}

void update(int i,int l,int r,ll v)
{
    if(tree[i].l==l&&tree[i].r==r)
    {
        tree[i].lazy+=v;
        tree[i].MAX+=v;
        tree[i].MIN+=v;
        return ;
    }
    if(tree[i].lazy)
        down(i);
    int mid=(tree[i].l+tree[i].r)>>1;
    if(r<=mid)
        update(i<<1,l,r,v);
    else if(l>mid)
        update(i<<1|1,l,r,v);
    else
        update(i<<1,l,mid,v),
        update(i<<1|1,mid+1,r,v);
    up(i);
}

ll query(int i,int l,int r,int flag,ll &MAX,ll &MIN)
{
    if(tree[i].l==l&&tree[i].r==r)
    {
        MAX=tree[i].MAX;
        MIN=tree[i].MIN;
        if(flag==1)//从左到右
            return tree[i].val1;
        else //从右到左
            return tree[i].val2;
    }
    if(tree[i].lazy)
        down(i);
    int mid=(tree[i].l+tree[i].r)>>1;
    if(r<=mid)
        return query(i<<1,l,r,flag,MAX,MIN);
    else if(l>mid)
        return query(i<<1|1,l,r,flag,MAX,MIN);
    else
    {
        ll lmax,lmin,rmax,rmin;
        ll lc=query(i<<1,l,mid,flag,lmax,lmin);
        ll rc=query(i<<1|1,mid+1,r,flag,rmax,rmin);
        ll tmp=max(lc,rc);
        if(flag==1) //从左到右
            tmp=max(tmp,rmax-lmin);
        else
            tmp=max(tmp,lmax-rmin);
        MAX=max(lmax,rmax);
        MIN=min(lmin,rmin);
        return tmp;
    }
}

ll solve(int u,int v,ll val)
{
    ll ans=0;
    ll maxu,minu,maxv,minv,MAX,MIN;
    maxu=maxv=0,minu=minv=INF;
    while(top[u]!=top[v])
    {
        if(deep[top[u]]>deep[top[v]])//u向上爬 区间顺序 从右到左
        {
            ans=max(ans,query(1,pos[top[u]],pos[u],2,MAX,MIN));
            ans=max(ans,MAX-minu);//该链最大值-走过的链中的最小值
            ans=max(ans,maxv-MIN);//从该链以后到终点的最大值-该链的最小值
            maxu=max(maxu,MAX);
            minu=min(minu,MIN);
            update(1,pos[top[u]],pos[u],val);
            u=fa[top[u]];
        }
        else//v向上爬 区间顺序 从左到右
        {
            ans=max(ans,query(1,pos[top[v]],pos[v],1,MAX,MIN));
            ans=max(ans,MAX-minu);
            ans=max(ans,maxv-MIN);
            maxv=max(maxv,MAX);
            minv=min(minv,MIN);
            update(1,pos[top[v]],pos[v],val);
            v=fa[top[v]];
        }
    }
    if(deep[u]>deep[v])
    {
        ans=max(ans,query(1,pos[v],pos[u],2,MAX,MIN));
        ans=max(ans,MAX-minu);
        ans=max(ans,maxv-MIN);
        update(1,pos[v],pos[u],val);
    }
    else
    {
        ans=max(ans,query(1,pos[u],pos[v],1,MAX,MIN));
        ans=max(ans,MAX-minu);
        ans=max(ans,maxv-MIN);
        update(1,pos[u],pos[v],val);
    }
    return ans;
}

inline void prework()
{
    scanf("%d",&n);
    int u,v;
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
    for(int i=1;i<=n;i++)
        while(!vec[i].empty())
            vec[i].pop_back();
    for(int i=1;i<n;i++)
    {
        scanf("%d %d",&u,&v);
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    dfs1(1,0);
    dfs2(1,0,1);
    build(1,1,n);
}

inline void mainwork()
{
    int u,v;
    ll val;
    scanf("%d",&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d%lld",&u,&v,&val);
        printf("%lld\n",solve(u,v,val));
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        tot=0;
        memset(siz,0,sizeof(siz));
        memset(son,0,sizeof(son));
        prework();
        mainwork();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值