Hdu 3966-Aragorn's Story LCT,动态树
题目: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
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.
1 2 3
2 1
2 3
I 1 3 5
Q 2
D 1 2 2
Q 1
Q 3
4
8
1.The number of enemies may be negative.
2.Huge input, be careful.
#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,动态树的更多相关文章
- HDU 3966 Aragorn's Story(树链剖分)
HDU Aragorn's Story 题目链接 树抛入门裸题,这题是区间改动单点查询,于是套树状数组就OK了 代码: #include <cstdio> #include <cst ...
- 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 ...
- HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树
HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...
- hdu 3966 Aragorn's Story(树链剖分+树状数组)
pid=3966" target="_blank" style="">题目链接:hdu 3966 Aragorn's Story 题目大意:给定 ...
- HDU - 3966 Aragorn's Story(树链剖分入门+线段树)
HDU - 3966 Aragorn's Story Time Limit: 3000MS Memory Limit: 32768KB 64bit IO Format: %I64d & ...
- Hdu 3966 Aragorn's Story (树链剖分 + 线段树区间更新)
题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2 ...
- HDU 3966 Aragorn's Story 动态树 树链剖分
Aragorn's Story Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3966 Aragorn's Story(树链剖分+区间修改+单点查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:给一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路径上 ...
- HDU 4010 Query on The Trees (动态树)(Link-Cut-Tree)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4010 题意; 先给你一棵树,有 \(4\) 种操作: 1.如果 \(x\) 和 \(y\) 不在同一 ...
随机推荐
- jsp中的注释
jsp中有各种针对不同类型语言的注释,值得注意的是对于标签 <jsp:include/>是需要使用jsp注释"<%----%>", (不能是<!-- ...
- Ubuntu下Hadoop快速安装手册
http://www.linuxidc.com/Linux/2012-02/53106.htm 一.环境 Ubuntu 10.10+jdk1.6 二.下载&安装程序 1.1 Apache Ha ...
- SQL反模式部分内容笔记
规范化: 1, 以一种我们能够理解的方式表达这个世界中的事物; 2, 减少数据冗余存储, 防止异常或者不一致的数据; 3, 支持完整性约束. Tips: 提高数据的性能不在此列表中. 意义: 规范化 ...
- jQuery noConflict() 方法
如何在页面上同时使用 jQuery 和其他框架? jQuery 和其他 JavaScript 框架 正如您已经了解到的,jQuery 使用 $ 符号作为 jQuery 的简写. 如果其他 JavaSc ...
- UVA 10817 Headmaster's Headache(DP +状态压缩)
Headmaster's Headache he headmaster of Spring Field School is considering employing some new teacher ...
- boost::xml————又一次失败的尝试
尝试使用wptree来进行xml解析,又一次失败了,可以正常读取正常输出,但是使用wptree进行节点读取失败(乱码) 请看源码: DealXml.h #pragma once #include &l ...
- 11-18的学习总结(DOMSecondday)
DOM:读取访问节点对象属性 批量删除父元素下所有子节点 elem.innerHTML=""; 批量替换父元素下所有子节点 elem.innerHTML="所有子元素标签 ...
- Linux 多用户和多用户边界
1. 需求背景 2. 多用户的边界: 独立的工作目录 3. 多用户的边界:可操作/访问的资源 4. 多用户的边界: 可执行的操作 5. 多用户的特性标识: UID和GID -------------- ...
- 2016022603 - redis数据类型
Redis支持5种类型的数据类型 1.字符串:Redis字符串是字节序列.Redis字符串是二进制安全的,这意味着他们有一个已知的长度没有任何特殊字符终止,所以你可以存储任何东西,512兆为上限.[类 ...
- classpath目录
WEB-INF/ 是资源目录, 客户端不能直接访问, 这话是没错,不过现在的IDE编译器在编译时会把src下的文件(是文件,不是.java)移到WEB-INF/classes下.不过值得注意的是,sp ...