【HDU 3966】Aragorn's Story(树链剖分+线段树)

30 篇文章 0 订阅
18 篇文章 0 订阅

Aragorn's Story

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


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.


【树链剖分,实际上就是把一棵树拆成多条链,然后用线段树维护。拆链的时候,出现了重儿子之说。所谓重儿子,就是当前节点的子节点中所含子节点数最多的一个,给每个节点只保留这一个儿子,其余的儿子全部拆成单点(即去除当前点的其余儿子节点的信息,但儿子节点依旧存有父亲信息),然后将这每一条链用线段树维护。】
【剖分后的树有两个性质: 性质1:如果(v,u)为轻边,则siz[u] * 2 < siz[v]; 性质2:从根到某一点的路径上轻链、重链的个数都不大于logn。】 

【树链剖分实际并不难写,属于树链剖分的过程实际只有两个dfs,先更新fa、son、size、dep,再更新top、trn1、trn2;剩下的部分都是线段树的基本操作,只有在修改两点间路径的时候,多加了一个类似于lca的东西】  
#pragma comment(linker, "/STACK:1024000000,1024000000")//为了防止爆栈 
#include<cmath> 
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int sum[200010],delta[200010];//线段树数组,sum是维护线段树信息;delta是打标记,表示处理到当前节点时当前节点的子节点是否更新了 
int fa[50010],son[50010],size[50010],dep[50010];//fa表示当前节点的父节点;son表示当前节点的重儿子;size表示当前节点的子节点数;dep表示当前节点的深度 
int top[50010],trn1[50010],trn2[50010],tip;//top表示当前节点所在链的头元素;trn1表示当前节点在线段树中的编号;trn2表示当前线段树里的标号所对应的节点 
int a[100100],nxt[100100],p[50010],tot,num[50010];//a、nxt、p数组存边的信息;num表示点权 
int n,m,k;

inline void clear()
{
 memset(son,-1,sizeof(son));
 memset(delta,0,sizeof(delta));
 memset(p,-1,sizeof(p));
 memset(nxt,0,sizeof(nxt));
 memset(fa,0,sizeof(fa));
 tot=0; tip=0;
}//多组数据,每次都要重置 
inline void add(int x,int y)
{
 nxt[tot]=p[x]; a[tot]=y; p[x]=tot++;
 nxt[tot]=p[y]; a[tot]=x; p[y]=tot++;
}//存边的信息 

inline void dfs1(int now,int father,int h)
{
 dep[now]=h; fa[now]=father; size[now]=1;
 int u=p[now];
 while (u!=-1)
  {
   int v=a[u];
   if (v!=father)
    {
     dfs1(v,now,h+1);
     size[now]+=size[v];
     if (son[now]==-1||size[v]>size[son[now]])
son[now]=v;
    }
  u=nxt[u];
 }
return;
}//树链剖分的第一次dfs,更新dep、fa、size、son 
inline void dfs2(int now,int tp)
{
 top[now]=tp; trn1[now]=++tip; trn2[trn1[now]]=now;
 if (son[now]==-1) return;
 dfs2(son[now],tp);
 int u=p[now];
 while (u!=-1)
  {
   int v;
   v=a[u];
   if (v!=fa[now]&&v!=son[now])
    dfs2(v,v);
   u=nxt[u];
  }
 return;
}//树链剖分的第二次dfs,更新top、trn1、trn2 
//树链剖分部分
 
inline void updata(int now)
{
 sum[now]=max(sum[now<<1],sum[(now<<1)+1]);
}//更新线段树信息 
inline void pushdata(int now,int l,int r,int mid)
{
 if (delta[now]!=0)
  {
   sum[(now<<1)]+=delta[now]*(mid-l+1);
   delta[(now<<1)]+=delta[now];
   sum[(now<<1)+1]+=delta[now]*(r-mid);
   delta[(now<<1)+1]+=delta[now];
   delta[now]=0;
  }
 return;
}//标记下放 
inline void build(int now,int l,int r)
{
 if (l==r)
   {sum[now]=num[trn2[l]]; return;}
 int mid=(l+r)>>1;
 build((now<<1),l,mid);
 build((now<<1)+1,mid+1,r);
 updata(now);
}//建线段树 
inline int ask(int now,int l,int r,int val)
{
 if (l==r)
  return sum[now];
 int mid=(l+r)>>1;
 pushdata(now,l,r,mid);
 int ans=0;
 if (val<=mid)
   ans=ask((now<<1),l,mid,val);
  else ans=ask(((now<<1)+1),mid+1,r,val);
 updata(now);
 return ans;
}//单点查询 
inline void change1(int now,int l,int r,int al,int ar,int val)
{
 if (al<=l&&ar>=r)
  {
  sum[now]+=val*(r-l+1);
  delta[now]+=val;
  return;
  }
 int mid=(l+r)>>1;
 pushdata(now,l,r,mid);
 if (al<=mid) change1((now<<1),l,mid,al,ar,val);
 if (ar>mid) change1(((now<<1)+1),mid+1,r,al,ar,val);
 updata(now);
 return;
}//区间修改 

inline void change(int x,int y,int val)
{
 while (top[x]!=top[y])
  {
   if (dep[top[x]]<dep[top[y]]) swap(x,y);
   change1(1,1,n,trn1[top[x]],trn1[x],val);
   x=fa[top[x]];
   }
 if (dep[x]>dep[y]) swap(x,y);
 change1(1,1,n,trn1[x],trn1[y],val);
 return;
}//类似于lca,更新两点间的所有信息(因为两个点不一定在同一条链上,不能直接用线段树的区间修改) 

int main()
{
 int i;
 while (scanf("%d%d%d",&n,&m,&k)==3){
  clear();
  tot=0; tip=0;
  for (i=1;i<=n;++i)
   scanf("%d",&num[i]);
  for (i=1;i<=m;++i)
   {
    int x,y;
    scanf("%d%d",&x,&y);
    add(x,y);
  }
  dfs1(1,0,0);
  dfs2(1,1);
  build(1,1,n);
  for (i=1;i<=k;++i)
   {
   char s[5];
   scanf("%s",s);
   if (s[0]=='Q')
    {  
     int x,ans;
     scanf("%d",&x);
     printf("%d\n",ask(1,1,n,trn1[x]));
    }
   else
   {
    int x,y,z;
    scanf("%d%d%d",&x,&y,&z);
    if (s[0]=='D') z=-z;
    change(x,y,z);
   }
  }
 }    
 return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值