题目:http://acm.hdu.edu.cn/showproblem.php?pid=3966

Aragorn's Story

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

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
 
Recommend
 
题意:
给定一棵树和树上的点权,每次要区间加上一个数或减去一个数,查询某一个结点的权值。 
 题解:
直接用LCT区间修改即可。。。
查询前要先access一下。。。
多组数据每次要清空一下树。。。QAQ Tle了一发。。。
 #include<bits/stdc++.h>
using namespace std;
#define MAXN 50010
struct node
{
int left,right,val;
}tree[MAXN];
int father[MAXN],rev[MAXN],tag[MAXN],Stack[MAXN];
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
int isroot(int x)
{
return tree[father[x]].left!=x&&tree[father[x]].right!=x;
}
void pushdown(int x)
{
int l=tree[x].left,r=tree[x].right;
if(rev[x]!=)
{
rev[x]^=;rev[l]^=;rev[r]^=;
swap(tree[x].left,tree[x].right);
}
if(tag[x]!=)
{
tag[l]+=tag[x];tag[r]+=tag[x];
tree[l].val+=tag[x];tree[r].val+=tag[x];
tag[x]=;
}
}
void rotate(int x)
{
int y=father[x],z=father[y];
if(!isroot(y))
{
if(tree[z].left==y)tree[z].left=x;
else tree[z].right=x;
}
if(tree[y].left==x)
{
father[x]=z;father[y]=x;tree[y].left=tree[x].right;tree[x].right=y;father[tree[y].left]=y;
}
else
{
father[x]=z;father[y]=x;tree[y].right=tree[x].left;tree[x].left=y;father[tree[y].right]=y;
}
}
void splay(int x)
{
int top=,i,y,z;Stack[++top]=x;
for(i=x;!isroot(i);i=father[i])Stack[++top]=father[i];
for(i=top;i>=;i--)pushdown(Stack[i]);
while(!isroot(x))
{
y=father[x];z=father[y];
if(!isroot(y))
{
if((tree[y].left==x)^(tree[z].left==y))rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x)
{
int last=;
while(x!=)
{
splay(x);
tree[x].right=last;
last=x;x=father[x];
}
}
void makeroot(int x)
{
access(x);splay(x);rev[x]^=;
}
void link(int u,int v)
{
makeroot(u);father[u]=v;splay(u);
}
void cut(int u,int v)
{
makeroot(u);access(v);splay(v);father[u]=tree[v].left=;
}
int findroot(int x)
{
access(x);splay(x);
while(tree[x].left!=)x=tree[x].left;
return x;
}
int main()
{
int n,m,p,bb,ee,k,camp,i;
char fh[];
while(scanf("%d %d %d",&n,&m,&p)!=EOF)
{
for(i=;i<=n;i++)tree[i].val=tree[i].left=tree[i].right=,tag[i]=,father[i]=,rev[i]=;
for(i=;i<=n;i++)tree[i].val=read();
for(i=;i<=m;i++)
{
bb=read();ee=read();link(bb,ee);
}
for(i=;i<=p;i++)
{
scanf("%s",fh);
if(fh[]=='I')
{
bb=read();ee=read();k=read();
makeroot(bb);access(ee);splay(ee);
tag[ee]+=k;tree[ee].val+=k;
}
else if(fh[]=='D')
{
bb=read();ee=read();k=read();
makeroot(bb);access(ee);splay(ee);
tag[ee]-=k;tree[ee].val-=k;
}
else
{
camp=read();
access(camp);
printf("%d\n",tree[camp].val);
}
}
}
fclose(stdin);
fclose(stdout);
return ;
}

Hdu 3966-Aragorn's Story LCT,动态树的更多相关文章

  1. HDU 3966 Aragorn&#39;s Story(树链剖分)

    HDU Aragorn's Story 题目链接 树抛入门裸题,这题是区间改动单点查询,于是套树状数组就OK了 代码: #include <cstdio> #include <cst ...

  2. Hdu 4010-Query on The Trees LCT,动态树

    Query on The Trees Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Othe ...

  3. HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树

    HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...

  4. hdu 3966 Aragorn&#39;s Story(树链剖分+树状数组)

    pid=3966" target="_blank" style="">题目链接:hdu 3966 Aragorn's Story 题目大意:给定 ...

  5. HDU - 3966 Aragorn's Story(树链剖分入门+线段树)

    HDU - 3966 Aragorn's Story Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & ...

  6. Hdu 3966 Aragorn's Story (树链剖分 + 线段树区间更新)

    题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2 ...

  7. HDU 3966 Aragorn's Story 动态树 树链剖分

    Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. hdu 3966 Aragorn's Story(树链剖分+区间修改+单点查询)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:给一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路径上 ...

  9. HDU 4010 Query on The Trees (动态树)(Link-Cut-Tree)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4010 题意; 先给你一棵树,有 \(4\) 种操作: 1.如果 \(x\) 和 \(y\) 不在同一 ...

随机推荐

  1. Swift基础知识入门(基于Swift2.0)

    //: Playground - noun: a place where people can play import UIKit // Swift中不需要设置main函数入口,编译器会在全局函数中自 ...

  2. 简单的完全背包HDU1114

    今天广州下雨啦,不过没关系啦,反正我最近也都在刷题学习算法. 昨天做了五题01背包,今天还是背包,不过是完全背包,估计做动态规划要持续好一段时间,一开始选了一道简单题目啦. HDU1114,看了小一段 ...

  3. Fatal error: Uncaught SoapFault exception

    Warning: SoapClient::SoapClient() expects parameter 2 to be array, boolean given in  login\login.php ...

  4. Linux 下的 blog客户端

    apt-cahce search metaweblog found gnome-blog blogilo try blogilo

  5. 闭包 this,arguemnts 问题

    因为每个函数在被调用时,其活动对象都会自动取得两个特殊的变量,this和arguments.内部函数的搜索这两个变量时,只会搜索到其活动对象为止.因此永远不可能直接访问到外部函数中的这两个变量     ...

  6. PHPCMS标签大全

    {$head[title]} 页面标题,用法: {$phpcms[sitename]} 网站名称 用法: {$head[keywords]} 要害字 用法: {$head[description]} ...

  7. const变量与define定义常量的区别

    一.概念性区别 const 变量就是在普通变量前边加上一个关键字const,它赋值的唯一机会就是“定义时”,此变量不能被程序修改,存储在rodata区. define定义的是常量,不是变量,所以编译器 ...

  8. uboot内存分布

    一.uboot的内存分布图 山人自己画的图 华清远见的图 二.如何修改编译地址 board/smdk2410/config.mk中定义有TEXT_BASE TEXT_BASE = 0x33F80000 ...

  9. 编程框架—Autofac

    Autofac是一款轻量级的IOC框架,性能高. Autofac基本使用步骤: 1.创建容器建造者(Builder): 2.对Builder注册类型. 3.Buildder创建容器(Container ...

  10. decimall类型数据

    同样是decimal(18,5)   和 decimal(18,4)  在VB中经过几次转化过后,数据就有可能改变. 遇到的情况 decimal(18,5)到  decimal(18,4)转换过程中数 ...