hdu 3966 Aragorn's Story

Aragorn's Story

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


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.

题目大意:
给出多组数据,每组数据第一行输入n,m,p,n 表示节点数,m表示边的信息,p表示操作数  
操作中:‘I' 表示[x,y]加z
               'D' 表示[x,y] 减z
               ’Q‘表示查询X点的值
题解:树链剖分模板题
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define N 50003
#define M 100003
using namespace std;
int n,m,q,tot,sz;
int point[N],nxt[M],v[M],val[N],tr[M*10],pos[N],maxson[N];
int top[N],delta[M*10],size[N],deep[N],fa[N],tv[N];
void clear()//清零
{
  tot=0; sz=0;
  memset(point,0,sizeof(point));  memset(nxt,0,sizeof(nxt));
  memset(maxson,0,sizeof(maxson));  memset(pos,0,sizeof(pos));
  memset(tr,0,sizeof(tr));  memset(val,0,sizeof(val));
  memset(tv,0,sizeof(tv));  memset(delta,0,sizeof(delta));
  memset(top,0,sizeof(top));  memset(deep,0,sizeof(deep));
  memset(size,0,sizeof(size));  memset(fa,0,sizeof(fa));
}
void add(int x,int y)//处理边权信息
{
  tot++; nxt[tot]=point[x]; point[x]=tot; v[tot]=y;
  tot++; nxt[tot]=point[y]; point[y]=tot; v[tot]=x;
}
void dfs1(int x,int f,int dep)//建树
{
  size[x]=1; deep[x]=dep; 
  int k=0;
  for (int i=point[x];i!=0;i=nxt[i])
   if (v[i]!=f)
    {
      fa[v[i]]=x;
	  dfs1(v[i],x,dep+1);
	  if (size[v[i]]>size[k])
	   k=v[i];
	  maxson[x]=k;
	  size[x]+=size[v[i]];	
	}
}
void dfs2(int x,int chain)
{
  pos[x]=++sz;  tv[sz]=val[x]; top[x]=chain;
  if (maxson[x]) dfs2(maxson[x],chain);
  for (int i=point[x];i!=0;i=nxt[i])
    if (deep[v[i]]>deep[x]&&v[i]!=maxson[x])
      dfs2(v[i],v[i]);
}
void pushdown(int now,int l,int r)//标记下放
{
  if (!delta[now]) return;
  int mid=(l+r)/2;
  tr[now<<1]+=(mid-l+1)*delta[now];
  delta[now<<1]+=delta[now];
  tr[(now<<1)+1]+=(r-mid)*delta[now];
  delta[(now<<1)+1]+=delta[now];
  delta[now]=0;
}
void build(int now,int l,int r)//线段树建树
{
	if (l==r)
	 {
	 	tr[now]=tv[l];
	 	return;
	 }
	int mid=(l+r)/2;
	build(now<<1,l,mid);
	build((now<<1)+1,mid+1,r);
	tr[now]=tr[now<<1]+tr[(now<<1)+1];
}
void change(int now,int l,int r,int ll,int rr,int x)//区间修改
{
  if(l>=ll&&r<=rr)
   {
   	 tr[now]+=x*(r-l+1);
   	 delta[now]+=x;
   	 return;
   }
  int mid=(l+r)/2;
  pushdown(now,l,r);
  if (ll<=mid)
    change(now<<1,l,mid,ll,rr,x);
  if (rr>mid)
    change((now<<1)+1,mid+1,r,ll,rr,x);
  tr[now]=tr[now<<1]+tr[(now<<1)+1];
}
void solve(int x,int y,int vv)
{
  while (top[x]!=top[y])
   {
   	if (deep[top[x]]<deep[top[y]])  swap(x,y);
   	change(1,1,n,pos[top[x]],pos[x],vv);
   	x=fa[top[x]];
   }
  if (deep[x]>deep[y]) swap(x,y);
  change(1,1,n,pos[x],pos[y],vv);
}
int ask(int now,int l,int r,int x)//点查询
{
  if (l==r) return tr[now];
  pushdown(now,l,r);
  int mid=(l+r)/2;
  if (x<=mid)  return ask(now<<1,l,mid,x);
  else return ask((now<<1)+1,mid+1,r,x);
}
int main()
{
  while (scanf("%d%d%d",&n,&m,&q)==3)
   {
   	clear();
   	for (int i=1;i<=n;i++) scanf("%d",&val[i]);
   	for (int i=1;i<=m;i++)
   	 {
   	 	int x,y; scanf("%d%d",&x,&y);
   	 	add(x,y);
   	 }
   	dfs1(1,0,1);
   	dfs2(1,1);
    char c;
    build(1,1,n);
   	for (int i=1;i<=q;i++)
   	 {
   	 	cin>>c;
   	 	if (c=='I')  
   	 	 {
   	 	 	int x,y,z; scanf("%d%d%d",&x,&y,&z);
   	 	 	solve(x,y,z);
   	 	 }
   	 	else
   	 	 if (c=='D')
   	 	  {
   	 	  	int x,y,z; scanf("%d%d%d",&x,&y,&z);
   	 	  	solve(x,y,-z);
   	 	  }
   	 	 else
   	 	  {
   	 	  	 int x; scanf("%d",&x);
   	 	  	 printf("%d\n",ask(1,1,n,pos[x]));
   	 	  }
   	 }
   }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值