bzoj 3999: [TJOI2015]旅游 LCT
没啥难的,inf 的值设小了调了半天~
code:
#include <bits/stdc++.h>
#define N 50003
#define lson t[x].ch[0]
#define rson t[x].ch[1]
using namespace std;
namespace IO
{
void setIO(string s)
{
string in=s+".in";
string out=s+".out";
freopen(in.c_str(),"r",stdin);
// freopen(out.c_str(),"w",stdout);
}
};
const int inf=0x3f3f3f3f;
int edges;
int sta[N],hd[N],to[N<<1],nex[N<<1];
struct node
{
int ch[2],minv,maxv,lmax,rmax,rev,tag,val,f;
}t[N];
void add(int u,int v)
{
nex[++edges]=hd[u],hd[u]=edges,to[edges]=v;
}
int get(int x)
{
return t[t[x].f].ch[1]==x;
}
int isrt(int x)
{
return !(t[t[x].f].ch[0]==x||t[t[x].f].ch[1]==x);
}
void pushup(int x)
{
t[x].minv=min(min(t[lson].minv,t[rson].minv), t[x].val);
t[x].maxv=max(max(t[lson].maxv,t[rson].maxv), t[x].val);
t[x].lmax=max(max(t[lson].lmax,t[rson].lmax),max(t[lson].maxv,t[x].val)-min(t[x].val,t[rson].minv));
t[x].rmax=max(max(t[lson].rmax,t[rson].rmax),max(t[rson].maxv,t[x].val)-min(t[x].val,t[lson].minv));
}
void marktag(int x,int v)
{
if(!x) return;
t[x].tag+=v,t[x].minv+=v,t[x].maxv+=v,t[x].val+=v;
}
void markrev(int x)
{
if(!x) return;
swap(t[x].lmax,t[x].rmax),swap(lson,rson),t[x].rev^=1;
}
void pushdown(int x)
{
if(t[x].tag)
{
marktag(lson,t[x].tag),marktag(rson,t[x].tag);
t[x].tag=0;
}
if(t[x].rev)
{
markrev(lson),markrev(rson);
t[x].rev=0;
}
}
void rotate(int x)
{
int old=t[x].f,fold=t[old].f,which=get(x);
if(!isrt(old)) t[fold].ch[t[fold].ch[1]==old]=x;
t[old].ch[which]=t[x].ch[which^1],t[t[old].ch[which]].f=old;
t[x].ch[which^1]=old,t[old].f=x,t[x].f=fold;
pushup(old),pushup(x);
}
void splay(int x)
{
int v=0,u=x,fa;
for(sta[++v]=u;!isrt(u);u=t[u].f) sta[++v]=t[u].f;
for(;v;--v) pushdown(sta[v]);
for(u=t[u].f;(fa=t[x].f)!=u;rotate(x))
if(t[fa].f!=u)
rotate(get(fa)==get(x)?fa:x);
}
void Access(int x)
{
for(int y=0;x;y=x,x=t[x].f)
splay(x),rson=y,pushup(x);
}
void makeroot(int x)
{
Access(x),splay(x),markrev(x);
}
void split(int x,int y)
{
makeroot(x),Access(y),splay(y);
}
void dfs(int u,int ff)
{
t[u].f=ff;
for(int i=hd[u];i;i=nex[i])
if(to[i]!=ff) dfs(to[i],u);
pushup(u);
}
int main()
{
// IO::setIO("input");
int i,j,n,q;
t[0].minv=inf,t[0].maxv=-inf;
scanf("%d",&n);
for(i=1;i<=n;++i) scanf("%d",&t[i].val);
for(i=1;i<n;++i)
{
int x,y;
scanf("%d%d",&x,&y),add(x,y),add(y,x);
}
dfs(1,0);
scanf("%d",&q);
for(i=1;i<=q;++i)
{
int a,b,v;
scanf("%d%d%d",&a,&b,&v);
split(a,b);
printf("%d\n",t[b].rmax);
marktag(b,v);
}
return 0;
}
bzoj 3999: [TJOI2015]旅游 LCT的更多相关文章
- bzoj 3999: [TJOI2015]旅游
Description 为了提高智商,ZJY准备去往一个新世界去旅游.这个世界的城市布局像一棵树.每两座城市之间只有一条路径可 以互达.每座城市都有一种宝石,有一定的价格.ZJY为了赚取最高利益,她会 ...
- 【BZOJ3999】[TJOI2015]旅游(Link-Cut Tree)
[BZOJ3999][TJOI2015]旅游(Link-Cut Tree) 题面 BZOJ 洛谷 题解 一道不难的\(LCT\)题(用树链剖分不是为难自己吗,这种有方向的东西用\(LCT\)不是方便那 ...
- [BZOJ - 2631] tree 【LCT】
题目链接:BZOJ - 2631 题目分析 LCT,像线段树区间乘,区间加那样打标记. 这道题我调了一下午. 提交之后TLE了,我一直以为是写错了导致了死循环. 于是一直在排查错误.直到.. 直到我看 ...
- bzoj 2157: 旅游 (LCT 边权)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2157 题面; 2157: 旅游 Time Limit: 10 Sec Memory Lim ...
- BZOJ 3999 旅游
.......好长啊. #include<iostream> #include<cstdio> #include<cstring> #include<algo ...
- 【BZOJ2157】旅游 LCT
模板T,SB的DMoon..其实样例也是中国好样例...一开始不会复制,yangyang:找到“sample input”按住shift,按page down.... #include <ios ...
- [BZOJ 3282] Tree 【LCT】
题目链接:BZOJ - 3282 题目分析 这道题是裸的LCT,包含 Link , Cut 和询问两点之间的路径信息. 写代码时出现的错误:Access(x) 的循环中应该切断的是原来的 Son[x] ...
- BZOJ.2816.[ZJOI2012]网络(LCT)
题目链接 BZOJ 洛谷 对每种颜色维护一个LCT,保存点之间的连接关系. 修改权值A[x]和所有Max[x]都要改: 修改边的颜色先枚举所有颜色,看是否在某种颜色中有边,然后断开.(枚举一遍就行啊 ...
- bzoj 3779: 重组病毒 LCT+线段树+倍增
题目: 黑客们通过对已有的病毒反编译,将许多不同的病毒重组,并重新编译出了新型的重组病毒.这种病毒的繁殖和变异能力极强.为了阻止这种病毒传播,某安全机构策划了一次实验,来研究这种病毒. 实验在一个封闭 ...
随机推荐
- 作业练习P194,jieba应用,读取,分词,存储,生成词云,排序,保存
import jieba #第一题 txt='Python是最有意思的编程语言' words=jieba.lcut(txt) #精确分词 words_all=jieba.lcut(txt,cut_al ...
- 安装python的pip库setup.py出现报错的解决过程
错误起因: 第一次安python3.72的时候,直接去官网下了压缩包,解压后也没有exe文件.环境也是手动配置,在之后安装Pycharm的时候,系统找不到解释器,手动加上. 错误经过: 等写程序用到i ...
- SQL——UPDATE(改)
一.UPDATE语句基本用法 UPADTE语句用于修改表中已存在的记录. UPDATE语句语法: UPDATE 表名 SET 列名1 = 值1,列名2 = 值2... WHERE 条件语句; 演示st ...
- Scratch编程:快乐的小马(三)
“ 上节课的内容全部掌握了吗?反复练习了没有,编程最好的学习方法就是练习.练习.再练习.一定要记得多动手.多动脑筋哦~~” 01 — 游戏介绍 这是一款简单的小游戏,实现了一匹小马跑来跑去(小马有跑动 ...
- oracle笔记之计算年龄、工龄和TRUNC
方法一:利用months_between 函数计算 SELECT TRUNC(months_between(sysdate, birthday)/12) AS agefrom dual; 方法二:日期 ...
- Docker 方式部署的应用的版本更新
前言 公司使用 Docker-Compose 的方式部署 Jenkins/Gitlab/Sonar/Confluence/Apollo/Harbor/ELK/MySQL 等一系列开发工具/数据库. 而 ...
- Gitlab服务不能启动postgresql
源博文:http://www.zxmseed.com/blog/911081 1.查看启动的服务 -sh-4.1$ gitlab-ctl status warning: gitlab-workhors ...
- python 循环结构(for-in)
循环结构(for-in) 说明:也是循环结构的一种,经常用于遍历字符串.列表,元组,字典等 格式: for x in y: 循环体 执行流程:x依次表示y中的一个元素,遍历完所有元素循环结束 示例1: ...
- Python 使用gevent下载图片案例
import urllib.request import gevent from gevent import monkey monkey.patch_all() def downloader(img_ ...
- Fortify漏洞之Access Control: Database(数据越权)
继续对Fortify的漏洞进行总结,本篇主要针对 Access Control: Database(数据越权)的漏洞进行总结,如下: 1.Access Control: Database(数据越权) ...