URAL 题目1553. Caves and Tunnels(Link Cut Tree 改动点权,求两点之间最大)
1553. Caves and Tunnels
Memory limit: 64 MB
one route between every pair of caves. But then scientists faced a particular problem. Sometimes in the caves faint explosions happen. They cause emission of radioactive isotopes and increase radiation level in the cave. Unfortunately robots don't stand radiation
well. But for the research purposes they must travel from one cave to another. So scientists placed sensors in every cave to monitor radiation level in the caves. And now every time they move robots they want to know the maximal radiation level the robot will
have to face during its relocation. So they asked you to write a program that will solve their problem.
Input
specifying the numbers of the caves connected by corresponding tunnel. The next line has an integer Q (Q ≤ 100000) representing the number of queries. The Q queries follow on a single line each. Every query has a form of "C U V",
where C is a single character and can be either 'I' or 'G' representing the type of the query (quotes for clarity only). In the case of an 'I' query radiation level in U-th cave (1 ≤ U ≤ N) is incremented by V (0 ≤ V ≤ 10000).
In the case of a 'G' query your program must output the maximal level of radiation on the way between caves with numbers U and V (1 ≤ U, V ≤ N) after all increases of radiation ('I' queries) specified before current
query. It is assumed that initially radiation level is 0 in all caves, and it never decreases with time (because isotopes' half-life time is much larger than the time of observations).
Output
Sample
input | output |
---|---|
4 |
1 |
Tags: data structures (
space=1&num=1553" style="color:gray">hide tags for unsolved problems
)
瞬秒~~
题目大意:一棵树,開始每一个点权的权值为0,后边q次操作。I a b,a点点权加b。G a b查询从a到b要走的最大的权值
ac代码
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#define INF 0x7fffffff
#define max(a,b) (a>b?a:b)
using namespace std;
int vis[100050];
struct LCT
{
int bef[100050],pre[100050],next[100050][2],key[100050],val[100050];
void init()
{
memset(pre,0,sizeof(pre));
memset(next,0,sizeof(next));
memset(key,0,sizeof(key));
val[0]=-INF;
}
void pushup(int x)
{
val[x]=max(key[x],max(val[next[x][1]],val[next[x][0]]));
}
void rotate(int x,int kind)
{
int y,z;
y=pre[x];
z=pre[y];
next[y][!kind]=next[x][kind];
pre[next[x][kind]]=y;
next[z][next[z][1]==y]=x;
pre[x]=z;
next[x][kind]=y;
pre[y]=x;
pushup(y);
}
void splay(int x)
{
int rt;
for(rt=x;pre[rt];rt=pre[rt]);
if(x!=rt)
{
bef[x]=bef[rt];
bef[rt]=0;
while(pre[x])
{
if(next[pre[x]][0]==x)
{
rotate(x,1);
}
else
rotate(x,0);
}
pushup(x);
}
}
void access(int x)
{
int fa;
for(fa=0;x;x=bef[x])
{
splay(x);
pre[next[x][1]]=0;
bef[next[x][1]]=x;
next[x][1]=fa;
pre[fa]=x;
bef[fa]=0;
fa=x;
pushup(x);
}
}
void change(int x,int y)
{
key[x]+=y;
splay(x);
}
int query(int x,int y)
{
access(y);
for(y=0;x;x=bef[x])
{
splay(x);
if(!bef[x])
{
return max(key[x],max(val[next[x][1]],val[y]));
}
pre[next[x][1]]=0;
bef[next[x][1]]=x;
next[x][1]=y;
pre[y]=x;
bef[y]=0;
y=x;
pushup(x);
}
return 0;
}
}lct;
struct s
{
int u,v,next;
}edge[200020<<1];
int head[200020],cnt;
void add(int u,int v)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void bfs(int u)
{
queue<int>q;
memset(vis,0,sizeof(vis));
vis[u]=1;
q.push(u);
while(!q.empty())
{
u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!vis[v])
{
lct.bef[v]=u;
vis[v]=1;
q.push(v);
}
}
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int i=1;
memset(head,-1,sizeof(head));
cnt=0;
for(i=1;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
int q;
scanf("%d",&q);
lct.init();
bfs(1);
while(q--)
{
char str[2];
int u,v;
scanf("%s%d%d",str,&u,&v);
if(str[0]=='I')
lct.change(u,v);
else
printf("%d\n",lct.query(u,v));
}
}
}
URAL 题目1553. Caves and Tunnels(Link Cut Tree 改动点权,求两点之间最大)的更多相关文章
- FOJ题目Problem 2082 过路费 (link cut tree边权更新)
Problem 2082 过路费 Accept: 382 Submit: 1279 Time Limit: 1000 mSec Memory Limit : 32768 KB Proble ...
- Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题
A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- Link Cut Tree学习笔记
从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...
- [CodeForces - 614A] A - Link/Cut Tree
A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, ...
- Link Cut Tree 总结
Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...
- 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)
题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...
- 洛谷P3690 [模板] Link Cut Tree [LCT]
题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...
- 脑洞大开加偏执人格——可持久化treap版的Link Cut Tree
一直没有点动态树这个科技树,因为听说只能用Splay,用Treap的话多一个log.有一天脑洞大开,想到也许Treap也能从底向上Split.仔细思考了一下,发现翻转标记不好写,再仔细思考了一下,发现 ...
随机推荐
- (转)hibernate-5.0.7+struts-2.3.24+spring-4.2.4三大框架整合
http://blog.csdn.net/yerenyuan_pku/article/details/70040220 SSH框架整合思想 三大框架应用在JavaEE三层结构,每一层都用到了不同的框架 ...
- 获取tomcat服务器上的部分日志
Linux下tomcat的日志很大,有的几G大,要用什么工具查看或把日志文件拆解? 一般习惯用 tail 的方式在服务器查看.如果要取下 可以用 tail -2000 xxxx.log > te ...
- chsh - 改变登录 shell
总览 (SYNOPSIS) chsh [ -s shell ] [ -l ] [ -u ] [ -v ] [ username ] 描述 (DESCRIPTION) chsh 用于 改变 用户的 登录 ...
- 20181019 记录 window.setTimeout('dofunction()',2000); - layui form 表单提交 事件 - F11全屏 事件 window.onresize
1 延时事件 window.setTimeout('dofunction()',2000); 函数外面要有引号 如果没有引号 就不能延时执行 应该是内容进行eval,所以外层不是传递字符串的话,外层函 ...
- CAD交互绘制样条线(com接口)
在CAD设计时,需要绘制样条线,用户可以设置样条线线重及颜色等属性. 主要用到函数说明: _DMxDrawX::SendStringToExecuteFun 把命令当着函数执行,可以传参数.详细说明如 ...
- 06CSS列表
CSS列表 列表样式——list-style-type list-style-type:<属性值> disc 黑圆点 circle 空心圆点 square 小黑方块 decimal ...
- Android ListView setEmptyView
http://my.eoe.cn/yaming/archive/879.html 1 当我们使用ListView或GridView的时候,当列表为空的时候,我们需要一个特殊的View来提示用户操作,于 ...
- 中南大学2019年ACM寒假集训前期训练题集(基础题)
先写一部分,持续到更新完. A: 寒衣调 Description 男从戎,女守家.一夜,狼烟四起,男战死沙场.从此一道黄泉,两地离别.最后,女终于在等待中老去逝去.逝去的最后是换尽一生等到的相逢和团圆 ...
- Spider-scrapy日志处理
Scrapy生成的调试信息非常有用,但是通常太啰嗦,你可以在Scrapy项目中的setting.py中设置日志显示等级: LOG_LEVEL = 'ERROR' 日志级别 Scrapy日志有五种等级, ...
- Qt笔记——连接第三方库&用libZPlay库获取音频文件的艺术家、专辑等信息
连接第三方库libZPlay 概述 需要.a/.lib ,.h , .dll 三个文件 官网下载 http://libzplay.sourceforge.net/ import .h 链接 .a 放入 ...