Hdu 5052 Yaoge’s maximum profit 动态树 LCT link-cut tree
来源:互联网 发布:重庆网络口碑营销 编辑:程序博客网 时间:2024/05/17 05:03
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 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
15123451 22 33 44 551 5 15 1 11 1 25 1 11 2 1
Sample Output
40010
Source
2014 ACM/ICPC Asia Regional Shanghai Online
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 1000000000struct 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到树根的路径并成一条pathNode *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
- Hdu 5052 Yaoge’s maximum profit 动态树 LCT link-cut tree
- hdu 5052 Yaoge’s maximum profit
- hdu 5052 Yaoge’s maximum profit(树链剖分)
- 【HDU】5052 Yaoge’s maximum profit 树链剖分
- 【HDU 5052】Yaoge’s maximum profit【树链刨分】
- HDU 5052 Yaoge’s maximum profit (树链剖分 + 线段树)
- Hdu 5052 Yaoge’s maximum profit(树链剖分)
- hdoj 5052 Yaoge’s maximum profit 【树链剖分】
- HDU 5052 Yaoge’s maximum profit(树链剖分+线段树,2014上海网络赛1011)
- HDU 5052 Yaoge’s maximum profit 裸树链剖分 2014 ACM/ICPC Asia Regional Shanghai Online
- hdu 5052 Yaoge’s maximum profit 树链剖分 2014 ACM/ICPC Asia Regional Shanghai Online
- HDU 5052 Yaoge’s maximum profit(树链剖分——点权,区间合并)
- hdu5052 Yaoge’s maximum profit 树链剖分
- hdu5052 Yaoge’s maximum profit (树链剖分)
- 【HDU】4010 Query on The Trees 动态树之Link Cut Tree(LCT)
- 专题总结:动态树 LCT(Link cut tree)
- (新知)动态树--LCT(Link-Cut-Tree)--入门教程
- LCT(Link Cut Tree)动态树学习笔记
- uva534
- String,StringBuffer,StringBuilder拼接操作的效率对比
- regex_match 函数 完全匹配
- linux运行级别
- 对象的深度复制
- Hdu 5052 Yaoge’s maximum profit 动态树 LCT link-cut tree
- 在Unity3D中使用ScriptableObject脚本对象化进行序列化
- LuaJava学习笔记[1]初探Lua
- OpenCL嵌入式图像处理
- Erlang学习三
- OJ 1040 Problem J 求一元二次方程的根
- 开源项目Brave TAH之初识Brave TAH---part1(连载中)
- 使用Rome解析百度RSS
- 你不知道的 页面编码,浏览器选择编码,get,post各种乱码由来