zoukankan      html  css  js  c++  java
  • 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 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
    
    Problem Source: Novosibirsk SU Contest. Petrozavodsk training camp, September 2007

    Tags: data structures  (

    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
    ",lct.query(u,v));
    		}
    	}
    }


  • 相关阅读:
    CF1592F2 Alice and Recoloring 2
    CF1601E Phys Ed Online
    AGC050B Three Coins
    [学习笔记]珂朵莉树(Old Drive Tree)
    CF30E. Tricky and Clever Password
    [学习笔记]替罪羊树
    开源项目MiniOA队员招募通知
    MiniOA开发过程记录(33)自动登录模式
    MiniOA开发过程记录(29)安装Maven
    简易工作流设计思考(欢迎补充和批评)
  • 原文地址:https://www.cnblogs.com/mthoutai/p/6892926.html
Copyright © 2011-2022 走看看