hdu 3966 树链剖分+线段树

Aragorn's Story

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2181    Accepted Submission(s): 589


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

题意:给一棵树,并给定各个点权的值,然后有3种操作:

I C1 C2 K: 把C1与C2的路径上的所有点权值加上K

D C1 C2 K:把C1与C2的路径上的所有点权值减去K

Q C:查询节点编号为C的权值

 

分析:典型的树链剖分题目,先进行剖分,然后用线段树去维护即可,注意HDU的OJ采用Windows系统,容易爆栈,所以在代码

前面加上:#pragma comment(linker, "/STACK:1024000000,1024000000")进行手动扩栈。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 50005
using namespace std;
struct node
{
    int u,v,next;
}bian[N*2];
struct pp
{
    int x,y,p;
}a[N*3];
int b[N],sz[N],dep[N],son[N],top[N],head[N],father[N],id,e,ti[N];
void add(int u,int v)
{
    bian[e].u=u;
    bian[e].v=v;
    bian[e].next=head[u];
    head[u]=e++;
}
void dfs1(int u,int fa)
{
    int i,v;
    dep[u]=dep[fa]+1; sz[u]=1; son[u]=0;  father[u]=fa;
    for(i=head[u];i!=-1;i=bian[i].next)
    {
        v=bian[i].v;
        if(v==fa)  continue;
        dfs1(v,u);
        sz[u]+=sz[v];
        if(sz[son[u]]<sz[v])
            son[u]=v;
    }
}
void dfs2(int u,int fa)
{
    int i,v;
    top[u]=fa;
    ti[u]=id++;
    if(son[u]!=0)
        dfs2(son[u],fa);
    for(i=head[u];i!=-1;i=bian[i].next)
    {
        v=bian[i].v;
        if(v==son[u]||v==father[u])
            continue;
        dfs2(v,v);
    }
}
void pushdown(int t)
{
    int temp=t<<1;
    if(a[t].p)
    {
        a[temp].p+=a[t].p;
        a[temp+1].p+=a[t].p;
        a[t].p=0;
    }
}
void build(int t,int x,int y)
{
    a[t].x=x; a[t].y=y; a[t].p=0;
    if(x==y)  return ;
    int mid=(x+y)>>1,temp=t<<1;
    build(temp,x,mid);
    build(temp+1,mid+1,y);
}
void update(int t,int x,int y ,int k)
{
    if(a[t].x==x&&a[t].y==y)
    {
        a[t].p+=k;
        return;
    }
    pushdown(t);
    int mid=(a[t].x+a[t].y)>>1,temp=t<<1;
    if(y<=mid)
        update(temp,x,y,k);
    else if(x>mid)
        update(temp+1,x,y,k);
    else
    {
        update(temp,x,mid,k);
        update(temp+1,mid+1,y,k);
    }
}
int query(int t,int x)
{
    if(a[t].x==a[t].y)
       return a[t].p;
    int mid=(a[t].x+a[t].y)>>1,temp=t<<1;
    pushdown(t);
    if(x<=mid)
        return query(temp,x);
    else
        return query(temp+1,x);
}
void lca(int x,int y,int k)
{
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]])
            swap(x,y);
        update(1,ti[top[x]],ti[x],k);
        x=father[top[x]];
    }
    if(dep[x]>dep[y])
        swap(x,y);
     update(1,ti[x],ti[y],k);
}
int main()
{
    int n,m,p,i,u,v,k;
    char str[10];
    while(scanf("%d%d%d",&n,&m,&p)!=EOF)
    {
        for(i=1;i<=n;i++)
            scanf("%d",&b[i]);
        e=0;
        memset(head,-1,sizeof(head));
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
        }
        dep[1]=0; sz[0]=0; id=1;
        dfs1(1,1); dfs2(1,1);
        build(1,1,n);
        while(p--)
        {
            scanf("%s",str);
            if(str[0]=='I')
            {
                scanf("%d%d%d",&u,&v,&k);
                lca(u,v,k);
            }
            else if(str[0]=='D')
            {
                scanf("%d%d%d",&u,&v,&k);
                lca(u,v,-k);
            }
            else
            {//询问的时候,先不要在树上加上原来的值,最后加上就行了,提前处理的话,超时。
                scanf("%d",&u);
                printf("%d\n",b[u]+query(1,ti[u]));
            }
        }
    }
    return 0;
}



1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值