codevs 3305 水果姐逛水果街Ⅱ&&codevs3006
水果姐第二天心情也很不错,又来逛水果街。
突然,cgh又出现了。cgh施展了魔法,水果街变成了树结构(店与店之间只有一条唯一的路径)。
同样还是n家水果店,编号为1~n,每家店能买水果也能卖水果,并且同一家店卖与买的价格一样。
cgh给出m个问题,每个问题要求水果姐从第x家店出发到第y家店,途中只能选一家店买一个水果,然后选一家店(可以是同一家店,但不能往回走)卖出去。求最多可以赚多少钱。
水果姐向学过oi的你求助。
第一行n,表示有n家店
下来n个正整数,表示每家店一个苹果的价格。
下来n-1行,每行两个整数x,y,表示第x家店和第y家店有一条边。
下来一个整数m,表示下来有m个询问。
下来有m行,每行两个整数x和y,表示从第x家店出发到第y家店。
有m行。
每行对应一个询问,一个整数,表示面对cgh的每次询问,水果姐最多可以赚到多少钱。
10
16 5 1 15 15 1 8 9 9 15
1 2
1 3
2 4
2 5
2 6
6 7
4 8
1 9
1 10
6
9 1
5 1
1 7
3 3
1 1
3 6
7
11
7
0
0
15
0<=苹果的价格<=10^8
0<n<=200000
0<m<=10000
—————————————————————
算是一道lct的裸题了吧
只有查询无修改 注意一下up就可以了
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL unsigned int
using namespace std;
const int M=;
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
int c[M][],fa[M],rev[M];
struct node{int v,mx,mn,lr,rl;}tr[M];
int n,m;
bool isrt(int x){return c[fa[x]][]!=x&&c[fa[x]][]!=x;}
void up(int x){
int l=c[x][],r=c[x][];
tr[x].mx=tr[x].mn=tr[x].v;
tr[x].lr=tr[x].rl=;
if(l){
tr[x].lr=max(tr[l].lr,tr[x].mx-tr[l].mn);
tr[x].rl=max(tr[l].rl,tr[l].mx-tr[x].mn);
tr[x].mx=max(tr[x].mx,tr[l].mx);
tr[x].mn=min(tr[x].mn,tr[l].mn);
}
if(r){
tr[x].lr=max(tr[x].lr,tr[r].lr);
tr[x].lr=max(tr[x].lr,tr[r].mx-tr[x].mn);
tr[x].rl=max(tr[x].rl,tr[r].rl);
tr[x].rl=max(tr[x].rl,tr[x].mx-tr[r].mn);
tr[x].mx=max(tr[x].mx,tr[r].mx);
tr[x].mn=min(tr[x].mn,tr[r].mn);
}
}
void modify(int x){
rev[x]^=;
swap(c[x][],c[x][]);
swap(tr[x].lr,tr[x].rl);
}
void down(int x){
if(rev[x]){
int l=c[x][],r=c[x][];
modify(l); modify(r);
rev[x]=;
}
}
void rotate(int x){
int y=fa[x],z=fa[y],l=,r=;
if(c[y][]==x) l=,r=;
if(!isrt(y)) c[z][c[z][]==y]=x;
fa[y]=x; fa[x]=z; fa[c[x][r]]=y;
c[y][l]=c[x][r]; c[x][r]=y;
up(y);
}
int st[M],top,S;
void splay(int x){
st[++top]=x; for(int i=x;!isrt(i);i=fa[i]) st[++top]=fa[i];
while(top) down(st[top--]);
while(!isrt(x)){
int y=fa[x],z=fa[y];
if(!isrt(y)){
if(c[z][]==y^c[y][]==x) rotate(x);
else rotate(y);
}
rotate(x);
}up(x);
}
void acs(int x0){
for(int x=x0,y=;x;splay(x),c[x][]=y,up(x),y=x,x=fa[x]);
splay(x0);
}
void mrt(int x){acs(x); modify(x);}
void link(int x,int y){mrt(x); fa[x]=y;}
void split(int x,int y){mrt(x); acs(y);}
int main()
{
int x,y;
n=read();
for(int i=;i<=n;i++) tr[i].v=read();
for(int i=;i<n;i++) x=read(),y=read(),link(x,y);
m=read();
for(int i=;i<=m;i++){
x=read(); y=read();
split(x,y);
printf("%d\n",tr[y].lr);
}
return ;
}
3006比3005就多了一个修改 代码也就多一行QAQ
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL unsigned int
using namespace std;
const int M=;
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
int c[M][],fa[M],rev[M];
struct node{int v,mx,mn,lr,rl;}tr[M];
int n,m;
bool isrt(int x){return c[fa[x]][]!=x&&c[fa[x]][]!=x;}
void up(int x){
int l=c[x][],r=c[x][];
tr[x].mx=tr[x].mn=tr[x].v;
tr[x].lr=tr[x].rl=;
if(l){
tr[x].lr=max(tr[l].lr,tr[x].mx-tr[l].mn);
tr[x].rl=max(tr[l].rl,tr[l].mx-tr[x].mn);
tr[x].mx=max(tr[x].mx,tr[l].mx);
tr[x].mn=min(tr[x].mn,tr[l].mn);
}
if(r){
tr[x].lr=max(tr[x].lr,tr[r].lr);
tr[x].lr=max(tr[x].lr,tr[r].mx-tr[x].mn);
tr[x].rl=max(tr[x].rl,tr[r].rl);
tr[x].rl=max(tr[x].rl,tr[x].mx-tr[r].mn);
tr[x].mx=max(tr[x].mx,tr[r].mx);
tr[x].mn=min(tr[x].mn,tr[r].mn);
}
}
void modify(int x){
rev[x]^=;
swap(c[x][],c[x][]);
swap(tr[x].lr,tr[x].rl);
}
void down(int x){
if(rev[x]){
int l=c[x][],r=c[x][];
modify(l); modify(r);
rev[x]=;
}
}
void rotate(int x){
int y=fa[x],z=fa[y],l=,r=;
if(c[y][]==x) l=,r=;
if(!isrt(y)) c[z][c[z][]==y]=x;
fa[y]=x; fa[x]=z; fa[c[x][r]]=y;
c[y][l]=c[x][r]; c[x][r]=y;
up(y);
}
int st[M],top,S;
void splay(int x){
st[++top]=x; for(int i=x;!isrt(i);i=fa[i]) st[++top]=fa[i];
while(top) down(st[top--]);
while(!isrt(x)){
int y=fa[x],z=fa[y];
if(!isrt(y)){
if(c[z][]==y^c[y][]==x) rotate(x);
else rotate(y);
}
rotate(x);
}up(x);
}
void acs(int x0){
for(int x=x0,y=;x;splay(x),c[x][]=y,up(x),y=x,x=fa[x]);
splay(x0);
}
void mrt(int x){acs(x); modify(x);}
void link(int x,int y){mrt(x); fa[x]=y;}
void split(int x,int y){mrt(x); acs(y);}
int main()
{
int k,x,y;
n=read();
for(int i=;i<=n;i++) tr[i].v=read();
for(int i=;i<n;i++) x=read(),y=read(),link(x,y);
m=read();
for(int i=;i<=m;i++){
k=read();
x=read(); y=read();
if(k) split(x,y),printf("%d\n",tr[y].lr);
else splay(x),tr[x].v=y,up(x);
}
return ;
}
codevs 3305 水果姐逛水果街Ⅱ&&codevs3006的更多相关文章
- 水果姐逛水果街Ⅱ codevs 3305
3305 水果姐逛水果街Ⅱ 时间限制: 2 s 空间限制: 256000 KB 题目描述 Description 水果姐第二天心情也很不错,又来逛水果街. 突然,cgh又出现了.cgh施展了魔 ...
- code vs 3305 水果姐逛水果街Ⅱ
3305 水果姐逛水果街Ⅱ 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 水果姐第二天心情也很不错, ...
- Codevs 3305 水果姐逛水果街Ⅱ 倍增LCA
题目:http://codevs.cn/problem/3305/ 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Des ...
- CodeVs——T 3305 水果姐逛水果街Ⅱ
http://codevs.cn/problem/3305/ 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 De ...
- Codevs 3304 水果姐逛水果街Ⅰ 线段树
题目: http://codevs.cn/problem/3304/ 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 D ...
- CodeVs——T 3304 水果姐逛水果街Ⅰ
http://codevs.cn/problem/3304/ 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Des ...
- 水果姐逛水果街Ⅰ(codevs 3304)
题目描述 Description 水果姐今天心情不错,来到了水果街. 水果街有n家水果店,呈直线结构,编号为1~n,每家店能买水果也能卖水果,并且同一家店卖与买的价格一样. 学过oi的水果姐迅速发现了 ...
- codevs3305 水果姐逛水果街Ⅱ
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...
- codevs3304 水果姐逛水果街
题目描述 Description 水果姐今天心情不错,来到了水果街. 水果街有n家水果店,呈直线结构,编号为1~n,每家店能买水果也能卖水果,并且同一家店卖与买的价格一样. 学过oi的水果姐迅速发现了 ...
随机推荐
- Linq工具篇(1)——使用LinqPad
学习Linq,有一个非常强大的工具,那就是LinqPad,具体功能有多强大就不说了,网上百度一下就可以知道,百闻不如一见,用用就知道,在网上下载一个绿色版的,无需安装,直接运行,界面如下: 具体功能, ...
- Linux初步——常用简单命令
散乱的记录,目前是边学边用,以后有机会再整理 curl命令 发起一个HTTP请求,如:curl "http://www.baidu.com" 加上-I选项查看HTTP协议头的信息, ...
- PyQt的QString和python的string的区别
转载于http://blog.chinaunix.net/uid-200142-id-4018863.html python的string和PyQt的QString的区别 python string和 ...
- nmon Analyser分析仪
nmon Analyser官网: https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/Power+System ...
- python基础篇 08 文件操作
本节主要内容:1. 初识⽂件操作2. 只读(r, rb)3. 只写(w, wb)4. 追加(a, ab)5. r+读写6. w+写读7. a+写读(追加写读)8. 其他操作⽅法9. ⽂件的修改以及另⼀ ...
- PostgreSQL基本配置
记一下Postgresql的基本操作,在Ubuntu下使用apt-get安装是不会像MySQL那样都配置好了,而是要安装后再配置: 1. 基本安装 # 安装postgresql和pgadmin(一个管 ...
- cmp快排 结构体快排
由于深陷于JAVA的面向对象思想,常常会用到结构体,记一下这个模板,方便直接调用进行结构体排序: struct point { int val,turn; }; bool cmp(struct poi ...
- node gyp的问题
解决 binding.gyp not found (xxx/xxx/xxx) while trying to load binding.gyp 问题 在使用ccap图形验证码模块时遇到这个问题 Err ...
- 学习materialize
<div class="container"> <div class="row"> </div> <div cla ...
- JavaSE复习(四)File类与IO流
File类 构造方法 public File(String pathname) :通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例. public File(String parent ...