HDOJ 题目3966 Aragorn's Story(Link Cut Tree成段加减点权,查询点权)

Aragorn‘s Story

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 5505    Accepted Submission(s): 1441

Problem Description

Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who want to invade his kingdom. As Aragorn knows, the enemy has N camps out of his kingdom and M edges connect
them. It is guaranteed that for any two camps, there is one and only one path connect them. At first Aragorn know the number of enemies in every camp. But the enemy is cunning , they will increase or decrease the number of soldiers in camps. Every time the
enemy change the number of soldiers, they will set two camps C1 and C2. Then, for C1, C2 and all camps on the path from C1 to C2, they will increase or decrease K soldiers to these camps. Now Aragorn wants to know the number of soldiers in some particular
camps real-time.

Input

Multiple test cases, process to the end of input.

For each case, The first line contains three integers N, M, P which means there will be N(1 ≤ N ≤ 50000) camps, M(M = N-1) edges and P(1 ≤ P ≤ 100000) operations. The number of camps starts from 1.

The next line contains N integers A1, A2, ...AN(0 ≤ Ai ≤ 1000), means at first in camp-i has Ai enemies.

The next M lines contains two integers u and v for each, denotes that there is an edge connects camp-u and camp-v.

The next P lines will start with a capital letter ‘I‘, ‘D‘ or ‘Q‘ for each line.

‘I‘, followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, increase K soldiers to these camps.

‘D‘, followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, decrease K soldiers to these camps.

‘Q‘, followed by one integer C, which is a query and means Aragorn wants to know the number of enemies in camp C at that time.

Output

For each query, you need to output the actually number of enemies in the specified camp.

Sample Input

3 2 5
1 2 3
2 1
2 3
I 1 3 5
Q 2
D 1 2 2
Q 1
Q 3

Sample Output

7
4
8

Hint

1.The number of enemies may be negative.

2.Huge input, be careful.


 

Source

2011 Multi-University Training Contest 13 - Host by HIT

Recommend

We have carefully selected several similar problems for you:  3965 3962 3963 3967 3968

瞬秒一A~~

题目大意:一个图,每个点都有点权,3种操作,I a b,a到b的点点权加一个值,D是减一个值,Q是查询这个点的点权

网上大都是树链剖分写的,提交了俩发现速度并不比我的快,,真的不知道前边300多毫秒的咋写的

