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 ...
随机推荐
- c#,读取二维码
/// <summary>/// 读取二维码/// 读取失败,返回空字符串/// </summary>/// <param name="filename&quo ...
- canvas和svg的区别
讨论关于canvas和svg的区别.首先canvas是html5提供的新元素<canvas>,而svg存在的历史要比canvas久远,已经有十几年了.svg并不是html5专有的标签,最初 ...
- Eclipse Class Decompiler——Java反编译插件(转)
Eclipse Class Decompiler是一款Eclipse插件,整合了多种反编译器,和Eclipse Class Viewer无缝集成,能够很方便的使用插件查看类库源码,进行Debug调试. ...
- 数组/指针/const/字符串常量的使用传值问题
#include<stdio.h> #include<string.h> int main() { ] = "abcd"; //常指针a指向字符串常量&qu ...
- pymysql.err.IntegrityError: (1062, "Duplicate entry 'roxml-ROXML' for key 'PRIMARY'")
在<Python数据挖掘-概念.方法与实践>一书的第3章实体匹配中,如果一路按照作者的代码及SQL语句进行配置运行的话,会出现如题目所示的错误.根据python脚本的执行错误提示显示,错误 ...
- Unity5 Shader Stripping 导致 LightMap 全部丢失的解决方法
当使用 SceneManager.LoadScene 的时候,会自动载入LightMap 和 NavMesh的数据.然后再对MeshRender 进行指定 LightMapIndex 以及 Light ...
- ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var mysql 启动不了 ERROR 2002 ( ...
- 大楼轮廓 · building-outline
[抄题]: 水平面上有 N 座大楼,每座大楼都是矩阵的形状,可以用一个三元组表示 (start, end, height),分别代表其在x轴上的起点,终点和高度.大楼之间从远处看可能会重叠,求出 N ...
- 命令--cut
--按文件大小排序 显示前100行 显示后五列 ll -Sh|head -n 100|cut -d ' ' -f 5- 一.基本语法cut是一个选取命令,以行为单位,用指定分隔符将行切分为若干字段,选 ...
- Java 设计模式系列(五)原型模式
Java 设计模式系列(五)原型模式 原型模式属于对象的创建模式.通过给出一个原型对象来指明所有创建的对象的类型,然后用复制这个原型对象的办法创建出更多同类型的对象.这就是选型模式的用意. 一.原型模 ...