hdu5052

原创 2018年04月16日 23:54:58

http://www.elijahqi.win/archives/830
tjoi2015我写错了 导致这题wa半天,tjoi数据太弱,没跑出来是错的

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 Wi(0 < Wi ≤ 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

#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 55000
#define inf 0x7fffffff
using namespace std;
inline int read(){
    int x=0;char ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
struct node{
    int y,next;
}data[N<<1];
struct node1{
    int max,min,ans,ans1;
    node1(){
        ans=ans1=max=0;min=inf;
    }
};
struct node2{
    int l,r,left,right,max,min,ans,ans1,lazy;
}tree[N<<2];
int size[N],id[N],fa[N],son[N],tp[N],a[N],dep[N],h[N],cnt,root,n,num,m,w[N];
void dfs1(int x){
    size[x]=1;
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;
        if(fa[x]==y) continue;
        dep[y]=dep[x]+1;fa[y]=x;dfs1(y);size[x]+=size[y];
        if (size[y]>size[son[x]]) son[x]=y;
    }
}
void dfs2(int x,int top){
    id[x]=++cnt;tp[x]=top;w[cnt]=a[x];
    if (son[x]) dfs2(son[x],top);
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;
        if (y==fa[x]||y==son[x]) continue;
        dfs2(y,y);
    }
}
inline void update(int x){
    int l=tree[x].left,r=tree[x].right;
    tree[x].ans=max(tree[r].max-tree[l].min,max(tree[l].ans,tree[r].ans));
    tree[x].ans1=max(tree[l].max-tree[r].min,max(tree[l].ans1,tree[r].ans1));
    tree[x].min=min(tree[l].min,tree[r].min);
    tree[x].max=max(tree[l].max,tree[r].max);

}
void print(int x){
    if (tree[x].left) print(tree[x].left);
    printf("%d %d %d %d %d %d %d\n",tree[x].l,tree[x].r,tree[x].max,tree[x].min,tree[x].ans,tree[x].ans1,tree[x].lazy);
    if (tree[x].right) print(tree[x].right);
}
void build(int &x,int l,int r){
    x=++cnt;tree[x].l=l;tree[x].r=r;tree[x].lazy=0;
    if(l==r){
        tree[x].min=tree[x].max=w[l];
        tree[x].ans=tree[x].ans1=0;return;
    }
    int mid=l+r>>1;
    build(tree[x].left,l,mid);build(tree[x].right,mid+1,r);
    update(x);
}
inline node1 update1(node1 tmp1,node1 tmp2){
    node1 ans1;
    ans1.ans=max(tmp2.max-tmp1.min,max(tmp1.ans,tmp2.ans));
    ans1.ans1=max(tmp1.max-tmp2.min,max(tmp1.ans1,tmp2.ans1));
    ans1.min=min(tmp1.min,tmp2.min);
    ans1.max=max(tmp1.max,tmp2.max);
    return ans1;
}
void pushdown(int x){
    if (!tree[x].lazy) return;
    int l=tree[x].left,r=tree[x].right;
    int lazy=tree[x].lazy;
    tree[l].lazy+=lazy;tree[r].lazy+=lazy;
    tree[l].max+=lazy;tree[r].max+=lazy;
    tree[l].min+=lazy;tree[r].min+=lazy;
    tree[x].lazy=0;
}
node1 query(int rt,int l,int r,int v){
    if(l<=tree[rt].l&&r>=tree[rt].r){
        tree[rt].lazy+=v;tree[rt].max+=v;tree[rt].min+=v;
        node1 tmp;tmp.max=tree[rt].max;tmp.min=tree[rt].min;tmp.ans=tree[rt].ans;tmp.ans1=tree[rt].ans1;
        return tmp;
    }
    int mid=(tree[rt].l+tree[rt].r)>>1;
    node1 tmp1;node1 tmp2;pushdown(rt);
    if (l<=mid) tmp1=query(tree[rt].left,l,r,v);
    if (r>mid) tmp2=query(tree[rt].right,l,r,v);
    update(rt);
    return update1(tmp1,tmp2);
}
inline void swap(int &x,int &y){
    x^=y;y^=x;x^=y;
}
void solve(int x,int y,int v){
    node1 ansl,ansr;
    while (tp[x]!=tp[y]){
        if(dep[tp[x]]>dep[tp[y]]){
            ansl=update1(query(root,id[tp[x]],id[x],v),ansl);
            x=fa[tp[x]];
        }else{
            ansr=update1(query(root,id[tp[y]],id[y],v),ansr);
            y=fa[tp[y]];
        }
    }
    swap(ansl.ans,ansl.ans1);
    if (id[x]<id[y]){
        ansl=update1(ansl,query(root,id[x],id[y],v));
        ansl=update1(ansl,ansr);    
    }else{
        node1 tmpp=query(root,id[y],id[x],v);
        swap(tmpp.ans,tmpp.ans1);
        ansr=update1(tmpp,ansr);
        ansl=update1(ansl,ansr);
    }    
    printf("%d\n",ansl.ans);
//    printf("sdfsdf\n");
    //print(root);
}
int main(){
    freopen("hdu5052.in","r",stdin);
    int T=read();
    while (T--){
        n=read();memset(h,0,sizeof(h));memset(son,0,sizeof(son));num=cnt=0;
        for (int i=1;i<=n;++i) a[i]=read();
        for (int i=1;i<n;++i){
            int x=read(),y=read();
            data[++num].y=y;data[num].next=h[x];h[x]=num;
            data[++num].y=x;data[num].next=h[y];h[y]=num;
        }dep[1]=1;
        dfs1(1);dfs2(1,1);cnt=0;
        //for (int i=1;i<=n;++i) printf("%d ",id[i]);printf("\n");
        build(root,1,n);//print(root);
        m=read();
        for (int i=1;i<=m;++i){
            int x=read(),y=read(),z=read();
            solve(x,y,z);
        }
    }
    return 0;
}
版权声明:辣鸡蒟蒻的blog https://blog.csdn.net/elijahqi/article/details/79968711