#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[50050];
struct LCT
{
    int bef[50050],pre[50050],next[50050][2],key[50050],add[50050];
    void init()
    {
        memset(pre,0,sizeof(pre));
        memset(next,0,sizeof(next));
        memset(key,0,sizeof(key));
        memset(add,0,sizeof(add));
    }
    void pushdown(int x)
    {
        if(add[x])
        {
            int a,b;
            a=next[x][0];
            b=next[x][1];
            if(a)//不用加,实际上,同下边那个b<span id="transmark"></span>
            {
                add[a]+=add[x];
                key[a]+=add[x];
            }
            if(b)
            {
                add[b]+=add[x];
                key[b]+=add[x];
            }
            add[x]=0;
        }
    }
    void rotate(int x,int kind)
    {
        int y,z;
        y=pre[x];
        z=pre[y];
        pushdown(y);
        pushdown(x);
        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;
    }
    void splay(int x)
    {
        int rt;
        for(rt=x;pre[rt];rt=pre[rt]);
        if(x!=rt)
        {
            bef[x]=bef[rt];
            bef[rt]=0;
            pushdown(x);
            while(pre[x])
            {
                if(next[pre[x]][0]==x)
                {
                    rotate(x,1);
                }
                else
                    rotate(x,0);
            }
        }
    }
    void access(int x)
    {
        int fa;
        for(fa=0;x;x=bef[x])
        {
            splay(x);
            pushdown(x);
            pre[next[x][1]]=0;
            bef[next[x][1]]=x;
            next[x][1]=fa;
            pre[fa]=x;
            bef[fa]=0;
            fa=x;
        }
    }
    void change(int x,int y,int val)
    {
       access(y);
        for(y=0;x;x=bef[x])
        {
            splay(x);
            if(!bef[x])
            {
                 key[x]+=val;
                 key[y]+=val;
                 key[next[x][1]]+=val;
                 add[next[x][1]]+=val;
                 add[y]+=val;
                 return;
            }
            pushdown(x);
            pre[next[x][1]]=0;
            bef[next[x][1]]=x;
            next[x][1]=y;
            pre[y]=x;
            bef[y]=0;
            y=x;
        }
    }
    int query(int x)
    {
       splay(x);
       return key[x];
    }
}lct;
struct s
{
    int u,v,next;
}edge[100020<<1];
int head[100020],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,m,q;
    while(scanf("%d%d%d",&n,&m,&q)!=EOF)
    {
        int i;
        lct.init();
        cnt=0;
        memset(head,-1,sizeof(head));
        for(i=1;i<=n;i++)
        {
            scanf("%d",&lct.key[i]);
        }
        for(i=0;i<m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
        }
        bfs(1);
        while(q--)
        {
            char op[2];
            scanf("%s",op);
            if(op[0]=='I')
            {
                int x,y,val;
                scanf("%d%d%d",&x,&y,&val);
                lct.change(x,y,val);
            }
            else
                if(op[0]=='D')
                {
                    int x,y,val;
                    scanf("%d%d%d",&x,&y,&val);
                    lct.change(x,y,-val);
                }
                else
                    if(op[0]=='Q')
                    {
                        int x;
                        scanf("%d",&x);
                        printf("%d\n",lct.query(x));
                    }
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

HDOJ 题目3966 Aragorn's Story(Link Cut Tree成段加减点权,查询点权)

时间: 2024-10-25 04:48:09

HDOJ 题目3966 Aragorn's Story(Link Cut Tree成段加减点权,查询点权)的相关文章

HDOJ 题目2763 Housewife Wind(Link Cut Tree修改边权,查询两点间距离)

Housewife Wind Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 7639   Accepted: 1990 Description After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautif

脑洞大开加偏执人格——可持久化treap版的Link Cut Tree

一直没有点动态树这个科技树,因为听说只能用Splay,用Treap的话多一个log.有一天脑洞大开,想到也许Treap也能从底向上Split.仔细思考了一下,发现翻转标记不好写,再仔细思考了一下,发现还是可以写的,只需要实时交换答案二元组里的两棵树,最后在吧提出来的访问节点放回去就行了.本着只学一种平衡树的想法,脑洞大开加偏执人格的开始写可持久化Treap版的Link Cut Tree... 写了才发现,常数硕大啊!!!代码超长啊!!!因为merge是从上到下,split从下到上,pushdow

LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板

P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的. 1:后接两个整数(x,y),代表连接x到y,若x到y已经联通则无需连接. 2:后接两个整数(x,y),代表删除边(x,y),不保证边(x,y)存在. 3:后接两个整数(x,y),代表将点x上的权值变成y. 输入输出

Link Cut Tree学习笔记

从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子树信息 小结 动态树问题和Link Cut Tree 动态树问题是一类要求维护一个有根树森林,支持对树的分割, 合并等操作的问题. Link Cut Tree(林可砍树?简称LCT)是解决这一类问题的一种数据结构. 一些无聊的定义 Link Cut Tree维护的是动态森林中每棵树的任意链剖分. P

Codeforces Round #339 (Div. 2) A. Link/Cut Tree

A. Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the expose procedure. Unfortunately, Rostislav is unable to understand the definition

AC日记——【模板】Link Cut Tree 洛谷 P3690

[模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 300005 int n,m,val[maxn]; int top,ch[maxn][2],f[maxn],xr[maxn],q[maxn],rev[maxn]; inline void in(int &now) { int if_z=1;now=0; char Cget=getchar(); while

bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门

link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isroot(int x):判断x是否为所在重链(splay)的根 void down(int x):下放各种标记 void rotate(int x):在x所在重链(splay)中将x旋转到fa[x]的位置上 void splay(int x):在x坐在重链(splay)中将x旋转到根 void acce

link cut tree 入门

鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. #include<cstdio> #include<cstring> #include<cctype> #include<algorithm> #include<queue> using namespace std; #define rep(i,s,

P3690 【模板】Link Cut Tree (动态树)

P3690 [模板]Link Cut Tree (动态树) https://www.luogu.org/problemnew/show/P3690 分析: LCT模板 代码: 注意一下cut! 1 #include<cstdio> 2 #include<algorithm> 3 4 using namespace std; 5 6 const int N = 300100; 7 8 int val[N],fa[N],ch[N][2],rev[N],sum[N],st[N],top;