Yaoge’s maximum profit

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 516    Accepted Submission(s): 150

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
 
Source
 

路径大值与小值差值的最大值,当中满足小值在大值前面出现,添�求u->:v的路径上答案,能够先u->make_root(),然后v->access(),然后输出答案就能够了。

代码:

/* ***********************************************
Author :rabbit
Created Time :2014/11/2 19:01:37
File Name :4.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=100100;
struct Node *null;
struct Node{
Node *ch[2],*fa;
int Max,Min,mm,rmm,rev,add,val;
inline void clear(int _val){
fa=ch[0]=ch[1]=null;
Max=Min=val=_val;
rev=add=mm=rmm=0;
}
inline void push_up(){
if(this==null)return;
mm=0;
mm=max(mm,ch[0]->mm);
mm=max(mm,ch[1]->mm);
mm=max(mm,max(val,ch[1]->Max)-ch[0]->Min);
mm=max(mm,ch[1]->Max-min(val,ch[0]->Min));
rmm=0;
rmm=max(rmm,ch[0]->rmm);
rmm=max(rmm,ch[1]->rmm);
rmm=max(rmm,max(val,ch[0]->Max)-ch[1]->Min);
rmm=max(rmm,ch[0]->Max-min(val,ch[1]->Min));
Max=max(val,max(ch[0]->Max,ch[1]->Max));
Min=min(val,min(ch[0]->Min,ch[1]->Min));
}
inline void setc(Node *p,int d){
ch[d]=p;
p->fa=this;
}
inline bool d(){
return fa->ch[1]==this;
}
inline bool isroot(){
return fa==null||fa->ch[0]!=this&&fa->ch[1]!=this;
}
inline void flip(){
if(this==null)return;
swap(ch[0],ch[1]);
rev^=1;
swap(mm,rmm);
}
inline void update_add(int w){
if(this==null)return;
Max+=w;
Min+=w;
val+=w;
add+=w;
}
inline void push_down(){
if(add){
ch[0]->update_add(add);
ch[1]->update_add(add);
add=0;
}
if(rev){
ch[0]->flip();
ch[1]->flip();
rev=0;
}
}
inline void go(){
if(!isroot())fa->go();
push_down();
}
inline void rot(){
Node *f=fa,*ff=fa->fa;
int c=d(),cc=fa->d();
f->setc(ch[!c],c);
this->setc(f,!c);
if(ff->ch[cc]==f)ff->setc(this,cc);
else this->fa=ff;
f->push_up();
}
inline Node *splay(){
go();
while(!isroot()){
if(!fa->isroot())
d()==fa->d()?fa->rot():rot();
rot();
}
push_up();
return this;
}
inline Node *access(){
for(Node *p=this,*q=null;p!=null;q=p,p=p->fa){
p->splay()->setc(q,1);
p->push_up();
}
return splay();
}
inline Node *find_root(){
Node *x;
for(x=access();x->push_down(),x->ch[0]!=null;x=x->ch[0]);
return x;
}
void make_root(){
access()->flip();
}
};
Node pool[maxn],*tail;
Node*node[maxn];
struct Edge{
int next,to;
}edge[maxn*2];
int head[maxn],tol;
inline void addedge(int u,int v){
edge[tol].to=v;
edge[tol].next=head[u];
head[u]=tol++;
}
void dfs(int u,int pre){
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(v==pre)continue;
node[v]->fa=node[u];
dfs(v,u);
}
}
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
int n,m,T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
tail=pool;
null=tail++;
null->val=null->mm=null->rmm=0;
null->Max=-INF;null->Min=INF;
null->ch[0]=null->ch[1]=null->fa=null;
null->add=null->rev=0;
for(int i=1;i<=n;i++){
int w;
scanf("%d",&w);
node[i]=tail++;
node[i]->clear(w);
}
memset(head,-1,sizeof(head));tol=0;
for(int i=1;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
dfs(1,1);
scanf("%d",&m);
while(m--){
int u,v,d;
scanf("%d%d%d",&u,&v,&d);
node[u]->make_root();
node[v]->access();
printf("%d\n",node[v]->mm);
node[v]->update_add(d);
}
}
return 0;
}

HDU 5052 LCT的更多相关文章

  1. hdu 5052 树链剖分

    Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  2. Yaoge’s maximum profit HDU - 5052

    Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  3. Hdu 2475-Box LCT,动态树

    Box Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  4. hdu 4010 Lct动态链接树

    #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include & ...

  5. HDU 5052 Yaoge’s maximum profit 光秃秃的树链拆分 2014 ACM/ICPC Asia Regional Shanghai Online

    意甲冠军: 特定n小点的树权. 以下n每一行给出了正确的一点点来表达一个销售点每只鸡价格的格 以下n-1行给出了树的侧 以下Q操作 Q行 u, v, val 从u走v,程中能够买一个鸡腿,然后到后面卖 ...

  6. Hdu 5052 Yaoge’s maximum profit(树链剖分)

    题目大意: 给出一棵树.每一个点有商店.每一个商店都有一个价格,Yaoge每次从x走到y都能够在一个倒卖商品,从中得取利益.当然,买一顶要在卖之前.可是没次走过一条路,这条路上的全部商品都会添加一个v ...

  7. HDU 5052 /// 树链剖分+线段树区间合并

    题目大意: 给定n (表示树有n个结点) 接下来n行给定n个点的点权(在这个点上买鸡或者卖鸡的价钱就是点权) 接下来n-1行每行给定 x y 表示x结点和y结点之间有一条边 给定q (表示有q个询问) ...

  8. Hdu 3966-Aragorn's Story LCT,动态树

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3966 Aragorn's Story Time Limit: 10000/3000 MS (Java/Ot ...

  9. hdu 5398 动态树LCT

    GCD Tree Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

随机推荐

  1. centos5.5字体为方块问题的解决_深入学习编程_百度空间

    centos5.5字体为方块问题的解决_深入学习编程_百度空间 centos5.5字体为方块问题的解决 一.yum -y install fonts-chinese二.yum -y install f ...

  2. poj 1011 Sticks ,剪枝神题

    木棒 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 118943 Accepted: 27429 Description 乔治拿 ...

  3. 该项目的建设maven片:4.协调和依赖,spring依赖注入demo

    源码下载 协调 <groupId>com.demo.animal</groupId> <artifactId>animal-core</artifactId& ...

  4. Android拖动和缩放图片

    Android拖动和缩放图片 2014年5月9日 我们在使用应用其中常常须要浏览图片.比方在微信其中.点击图片之后能够对图片进行缩放. 本博客介绍怎样对图片进行拖拽和缩放.这首先要了解Android中 ...

  5. [置顶] android系统功能调用大全

    1.从google搜索内容  Intent intent = new Intent();  intent.setAction(Intent.ACTION_WEB_SEARCH);  intent.pu ...

  6. ubuntu配置jdk脚本以及导致开不了机的解决方案

    关于在那个文件里配置jdk脚本,有些大牛总结了四个地方,大体就是ubuntu系统启动后会默认加载的四个地方.例如:/etcenvironment,/etc/profile这两个文件处于系统层面的,还有 ...

  7. Unity3D游戏开发从零单排(四) - 制作一个iOS游戏

    提要 此篇是一个国外教程的翻译,尽管有点老,可是适合新手入门. 自己去写代码.debug,布置场景,能够收获到非常多.游戏邦上已经有前面两部分的译文,这里翻译的是游戏的最后一个部分. 欢迎回来 在第一 ...

  8. Phaser是一款专门用于桌面及移动HTML5 2D游戏开发的开源免费框架

    Phaser是一款专门用于桌面及移动HTML5 2D游戏开发的开源免费框架,提供JavaScript和TypeScript双重支持,内置游戏对象的物理属性,采用Pixi.js引擎以加快Canvas和W ...

  9. 用java代码实现环圈报数

    环圈报数就是围一圈人,每一次数数数到三的人自动出圈,再接着数, 用数据结构的思想实现 public class Count3Quit {     public static void main(Str ...

  10. iOS_ScrollView的自己主动布局

    ScrollView的自己主动布局稍显麻烦.但也是有规律可循, 下面就是仅竖向滑动的scrollView加入约束的固定做法 1.在控制器的view加入一个label.取名做anchor 2.给anch ...