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. FUSE and File System

    FUSE: File system in USErspace. So what is a file system? A file system maps file paths to file cont ...

  2. vim编程配置方法

    vim简介Vim 有以下几个模式:1) 正常(normal)模式,缺省的编辑模式:下面如果不加特殊说明,提到的命令都直接在正常模式下输入:任何其它模式中都可以通过键盘上的 Esc 键回到正常模式.2) ...

  3. AJAX程序实验

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 < ...

  4. c++ map unordered_map

    map operator<的重载一定要定义成const.因为map内部实现时调用operator<的函数好像是const. #include<string> #include& ...

  5. undefined与null

    undefined 声明的变量尚未初始化 null 对象尚未存在 eg: var a; console.log(typeof a); 输出undefined var b= document.getEl ...

  6. JPA 系列教程9-双向一对一唯一外键

    双向一对一唯一外键的ddl语句 CREATE TABLE `t_person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(25 ...

  7. OVS - commands

    journalctl -t ovs-vswitchd ovs-vsctl show ovs-ofctl show br0 set vlanid ovs-vsctl set port eth0 tag= ...

  8. java数据结构之有序表查找

    这篇文章是关于有序表的查找,主要包括了顺序查找的优化用法.折半查找.插值查找.斐波那契查找: 顺序优化查找:效率极为底下,但是算法简单,适用于小型数据查找: 折半查找:又称为二分查找,它是从查找表的中 ...

  9. C++文件编程(文件流操作)

    给出了比较常见的文件操作,包括二进制文件操作.代码如下: #include<iostream> #include<cstdio> #include<cstring> ...

  10. 使用Github搭建个人博客网站

    1 新建一个repo,创建一个没有父节点的分支gh-pages(github规定,只有该分支中的页面,才会生成网页文件): mkdir jekyll_demo cd jekyll_demo git i ...