Hdu 3966 . Aragorn's Story

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

Solution

  • 这题是一道典型的树链剖分题,只需区间修改和单点询问。

  • 有关树链剖分,可以参考这里:

  • 由于只是一道英文题,有几点需要注意:

    1. 本题有多组数据

    2. I 询问是区间增加一个值,D 询问是区间减少一个值, Q <script type="math/tex" id="MathJax-Element-75">Q</script> 询问是询问一个点的值。

Code

#include<cstdio>
#include<cstring>
#include<algorithm>
#define clr(a) memset(a,0,sizeof(a))
using namespace std;
const int N=50002;
struct data
{
    int l,r,c,n;
}f[N<<2];
int n,m,q,tot,num;
int first[N],nex[N<<1],en[N<<1];
int dep[N],fa[N],size[N];
int son[N],pre[N],tree[N],top[N],a[N];
char s[2];
inline int read()
{
    int data=0; char ch=0;
    while(ch<'0' || ch>'9') ch=getchar();
    while(ch>='0' && ch<='9') data=data*10+ch-'0',ch=getchar();
    return data;
}
inline void insert(int x,int y)
{
    nex[++tot]=first[x];
    first[x]=tot;
    en[tot]=y;
}
inline void dfs1(int x,int y)
{
    dep[x]=dep[fa[x]=y]+1;
    size[x]=1;
    for(int i=first[x];i;i=nex[i])
        if(en[i]!=fa[x])
        {
            fa[en[i]]=x;
            dfs1(en[i],x);
            size[x]+=size[en[i]];
            if(size[en[i]]>size[son[x]]) son[x]=en[i];
        }
}
inline void dfs2(int x,int y)
{
    top[pre[tree[x]=++num]=x]=y;
    if(!son[x]) return;
    dfs2(son[x],y);
    for(int i=first[x];i;i=nex[i])
        if(en[i]!=fa[x] && en[i]!=son[x]) dfs2(en[i],en[i]);
}
inline void make(int v,int l,int r)
{
    f[v].l=l,f[v].r=r;
    if(l==r)
    {
        f[v].n=a[pre[l]];
        return;
    }
    int mid=(l+r)>>1;
    make(v<<1,l,mid);
    make(v<<1|1,mid+1,r);
}
inline void update(int v)
{
    if(f[v].c)
    {
        int ls=v<<1,rs=ls|1;
        f[ls].n+=f[v].c,f[rs].n+=f[v].c;
        f[ls].c+=f[v].c,f[rs].c+=f[v].c;
        f[v].c=0;
    }
}
inline void change(int v,int x,int y,int z)
{
    if(f[v].l==x && f[v].r==y)
    {
        f[v].n+=z;
        f[v].c+=z;
        return;
    }
    update(v);
    int mid=(f[v].l+f[v].r)>>1;
    if(y<=mid) change(v<<1,x,y,z); else
        if(x>mid) change(v<<1|1,x,y,z); else
        {
            change(v<<1,x,mid,z);
            change(v<<1|1,mid+1,y,z);
        }
}
inline int find(int v,int x)
{
    if(f[v].l==f[v].r) return f[v].n;
    update(v);
    int mid=(f[v].l+f[v].r)>>1;
    if(x<=mid) return find(v<<1,x);
    return find(v<<1|1,x);
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&q))
    {
        clr(first),clr(f),clr(son);
        tot=num=0;
        for(int i=1;i<=n;i++) a[i]=read();
        for(int i=1;i<=m;i++)
        {
            int x=read(),y=read();
            insert(x,y);
            insert(y,x);
        }
        dfs1(1,0);
        dfs2(1,1);
        make(1,1,n);
        while(q--)
        {
            scanf("%s",&s);
            int x=read();
            if(s[0]=='Q') printf("%d\n",find(1,tree[x])); else
            {
                int y=read(),z=read();
                if(s[0]=='D') z=-z;
                int f1=top[x],f2=top[y];
                while(f1!=f2)
                {
                    if(dep[f1]<dep[f2]) swap(x,y),swap(f1,f2);
                    change(1,tree[f1],tree[x],z);
                    x=fa[f1],f1=top[x];
                }
                if(dep[x]>dep[y]) swap(x,y);
                change(1,tree[x],tree[y],z);
            }
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,HDU1622是一道关于二叉树的题目,要求读入一系列二叉树的节点信息,输出它们的层序遍历结果。如果输入的二叉树不完整或存在重复节点,则输出"not complete"。下面是Java的实现代码: ```java import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { static class Node { int val; Node left, right; public Node(int val) { this.val = val; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String s = sc.nextLine(); if (s.isEmpty()) { continue; } String[] nodes = s.split("\\s+"); Node root = new Node(Integer.parseInt(nodes[0].substring(1))); Queue<Node> queue = new LinkedList<>(); queue.offer(root); boolean isComplete = true; for (int i = 1; i < nodes.length - 1; i += 2) { Node cur = queue.poll(); if (!nodes[i].equals("()")) { cur.left = new Node(Integer.parseInt(nodes[i].substring(1))); queue.offer(cur.left); } else { isComplete = false; } if (!nodes[i + 1].equals("()")) { cur.right = new Node(Integer.parseInt(nodes[i + 1].substring(0, nodes[i + 1].length() - 1))); queue.offer(cur.right); } else { isComplete = false; } } if (!isComplete) { System.out.println("not complete"); continue; } StringBuilder sb = new StringBuilder(); queue.offer(root); while (!queue.isEmpty()) { Node cur = queue.poll(); sb.append(cur.val).append(" "); if (cur.left != null) { queue.offer(cur.left); } if (cur.right != null) { queue.offer(cur.right); } } System.out.println(sb.toString().trim()); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值