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

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

 
题意
给你一棵树,每次询问, 从x到y的有向路径,路径上任意一点有一个商品,要求在路径上任意一点i买商品,然后在它之后的点j卖掉,求最多能赚多少钱。
然后把路径上的每个点的权值加上一个给定的值。

题解

因为路径有方向,所以肯定不是最大值减最小值那么简单,但是树链剖分肯定是少不了的,因为树剖可以把树上问题转化成区间问题。

主要是在一个区间里,线段树如何解决这个问题。

我们可以先想一想线段树主要处理什么类型的问题。

线段树是先将区间分成两半,然后依次处理两边,再整合到一起,顺着这个思路想。

我们假设左右区间已经得到了答案,那么我们整个区间的答案就有三种可能:

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的更多相关文章

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

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

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

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

  3. HDU5052 Yaoge’s maximum profit(LCT)

    典型的LCT操作,但是维护的是一个序列最左边减最右边的最小值,所以要维护左边减右边的最小值del[0]和一个右边减左边的最小值del[1](因为rev标记swap的时候对应的值也要交换).维护的时候d ...

  4. hdu 5052 树链剖分

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

  5. HDU 5052 LCT

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

  6. [Educational Round 59][Codeforces 1107G. Vasya and Maximum Profit]

    咸鱼了好久...出来冒个泡_(:з」∠)_ 题目连接:1107G - Vasya and Maximum Profit 题目大意:给出\(n,a\)以及长度为\(n\)的数组\(c_i\)和长度为\( ...

  7. Maximum profit of stocks

    https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...

  8. Maximum Profit

    Maximum Profit You can obtain profits from foreign exchange margin transactions. For example, if you ...

  9. Codeforces 1107G Vasya and Maximum Profit 线段树最大子段和 + 单调栈

    Codeforces 1107G 线段树最大子段和 + 单调栈 G. Vasya and Maximum Profit Description: Vasya got really tired of t ...

随机推荐

  1. 消息队列kafka

    消息队列kafka   为什么用消息队列 举例 比如在一个企业里,技术老大接到boss的任务,技术老大把这个任务拆分成多个小任务,完成所有的小任务就算搞定整个任务了. 那么在执行这些小任务的时候,可能 ...

  2. 33.使用默认的execAndWait拦截器

    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 当我们进行数据库查询等相关的操作时,如果服务器负荷过重可能不能及时把数据查询 ...

  3. eclipse中使用maven的 maven install

    windows -> preferences -> Java -> Installed JREs 在default VM arguments 中添加 -Dmaven.multiMod ...

  4. css继承和层叠

    在前面介绍了如何利用文档结构和css选择器为元素应用各种丰富的样式,今天来好好聊聊css的层叠和继承,先说说概念. 继承:一个元素向其后代元素传递属性值所采用的机制,说的通俗点,就是元素的某些属性可以 ...

  5. Going Home(最小费用最大流)

    Going Home http://poj.org/problem?id=2195 Time Limit: 1000MS   Memory Limit: 65536K Total Submission ...

  6. leetcode 13 Roman to Integer 罗马数组转整型

    描述: 将一个字符串表示的罗马数字转为整数,范围0~3999 解决: 如果后一个比前一个大,则表示减,没什么技巧. map<}, {}, {}, {}, {}, {}, {}}; int rom ...

  7. How To Check Member In Window VS With CplusPlus?

    实例说明 下面这个实例代码, 快速举例了在Win32应用程序下,对于内存的泄漏检查. 其中的原理,目前本人还是不太的理解. 只是记录了使用方法. 以后,看情况,会更新的. #ifdef _WIN32 ...

  8. Java 设计模式系列(五)原型模式

    Java 设计模式系列(五)原型模式 原型模式属于对象的创建模式.通过给出一个原型对象来指明所有创建的对象的类型,然后用复制这个原型对象的办法创建出更多同类型的对象.这就是选型模式的用意. 一.原型模 ...

  9. 拒绝枯燥,有意思的 Loading 页面动效设计

    互联网时代,网络“提速”日益频繁,人们打开Web或软件的速度越来越快,一般页面缓冲和加载地过程也是几不可查.然而,在某些情况下,例如软件急需加载大量页面,首页急需加载大量内容,用户下载文件过大,甚至是 ...

  10. 斜杠反斜杠,去空格\xa0,连接函数join()

    1斜杠反斜杠 斜杠:/.反斜杠:\. 反斜杠\,在windows系统中用来表示目录. 而在unix系统中,/表示目录.由于web遵循unix命名,所以在网址(URL)中,/表示目录. 在unix系统中 ...