Hdu 5052 Yaoge’s maximum profit 动态树 LCT link-cut tree

Yaoge’s maximum profit

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 520    Accepted Submission(s): 152


Problem Description
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
 

Source

splay维护路径上的最大值max,最小值min,

以及从左到右能得到的最大利益l,从右到左能得到的最大利益r


举例:

根的最大值=max(左孩子最大值,自己的值,右孩子最大值)

根从左到右的最大值=max(左孩子的左到右最大值,右孩子的左到右最大值,max(右孩子最大值,根)-min(根,左孩子最小值))


在be_root的时候,要记得把r,l反转即可



time:2031ms

#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define maxn 500007
#define inf  1000000000
struct Node{
    Node *fa,*ch[2];
    bool rev,root;
    int val,add,max,min,l,r;
};
Node pool[maxn];
Node *nil,*tree[maxn];
int cnt = 0;
void init(){
    cnt = 1;
    nil = tree[0] = pool;
    nil -> l = 0;
    nil -> r = 0;
    nil -> max = -inf;
    nil -> min = inf;
}
inline Node *newnode(int val,Node *f){
    pool[cnt].fa = f;
    pool[cnt].ch[0] = pool[cnt].ch[1] = nil;
    pool[cnt].rev = false;
    pool[cnt].root = true;
    pool[cnt].val = val;
    pool[cnt].add = 0;
    pool[cnt].max = val;
    pool[cnt].min = val;
    pool[cnt].l = 0;
    pool[cnt].r = 0;
    return &pool[cnt++];
}
//splay向上更新信息
void update(Node *x){
    x->max = x->min = x->val;
    if(x->ch[0] != nil){
        x->max < x->ch[0]->max?x->max=x->ch[0]->max:0;
        x->min > x->ch[0]->min?x->min=x->ch[0]->min:0;
    }
    if(x->ch[1] != nil){
        x->max < x->ch[1]->max?x->max=x->ch[1]->max:0;
        x->min > x->ch[1]->min?x->min=x->ch[1]->min:0;
    }
    x->l = x->ch[1]->l < x->ch[0]->l?x->ch[0]->l:x->ch[1]->l;
    x->l = max(x->l,max(x->ch[1]->max,x->val)
                -min(x->ch[0]->min,x->val));
    x->r = x->ch[1]->r < x->ch[0]->r?x->ch[0]->r:x->ch[1]->r;
    x->r = max(x->r,max(x->ch[0]->max,x->val)
                -min(x->ch[1]->min,x->val));
}
void update_rev(Node *x){
    if(x == nil) return ;
    x->rev = !x->rev;
    swap(x->ch[0],x->ch[1]);
    swap(x->l,x->r);
}
void update_add(Node *x,int n){
    if(x == nil) return ;
    x->max += n ;
    x->min += n;
    x->val += n;
    x->add += n;
}
//splay下推信息
void pushdown(Node *x){
    if(x->add != 0){
        update_add(x->ch[0],x->add);
        update_add(x->ch[1],x->add);
        x->add = 0;
    }
    if(x->rev != false){
        update_rev(x->ch[0]);
        update_rev(x->ch[1]);
        x->rev = false;
    }
}
//splay在root-->x的路径下推信息
void push(Node *x){
    if(!x->root) push(x->fa);
    pushdown(x);
}
//将结点x旋转至splay中父亲的位置
void rotate(Node *x){
    Node *f = x->fa, *ff = f->fa;
    int t = (f->ch[1] == x);
    if(f->root) x->root = true, f->root = false;
    else ff->ch[ff->ch[1] == f] = x;
    x->fa = ff;
    f->ch[t] = x->ch[t^1];
    x->ch[t^1]->fa = f;
    x->ch[t^1] = f;
    f->fa = x;
    update(f);
}
//将结点x旋转至x所在splay的根位置
void splay(Node *x){
    push(x);
    Node *f, *ff;
    while(!x->root){
        f = x->fa,ff = f->fa;
        if(!f->root)
            if((ff->ch[1] == f) && (f->ch[1] == x)) rotate(f);
            else rotate(x);
        rotate(x);
    }
    update(x);
}
//将x到树根的路径并成一条path
Node *access(Node *x){
    Node *y = nil;
    while(x != nil){
        splay(x);
        x->ch[1]->root = true;
        (x->ch[1] = y)->root = false;
        update(x);
        y = x;
        x = x->fa;
    }
    return y;
}
//将结点x变成树根
void be_root(Node *x){
    access(x);
    splay(x);
    update_rev(x);
}
struct Edge{
    int v,next;
};
Edge edge[2*maxn];
int head[maxn],ecnt;
int value[maxn];
void add_edge(int u,int v){
    edge[ecnt].v = v;
    edge[ecnt].next = head[u];
    head[u] = ecnt++;
    edge[ecnt].v = u;
    edge[ecnt].next = head[v];
    head[v] = ecnt++;
}
void dfs(int u,int f){
    tree[u] = newnode(value[u],tree[f]);
    for(int i = head[u]; i != -1;i = edge[i].next){
        if(edge[i].v == f) continue;
        dfs(edge[i].v,u);
    }
}
int main(){
    int t,n,w,x,y,v,q;
    Node*p;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        memset(head,-1,sizeof(head));
        ecnt = 0;
        init();
        for(int i = 1;i <= n; i++)
            scanf("%d",&value[i]);
        for(int i = 1; i < n; i++){
            scanf("%d%d",&x,&y);
            add_edge(x,y);
        }
        dfs(1,0);
        scanf("%d",&q);
        while(q--){
            scanf("%d%d%d",&x,&y,&w);
            be_root(tree[x]);
            p = access(tree[y]);
            printf("%d\n",p->l);
            update_add(p,w);
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值