hdu5052 Yaoge’s maximum profit 树链剖分

一棵树上,从u走到v,在某点买入,咋之后的某点卖出,求最大利润。 维护正着走和反着走的最大利润。 #include #include #include #include #include #inc...
  • u013044116
  • u013044116
  • 2015-10-03 00:37:13
  • 197

HDU5052 Yaoge’s maximum profit(树链剖分)点权更新,经典题

Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java...
  • u010372095
  • u010372095
  • 2015-05-07 20:52:35
  • 959

HDU 5052 LCT

Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java...
  • u012358934
  • u012358934
  • 2014-11-02 19:57:47
  • 803

hdu5052 LCT

题意:给定每一个节点的权值
  • ahjkl007
  • ahjkl007
  • 2014-09-29 20:07:08
  • 612

HDU 5052 Yaoge’s maximum profit(树链剖分+线段树,2014上海网络赛1011)

题目:Yaoge’s maximum profit
  • hongrock
  • hongrock
  • 2014-09-27 20:18:32
  • 986

HDU5052 树链剖分

题目:HDU5052我做的第一道不全算是模板题的树链剖分题(不会LCT),题目题意很清晰,维护很恶心 题意是这样: 给出一颗树,给出节点上的值和边,每次询问从u到v的过程中先走的值和后走的值的...
  • zlc1114
  • zlc1114
  • 2017-05-02 20:36:37
  • 111

2014上海网络预选赛1011(LCT)HDU5052

Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java...
  • cq_phqg
  • cq_phqg
  • 2014-09-30 18:39:13
  • 632

hdu5052 Yaoge’s maximum profit (树链剖分)

同bzoj3999
  • Icefox_zhx
  • Icefox_zhx
  • 2017-09-11 13:40:35
  • 113
收藏助手
不良信息举报
您举报文章:hdu5052
举报原因:
原因补充:

(最多只允许输入30个字)