Yaoge’s maximum profit HDU - 5052
Yaoge’s maximum profit
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=5052
Problem Description
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
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
Sample Input
Sample Output
Source
题解
因为路径有方向,所以肯定不是最大值减最小值那么简单,但是树链剖分肯定是少不了的,因为树剖可以把树上问题转化成区间问题。
主要是在一个区间里,线段树如何解决这个问题。
我们可以先想一想线段树主要处理什么类型的问题。
线段树是先将区间分成两半,然后依次处理两边,再整合到一起,顺着这个思路想。
我们假设左右区间已经得到了答案,那么我们整个区间的答案就有三种可能:
1.左区间的答案
2.右区间的答案
3.右边的最大值-左边的最大值
所以我们只需要记录区间最大值、最小值和答案即可,如果有lazy标记,就像平常一样处理就行。
还有一点需要注意就是x-->y那么x-->lca(x,y)区间是倒过来的,我们需要存赚的最小值,然后取相反数。
细节还是有点小麻烦,最好对树链剖分打的比较熟,并有一定的理解,再来切这道题。
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 200050
const int INF=1e9;
struct Tree{int l,r,j,mi,mx,qmi,qmx;}tr[N<<];
struct Query{int mi,mx,qmi,qmx;};
struct Edge{int from,to,s;}edges[N<<];
int n,m,w[N];
int tot,last[N];
int cnt,fa[N],dp[N],size[N],son[N],rk[N],kth[N],top[N];
template<typename T>void read(T&x)
{
ll k=; char c=getchar();
x=;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit();
while(isdigit(c))x=x*+c-'',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
void AddEdge(int x,int y)
{
edges[++tot]=Edge{x,y,last[x]};
last[x]=tot;
}
template<typename T>void he(T &c,T a,T b)
{
c.mx=max(a.mx,b.mx);
c.mi=min(a.mi,b.mi);
c.qmi=min(a.qmi,b.qmi);
c.qmx=max(a.qmx,b.qmx);
c.qmi=min(c.qmi,b.mi-a.mx);
c.qmx=max(c.qmx,b.mx-a.mi);
}
void push_up(int x)
{
Tree &a=tr[x<<],&b=tr[x<<|];
int len=tr[x].r-tr[x].l+;
if (len>) he(tr[x],a,b);
else tr[x].mx=tr[x].mi=;
tr[x].mx+=tr[x].j;
tr[x].mi+=tr[x].j;
}
void push_down(int x)
{
Tree &a=tr[x<<],&b=tr[x<<|];
a.j+=tr[x].j;
b.j+=tr[x].j;
push_up(x<<);
push_up(x<<|);
tr[x].j=;
}
void bt(int x,int l,int r)
{
tr[x].l=l; tr[x].r=r; tr[x].j=;
if (l==r)
{
tr[x].qmi=tr[x].qmx=;
tr[x].j=w[kth[l]];
push_up(x);
return;
}
int mid=(l+r)>>;
bt(x<<,l,mid);
bt(x<<|,mid+,r);
push_up(x);
}
void update(int x,int l,int r,int tt)
{
if (l<=tr[x].l&&tr[x].r<=r)
{
tr[x].j+=tt;
push_up(x);
return;
}
int mid=(tr[x].l+tr[x].r)>>;
push_down(x);
if(l<=mid)update(x<<,l,r,tt);
if(mid<r)update(x<<|,l,r,tt);
push_up(x);
}
Query query(int x,int l,int r)
{
if (l<=tr[x].l&&tr[x].r<=r)
return Query{tr[x].mi,tr[x].mx,tr[x].qmi,tr[x].qmx};
int mid=(tr[x].l+tr[x].r)>>;
Query tp1=Query{INF,-INF,,},tp2=Query{INF,-INF,,},tp;
push_down(x);
if (l<=mid)tp1=query(x<<,l,r);
if (mid<r)tp2=query(x<<|,l,r);
push_up(x);
he(tp,tp1,tp2);
return tp;
}
void dfs1(int x,int pre)
{
fa[x]=pre;
dp[x]=dp[pre]+;
size[x]=;
son[x]=;
for(int i=last[x];i;i=edges[i].s)
{
Edge &e=edges[i];
if (e.to==pre)continue;
dfs1(e.to,x);
size[x]+=size[e.to];
if (size[e.to]>size[son[x]])son[x]=e.to;
}
}
void dfs2(int x,int y)
{
rk[x]=++cnt;
kth[cnt]=x;
top[x]=y;
if (son[x]==)return;
dfs2(son[x],y);
for(int i=last[x];i;i=edges[i].s)
{
Edge &e=edges[i];
if (e.to==son[x]||e.to==fa[x])continue;
dfs2(e.to,e.to);
}
}
int get_max(int x,int y)
{
int fx=top[x],fy=top[y],ans=;
Query tpx={INF,-INF,,},tpy={INF,-INF,,},tp;
while(fx!=fy)
{
if (dp[fx]>dp[fy])
{
tp=query(,rk[fx],rk[x]);
he(tpx,tp,tpx);
x=fa[fx]; fx=top[x];
}
else
{
tp=query(,rk[fy],rk[y]);
he(tpy,tp,tpy);
y=fa[fy]; fy=top[y];
}
}
if (dp[x]>dp[y])
tp=query(,rk[y],rk[x]),he(tpx,tp,tpx);
else
tp=query(,rk[x],rk[y]),he(tpy,tp,tpy);
ans=max(-tpx.qmi,tpy.qmx);
ans=max(ans,tpy.mx-tpx.mi);
return ans;
}
void change(int x,int y,int tt)
{
int fx=top[x],fy=top[y];
while(fx!=fy)
{
if (dp[fx]<dp[fy])swap(x,y),swap(fx,fy);
update(,rk[fx],rk[x],tt);
x=fa[fx]; fx=top[x];
}
if (dp[x]<dp[y])swap(x,y),swap(fx,fy);
update(,rk[y],rk[x],tt);
}
void work()
{
read(n);
for(int i=;i<=n;i++)read(w[i]);
for(int i=;i<=n-;i++)
{
int x,y;
read(x); read(y);
AddEdge(x,y);
AddEdge(y,x);
}
dfs1(,);
dfs2(,);
bt(,,n);
read(m);
for(int i=;i<=m;i++)
{
int x,y,tt;
read(x); read(y); read(tt);
printf("%d\n",get_max(x,y));
change(x,y,tt);
}
}
void clear()
{
cnt=; tot=;
memset(last,,sizeof(last));
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
int q;
read(q);
while(q--)
{
clear();
work();
}
}
Yaoge’s maximum profit HDU - 5052的更多相关文章
- Hdu 5052 Yaoge’s maximum profit(树链剖分)
题目大意: 给出一棵树.每一个点有商店.每一个商店都有一个价格,Yaoge每次从x走到y都能够在一个倒卖商品,从中得取利益.当然,买一顶要在卖之前.可是没次走过一条路,这条路上的全部商品都会添加一个v ...
- HDU 5052 Yaoge’s maximum profit 光秃秃的树链拆分 2014 ACM/ICPC Asia Regional Shanghai Online
意甲冠军: 特定n小点的树权. 以下n每一行给出了正确的一点点来表达一个销售点每只鸡价格的格 以下n-1行给出了树的侧 以下Q操作 Q行 u, v, val 从u走v,程中能够买一个鸡腿,然后到后面卖 ...
- HDU5052 Yaoge’s maximum profit(LCT)
典型的LCT操作,但是维护的是一个序列最左边减最右边的最小值,所以要维护左边减右边的最小值del[0]和一个右边减左边的最小值del[1](因为rev标记swap的时候对应的值也要交换).维护的时候d ...
- hdu 5052 树链剖分
Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- HDU 5052 LCT
Yaoge's maximum profit Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- [Educational Round 59][Codeforces 1107G. Vasya and Maximum Profit]
咸鱼了好久...出来冒个泡_(:з」∠)_ 题目连接:1107G - Vasya and Maximum Profit 题目大意:给出\(n,a\)以及长度为\(n\)的数组\(c_i\)和长度为\( ...
- Maximum profit of stocks
https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...
- Maximum Profit
Maximum Profit You can obtain profits from foreign exchange margin transactions. For example, if you ...
- Codeforces 1107G Vasya and Maximum Profit 线段树最大子段和 + 单调栈
Codeforces 1107G 线段树最大子段和 + 单调栈 G. Vasya and Maximum Profit Description: Vasya got really tired of t ...
随机推荐
- JS表单常见表达式(正则)
整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$ 只能输入数字:"^[0-9]*$". 只能输入n位的数字:"^\d{n}$". 只能输入至少n ...
- MPI 并行奇偶交换排序 + 集合通信函数 Sendrecv() Sendvecv_replace()
▶ <并行程序设计导论>第三章的例子程序 ● 代码 #include <stdio.h> #include <mpi.h> #include <stdlib. ...
- Eclipse使用xdoclet1.2.3 生成hibernate配置文件和映射文件
用ant和xdoclet生成hibernate配置文件可以为我们省去很多配置的操作,废话不多说,直接给栗子: 测试环境: eclipse:Eclipse Java EE IDE for Web Dev ...
- VBS 创建快捷方式
Dim Shell,DesktopPath,link Set Shell = CreateObject("WScript.Shell") DesktopPath = Shell.S ...
- JVM 理解
https://blog.csdn.net/hjxgood/article/details/53896229 一.什么是JVM JVM是Java Virtual Machine(Java虚拟机)的缩写 ...
- linux vim操作技巧
安装: NERDTree 从 http://www.vim.org/scripts/script.php?script_id=1658 下载 unzip NERD_tree.zip cd ~/.vi ...
- TCP/IP知识总结(TCP/IP协议族读书笔记一)
一.简述TCP/IP协议 Transmission Control Protocol/Internet Protocol的简写,即传输控制协议/互联网互联协议,又名网络通信协议.是Internet最基 ...
- JDK8新特性:函数式接口@FunctionalInterface的使用说明
我们常用的一些接口Callable.Runnable.Comparator等在JDK8中都添加了@FunctionalInterface注解. 通过JDK8源码javadoc,可以知道这个注解有以下特 ...
- cannot nest '/dubboService/src/main/resources' inside '/dubboService/src/main' .To enable the nesting exclude '/resources' from '/dubboService/src/main'
eclipse Maven--->update Project时出现以上错误: cannot nest '/dubboService/src/main/resources' inside '/d ...
- python 字符串报错问题
http://jingyan.baidu.com/article/25648fc1a96dd49191fd00c0.html 解决'ascii' codec can't encode characte ...