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. Npm包的开发

    个人开发包的目录结构 ├── coverage //istanbul测试覆盖率生成的文件 ├── index.js //入口文件 ├── introduce.md //说明文件 ├── lib │   ...

  2. R--相关分布函数、统计函数的使用

    分布函数家族: *func()r : 随机分布函数d : 概率密度函数p : 累积分布函数q : 分位数函数 func()表示具体的名称如下表: 例子 #r : 随机分布函数 #d : 概率密度函数 ...

  3. Java基础之一组有用的类——为标记定义自己的模式(ScanString)

    控制台程序. Scanner类提供了一种方式,用来指定如何识别标记.这需要使用next()方法的两个重载版本.其中的一个版本接受Pattern类型的参数.另一个版本接受String类型的参数,用来指定 ...

  4. 断点续传 (HTTP) 归纳

    由于最近项目中要上传较大的文件,基于公司原有的底层框架的局限性,对于大文件的传输都束手无策,基于文件传输的安全性,考虑用断点续传(HTTP)以及FTP上传两种方式实现下面归纳下HTTP续传和FTP上传 ...

  5. php 遍历一个文件夹下的所有文件和子文件夹

    <?php function my_scandir($dir) { $files=array(); if(is_dir($dir)) { if($handle=opendir($dir)) { ...

  6. iOS 多人共享开发证书

    当多人开发时,如果已经申请了几个开发者证书和发布者证书,苹果就不允许再创建了,页面添加的地方被灰化了,所以不可能每个人都建一个开发证书,这时候需要共用一个证书了.(其实一般在我们的证书界面中应该只有一 ...

  7. api_response.go

    , "METHOD_NOT_ALLOWED"}         }, Log(l), V1)(w, req, nil)     }) }

  8. spring.factories

    在Spring Boot中有一种非常解耦的扩展机制:Spring Factories.这种扩展机制实际上是仿照Java中的SPI扩展机制来实现的. Java SPI机制SPI的全名为Service P ...

  9. hashlib 简单的登录例子

    hashlib例子: =============================================== import hashlib md5 = hashlib.md5() md5.up ...

  10. sqlserver批量删除字段 msrepl_tran_version

    屁话不多说. 原因: msrepl_tran_version由于有非空约束.所以不能直接删除. --###############################################--1 ...