Caves and Tunnels

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 <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e5+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}return f;}
int n,m,k,t,q,tot,fa[maxn],dep[maxn],son[maxn],h[maxn],bl[maxn],pos[maxn];
struct node
{
    int to,nxt;
}a[maxn<<1];
struct node1
{
    char a[2];
    int x,y;
}op[maxn];
void add(int x,int y)
{
    tot++;
    a[tot].to=y;
    a[tot].nxt=h[x];
    h[x]=tot;
}
void dfs(int now,int pre)
{
    son[now]=1;
    for(int i=h[now];i;i=a[i].nxt)
    {
        if(a[i].to!=pre)
        {
            fa[a[i].to]=now;
            dep[a[i].to]=dep[now]+1;
            dfs(a[i].to,now);
            son[now]+=son[a[i].to];
        }
    }
}
void dfs1(int now,int chain)
{
    int k=0;
    pos[now]=++t;
    bl[now]=chain;
    for(int i=h[now];i;i=a[i].nxt)
    {
        int x=a[i].to;
        if(dep[x]>dep[now]&&son[x]>son[k])
            k=x;
    }
    if(k==0)return;
    dfs1(k,chain);
    for(int i=h[now];i;i=a[i].nxt)
    {
        int x=a[i].to;
        if(dep[x]>dep[now]&&k!=x)
            dfs1(x,x);
    }
}
struct Node
{
    ll Max, lazy;
} T[maxn<<2];

void PushUp(int rt)
{
    T[rt].Max = max(T[rt<<1].Max, T[rt<<1|1].Max);
}

void PushDown(int L, int R, int rt)
{
    int mid = (L + R) >> 1;
    ll t = T[rt].lazy;
    T[rt<<1].Max += t;
    T[rt<<1|1].Max += t;
    T[rt<<1].lazy += t;
    T[rt<<1|1].lazy += t;
    T[rt].lazy = 0;
}

void Update(int l, int r, int v, int L, int R, int rt)
{
    if(l==L && r==R)
    {
        T[rt].lazy += v;
        T[rt].Max += v;
        return ;
    }
    int mid = (L + R) >> 1;
    if(T[rt].lazy) PushDown(L, R, rt);
    if(r <= mid) Update(l, r, v, Lson);
    else if(l > mid) Update(l, r, v, Rson);
    else
    {
        Update(l, mid, v, Lson);
        Update(mid+1, r, v, Rson);
    }
    PushUp(rt);
}
ll Query(int l, int r, int L, int R, int rt)
{
    if(l==L && r== R)
    {
        return T[rt].Max;
    }
    int mid = (L + R) >> 1;
    if(T[rt].lazy) PushDown(L, R, rt);
    if(r <= mid) return Query(l, r, Lson);
    else if(l > mid) return Query(l, r, Rson);
    return max(Query(l, mid, Lson) , Query(mid + 1, r, Rson));
}
ll gao(int x,int y)
{
    ll ma=-1e19;
    while(bl[x]!=bl[y])
    {
        if(dep[bl[x]]<dep[bl[y]])swap(x,y);
        ma=max(ma,Query(pos[bl[x]],pos[x],1,n,1));
        x=fa[bl[x]];
    }
    if(pos[x]>pos[y])swap(x,y);
    ma=max(ma,Query(pos[x],pos[y],1,n,1));
    return ma;
}
int main()
{
    int i,j;
    scanf("%d",&n);
    rep(i,1,n-1)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y);
        add(y,x);
    }
    dfs(1,-1);
    dfs1(1,1);
    scanf("%d",&q);
    rep(i,1,q)
    {
        scanf("%s%d%d",op[i].a,&op[i].x,&op[i].y);
        if(op[i].a[0]==‘I‘)Update(pos[op[i].x],pos[op[i].x],op[i].y,1,n,1);
        else printf("%lld\n",gao(op[i].x,op[i].y));
    }
    //system("Pause");
    return 0;
}
时间: 2024-10-21 16:24:10

Caves and Tunnels的相关文章

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, 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 ther

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, 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 ther

Uva1553 Caves and Tunnels LCT

简单题,主要为了练手. 1 #include <cstdio> 2 #include <iostream> 3 #define maxn 100010 4 using namespace std; 5 6 namespace L { 7 int pnt[maxn], pre[maxn], son[maxn][2], rtg[maxn], val[maxn], mxv[maxn]; 8 9 inline void update( int nd ) { 10 mxv[nd] = max

URAL 1553. Caves and Tunnels 树链拆分

一颗树 每次出发点右键值是0 2操作模式1.第一i右键点值添加x 2.乞讨u至v在这条路上右上方值 树为主的连锁分裂称号 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 100010; struct edge { int v, next; }e[maxn*2]; int first[maxn], cnt; void AddEdg

URAL 1553. Caves and Tunnels 树链剖分

一棵树 开始每个点的权值都为0 2种操作1.将第i个点的权值增加x 2.求u到v这条路上最大的权值 树链剖分基础题 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 100010; struct edge { int v, next; }e[maxn*2]; int first[maxn], cnt; void AddEdge(i

Destroy Tunnels(矩阵水题)

Destroy Tunnels Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 39  Solved: 22 [Submit][Status][Web Board] Description Zuosige always has bad luck. Recently, he is in hospital because of pneumonia. While he is taking his injection, he feels extremely

Configure Open vSwitch Tunnels

转自: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux_OpenStack_Platform/4/html/Installation_and_Configuration_Guide/Configuring_Open_vSwitch_tunnels.html 13.4.4. Configure Open vSwitch Tunnels Tunneling with Open vSwitch allows

HDU 4856 Tunnels(bfs+状压dp)

题目大意:给你一个N*N的图让你到达所有的"."点,"#"不能通过,有m组每组有一个入口,一个出口,入口可以传送到出口,不知道经过m组的先后顺序,让你求出走过所有的"."的最小时间. 思路:先bfs出来所有的m之间的最短距离,然后dp[j][i] 表示,在j状态下开始第i步的最小路程,枚举找到一个最小的dp[1<<m - 1][i],就是最短距离,否则输出"-1". Tunnels Time Limit: 3000

HDU 4856 Tunnels

Tunnels Time Limit: 1500ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 485664-bit integer IO format: %I64d      Java class name: Main Bob is travelling in Xi’an. He finds many secret tunnels beneath the city. In his eyes, t