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 ...
随机推荐
- HTTP 协议基础
HTTP 协议的主要特点可概括如下: 1.支持客户/服务器模式. 2.简单快速:客户向服务器请求服务时,只需传送请求方法和路径.请求方法常用的有GET.HEAD.POST.每种方法规定了客户与服务器联 ...
- 人脸检测及识别python实现系列(2)——识别出人脸
人脸检测及识别python实现系列(2)——识别出人脸 http://www.cnblogs.com/neo-T/p/6430583.html
- intellij idea15,SVN commit file提示No changes detected
备注:我用的intellij 15,已经配置了SVN.且我的工程是从SVN导出的 遇到的问题:Subversion提交代码时提示No changes detected,当然新文件想要add也是选项 ...
- Visual Studio工具 vcpkg简介
博客参考: https://blog.csdn.net/cjmqas/article/details/79282847#43-%E7%A7%BB%E9%99%A4%E5%85%A8%E5%B1%80% ...
- PHP逻辑运算符中的and和&&以及or和||是有区别的
下图是PHP的逻辑运算符: 看图中and和&&都是“与”,而or和||都是“或”,初开起来没有区别,但实际上这里面有一个优先级别的区别,即: &&和||的优先级别要高于 ...
- TableLayout 中不显示动态添加的tableRow
下面的代码不显示: TableRow lay = new TableRow(layIndex.getContext()); lay.setLayoutParams(lpRow); //layIndex ...
- 视觉SLAM的数学基础 第一篇 3D空间的位置表示
视觉SLAM中的数学基础 第一篇 3D空间的位置表示 前言 转眼间一个学期又将过去,距离我上次写<一起做RGBD SLAM>已经半年之久.<一起做>系列反响很不错,主要由于它为 ...
- wamp如何设置数据库的密码
WAMP安装好后,MySQL密码是为空的,那么要如何修改呢?其实很简单,通过几条指令就行了,下面我就一步步来操作. 首先,通过WAMP打开mysql控制台. 提示输入密码,因为现在是空,所以直接按回车 ...
- java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector解决方法
java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector解决方法 错误描述:java.lang.NoClassDefFoundErro ...
- ASP.NET MVC与ASP.NET Web API的区别(转)
出处:http://blog.csdn.net/wangzl1163/article/details/72676616 MVC主要用来构建网站,既关心数据也关心页面展示,而Web API只关注数据 W ...