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

Yaoge’s maximum profit

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


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

题意:找出从u到v路径上的最大的一个值,这个值由路径后面的数减前面的数

思路:LCT

            要维护四个值

            路径最大值mx

            路径最小值mn

            路径从左到右的最小值L  (将问题转化为了求最小值,路径前面的数减后面的)

            路径从右到左的最小值R

            重点是L和R怎么维护呢

            分析L,可以由以下几种部分组成

            1.左边链的L

            2.右边链的L

            3.左边链的最小值 - val

            4.val - 右边链的最大值

            5.左边链的最小值 - 右边链的最大值

            在这几部分中取最小的就好了,R类似

            然后还有一点要注意,交换左右孩子的时候要交换L和R

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #include 
        
          #include 
          #include 
          
            #include 
           
             #include 
            
              #include 
             
               using namespace std; const int INF = (int)1e9; const int MAXN = 50010; struct node{ node *fa,*p,*ch[2]; int flag; bool root; int mn,mx; int L,R; int val; int cnt; node(){ mx=-INF; mn=INF; L=R=val=cnt=0; fa=p=ch[0]=ch[1]=this; } int cd(){ return p->ch[1]==this ? 1:0; } void setc(node *o,int d){ ch[d]=o; o->p=this; } void setroot(); void pushup(); void add(int w){ cnt+=w; val+=w; mn+=w; mx+=w; } void rev(){ flag^=1; swap(L,R); swap(ch[0],ch[1]); } }Tnull,*null=&Tnull,*vex[MAXN]; void node::setroot() { fa=p; p=null; root=true; } node *newnode(int w) { node *u=(node*)malloc(sizeof(node)); u->ch[0]=u->ch[1]=u->p=u->fa=null; u->flag=0; u->cnt=0; u->val=w; u->mn=u->mx=w; u->L=u->R=0; u->root=true; return u; } void pushdown(node *o) { if(o->cnt){ for(int i=0;i<2;i++)if(o->ch[i]!=null)o->ch[i]->add(o->cnt); o->cnt=0; } if(o->flag){ for(int i=0;i<2;++i)if(o->ch[i]!=null)o->ch[i]->rev(); o->flag^=1; } } void node::pushup() { mx=max(val,max(ch[0]->mx,ch[1]->mx)); mn=min(val,min(ch[0]->mn,ch[1]->mn)); L=min(ch[0]->L,ch[1]->L); L=min(L,min(ch[0]->mn,val)-ch[1]->mx); L=min(L,ch[0]->mn-val); R=min(ch[0]->R,ch[1]->R); R=min(R,min(ch[1]->mn,val)-ch[0]->mx); R=min(R,ch[1]->mn-val); } void pushto(node *o) { node *stk[MAXN]; int cnt=0; while(o!=null){ stk[cnt++]=o; o=o->p; } for(int i=cnt-1;i>=0;--i)pushdown(stk[i]); } void rot(node *o) { node *k=o->p; int d=o->cd(); k->setc(o->ch[d^1],d); k->p->setc(o,k->cd()); o->setc(k,d^1); k->pushup(); if(k->root){ o->fa=k->fa; k->fa=null; k->root=false; o->root=true; } } void splay(node *o) { pushto(o); while(o->p!=null){ if( o->p->p==null)rot(o); else o->cd()==o->p->cd() ? ( rot(o->p) , rot(o) ):( rot(o) , rot(o) ); } o->pushup(); } void access(node *o) { for(node *pre=null;o!=null;pre=o,o=o->fa){ splay(o); o->ch[1]->setroot(); o->setc(pre,1); pre->root=false; } } void makeroot(node *o) { access(o); splay(o); o->rev(); } void query(node *u,node *v,int w) { makeroot(u); access(v); splay(v); printf("%d\n",-v->L); v->add(w); } int head[MAXN]; int edge[MAXN<<1]; int next[MAXN<<1]; int esz; void addedge(int u,int v) { edge[esz]=v; next[esz]=head[u]; head[u]=esz++; } void dfs(int u,int p) { for(int i=head[u];i!=-1;i=next[i]){ int v=edge[i]; if(v==p)continue; vex[v]->fa=vex[u]; dfs(v,u); } } void init() { memset(head,-1,sizeof(head)); esz=0; } int main() { int t; scanf("%d",&t); while(t--){ int n; scanf("%d",&n); init(); for(int i=1;i<=n;i++){ int w; scanf("%d",&w); vex[i]=newnode(w); } for(int i=1;i 
               
              
             
            
           
         
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值