Caves and Tunnels

Time limit: 3.0 second
Memory limit: 64 MB
After landing on Mars surface, scientists found a strange system of caves connected by tunnels. So they began to research it using remote controlled robots. It was found out that there exists exactly 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

The first line of the input contains one integer N (1 ≤ N ≤ 100000) — the number of caves. NextN − 1 lines describe tunnels. Each of these lines contains a pair of integers aibi(1 ≤ aibi ≤ N) 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 ≤ UV ≤ 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

For every 'G' query output one line containing the maximal radiation level by itself.

Sample

input output
4
1 2
2 3
2 4
6
I 1 1
G 1 1
G 3 4
I 2 3
G 1 1
G 3 4
1
0
1
3

分析:树上点修改+区间极值查询,树链剖分;

代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+,mod=1e9+,inf=0x3f3f3f3f;
int n,m,k,t,tot;
int bl[maxn],pos[maxn],dep[maxn],sz[maxn],fa[maxn];
int mx[maxn<<];
vector<int>e[maxn];
void dfs(int x,int y=)
{
sz[x]=;
for(int i=;i<e[x].size();i++)
{
int z=e[x][i];
if(z==y)continue;
fa[z]=x;dep[z]=dep[x]+;
dfs(z,x);
sz[x]+=sz[z];
}
}
void dfs1(int x,int chain)
{
bl[x]=chain;
pos[x]=++tot;
int big=;
for(int i=;i<e[x].size();i++)
{
int z=e[x][i];
if(dep[z]<dep[x])continue;
if(sz[z]>sz[big])big=z;
}
if(!big)return;
dfs1(big,chain);
for(int i=;i<e[x].size();i++)
{
int z=e[x][i];
if(dep[z]<dep[x]||z==big)continue;
dfs1(z,z);
}
}
void pup(int rt){mx[rt]=max(mx[rt<<],mx[rt<<|]);}
void upd(int x,int y,int l,int r,int rt)
{
if(x==l&&x==r){mx[rt]+=y;return;}
int mid=l+r>>;
if(x<=mid)upd(x,y,l,mid,rt<<);
else upd(x,y,mid+,r,rt<<|);
pup(rt);
}
int q(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)return mx[rt];
int mid=l+r>>;
int ret=;
if(L<=mid)ret=max(ret,q(L,R,l,mid,rt<<));
if(mid+<=R)ret=max(ret,q(L,R,mid+,r,rt<<|));
return ret;
}
int main()
{
int i,j;
//freopen("in.txt","r",stdin);
scanf("%d",&n);
for(i=;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
e[x].push_back(y);
e[y].push_back(x);
}
dfs();
dfs1(,);
scanf("%d",&m);
while(m--)
{
char op[];
int x,y;
scanf("%s%d%d",op,&x,&y);
if(op[]=='I')upd(pos[x],y,,n,);
else
{
int ret=;
while(bl[x]!=bl[y])
{
if(dep[bl[x]]<dep[bl[y]])swap(x,y);
ret=max(ret,q(pos[bl[x]],pos[x],,n,));
x=fa[bl[x]];
}
if(pos[x]>pos[y])swap(x,y);
ret=max(ret,q(pos[x],pos[y],,n,));
printf("%d\n",ret);
}
}
return ;
}

