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. 回到顶部最简单的JQuery实现代码

    CSS代码,使用了fixed让对象固定于浏览器窗口: top{position:fixed;bottom:0;right:10px;} jQuery代码,注意正常使用的几个条件:$('#top').c ...

  2. 横向文本框 index获取索引 和 eq 实现

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. asp.net 初级程序员面试题【待续】

     C# 常见的排序方式 冒泡排序(Bubble sort) 堆排序(Heap sort) 插入排序(Insertion sort) 归并排序(Merge sort) 快速排序(Quick sort) ...

  4. 理解C# 4 dynamic(4) – 让人惊艳的Clay(转)

    作者:Justrun名字来自<阿甘正传>,是希望自己能够更更傻一点. link: http://www.cnblogs.com/JustRun1983/p/3529157.html   理 ...

  5. MyBatis 动态SQL注意事项

  6. 清华镜像站安装docker

    https://mirrors.tuna.tsinghua.edu.cn/help/docker-ce/

  7. 无法启动Tomcat, 端口被占用的问题

    这个错误是说这几个端口已经有某个应用程序占用了,所以Tomcat就没法启动了.   出现这个问题的原因可能有以下几种: 情况一:点击运行的时候没有选中页面或Servlet窗口的标签 标签被选中时: 标 ...

  8. MySql 关键字冲突解决办法

    今天把项目发布到另一台机器上时,因为mysql版本不一致,出现了关键字冲突,virtual关键字,不清楚是不是mysql添加的新特性. select * from herb where name=&q ...

  9. Cocoa Touch(二):数据存储CoreData, NSKeyArchiver, NSOutputStream, NSUserDefaults

    应用程序离不开数据的永久存储,有两种方式实现存储:数据库和文本文件. 作为存储管理器,最基本的功能就是增删改查了. CoreData 1.插入 AppDelegate *app = [[UIAppli ...

  10. MYISM表并发写请求过多 导致无法被读取解决方案

    MyISAM锁调度是如何实现的呢,这也是一个很关键的问题.例如,当一个进程请求某个MyISAM表的读锁,同时另一个进程也请求同一表的写锁,此时MySQL将会如优先处理进程呢?通过研究表明,写进程将先获 ...