ural1553 Caves and Tunnels的更多相关文章

  1. URAL1553 Caves and Tunnels 树链剖分 动态树

    URAL1553 维护一棵树,随时修改某个节点的权值,询问(x,y)路径上权值最大的点. 树是静态的,不过套动态树也能过,时限卡的严就得上树链剖分了. 还是那句话 splay的核心是splay(x) ...

  2. URAL 题目1553. Caves and Tunnels(Link Cut Tree 改动点权,求两点之间最大)

    1553. Caves and Tunnels Time limit: 3.0 second Memory limit: 64 MB After landing on Mars surface, sc ...

  3. URAL 1553. Caves and Tunnels 树链拆分

    一颗树 每次出发点右键值是0 2操作模式1.第一i右键点值添加x 2.乞讨u至v在这条路上右上方值 树为主的连锁分裂称号 #include <cstdio> #include <cs ...

  4. Uva1553 Caves and Tunnels LCT

    简单题,主要为了练手. #include <cstdio> #include <iostream> #define maxn 100010 using namespace st ...

  5. LCT(link cut tree) 动态树

    模板参考:https://blog.csdn.net/saramanda/article/details/55253627 综合各位大大博客后整理的模板: #include<iostream&g ...

  6. Sorry, but the Android VPN API doesn’t currently allow TAP-based tunnels.

    Sorry, but the Android VPN API doesn’t currently allow TAP-based tunnels. Edit .ovpn configfile “dev ...

  7. hdu 4856 Tunnels (记忆化搜索)

    Tunnels Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  8. hdu 4856 Tunnels (bfs + 状压dp)

    题目链接 The input contains mutiple testcases. Please process till EOF.For each testcase, the first line ...

  9. CSU1612Destroy Tunnels(强连通)

    Destroy Tunnels 原来早忘记了离散里含有这么一个叫传递闭包的东西 矩阵A的闭包B = A U A^2 U A^3 U ... 所以这里直接如果A[i][j]!= 0,建边i->j跑 ...

随机推荐

  1. JavaScript性能优化技巧之函数节流

    函数节流技术的主要思路是,通过一个定时器,阻断连续重复的函数调用.对于我们自己内部使用的函数,这通常意义不大,也不推荐使用这个技术,它可能会丢失对某些数据的处理.但是对于在用户界面调用的函数,却非常有 ...

  2. 使用SQLCMD在SQLServer执行多个脚本 转载

    出处不明 概述: 作为DBA,经常要用开发人员提供的SQL脚本来更新正式数据库,但是一个比较合理的开发流程,当提交脚本给DBA执行的时候,可能已经有几百个sql文件,并且有执行顺序,如我现在工作的公司 ...

  3. Django 用户认证及OneToOneField

    Django 用户认证如果自己不想写 就可以用django自带的认证 首选导入模块 models.py #!/usr/bin/env python #_*_ coding:utf8 _*_ from ...

  4. Memcached 缓存服务器介绍

    1.memcached  高性能分布式内存对象缓存系统 2.目的:减轻数据库负载,提高基于动态数据库驱动网站的响应速度 3.数据格式:文本行 4.协议:memcache协议 5.存储方式:hashMa ...

  5. iOS \'The sandbox is not sync with the Podfile.lock\'问题解决

    iOS \'The sandbox is not sync with the Podfile.lock\'问题解决 HUANGDI 发表于 2015-02-27 09:51:13 问题描述: gith ...

  6. OpenGL---------BMP文件格式

    计算机保存图象的方法通常有两种:一是“矢量图”,一是“像素图”.矢量图保存了图象中每一几何物体的位置.形状.大小等信息,在显示图象时,根据这些信息计算得到完整的图象.“像素图”是将完整的图象纵横分为若 ...

  7. easyui实现权限管理

    在js中: function makeEasyTree(data){ if(!data) return []; var _newData = []; //最终返回结果 var _treeArray = ...

  8. bos项目经验心得(1)

    ---恢复内容开始--- 一.搭建数据库环境: 1.在cmd窗口登录mysql数据库: mysql -uroot -proot   (mysql登录数据库的形式就是 mysql-u用户名 -p密码) ...

  9. NDK Build 用法(NDK Build)

    1.ndk-build的用法 Android NDKr4引入了一个新的.小巧的shell脚本ndk-build,来简化源码编译. 该文件位于NDK根目录,进入你的工程根目录或子目录之后,在命令行下调用 ...

  10. javascript语句语义大全(6)

    var d = new Date();//创建当前日期对象var d = new Date('2016/03/22');//允许var d = new Date('2016/3/22');//允许va ...