HDOJ 3966 Aragorn's Story
树链拆分+树阵
(进入坑....)
Aragorn's Story
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2934 Accepted Submission(s): 806
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.
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.
3 2 5
1 2 3
2 1
2 3
I 1 3 5
Q 2
D 1 2 2
Q 1
Q 3
7
4
8Hint1.The number of enemies may be negative. 2.Huge input, be careful.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn=100500; struct Edge
{
int to,next;
}edge[maxn]; int Adj[maxn],Size; void init_edge()
{
memset(Adj,-1,sizeof(Adj)); Size=0;
} void add_edge(int u,int v)
{
edge[Size].to=v; edge[Size].next=Adj[u]; Adj[u]=Size++;
} int fa[maxn],deep[maxn],num[maxn],son[maxn];
int top[maxn],p[maxn],rp[maxn],pos; void init()
{
init_edge();
memset(son,-1,sizeof(son)); pos=1;
} void dfs1(int u,int pre,int d)
{
num[u]=1; fa[u]=pre; deep[u]=d;
for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(v==pre) continue;
dfs1(v,u,d+1);
num[u]+=num[v];
if(son[u]==-1||num[son[u]]<num[v])
{
son[u]=v;
}
}
} void getPOS(int u,int to)
{
top[u]=to;
p[u]=pos++;
rp[p[u]]=u;
if(son[u]!=-1) getPOS(son[u],to);
for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(v!=fa[u]&&v!=son[u])
getPOS(v,v);
}
} int tree[maxn];
int n,m,q; inline int lowbit(int x)
{
return x&(-x);
} void ADD(int p,int v)
{
for(int i=p;i<=n;i+=lowbit(i))
tree[i]+=v;
} int SUM(int p)
{
int ret=0;
for(int i=p;i;i-=lowbit(i))
ret+=tree[i];
return ret;
} void Change(int u,int v,int K)
{
int f1=top[u],f2=top[v];
while(f1!=f2)
{
if(deep[f1]<deep[f2])
{
swap(f1,f2);
swap(u,v);
}
ADD(p[f1],K);
ADD(p[u]+1,-K);
u=fa[f1];
f1=top[u];
}
if(deep[u]<deep[v]) swap(u,v);
ADD(p[v],K);
ADD(p[u]+1,-K);
} int a[maxn]; int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d%d",&n,&m,&q)!=EOF)
{
init();
for(int i=1;i<=n;i++)
scanf("%d",a+i);
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
add_edge(a,b);
add_edge(b,a);
}
dfs1(1,-1,0);
getPOS(1,1);
memset(tree,0,sizeof(tree));
for(int i=1;i<=n;i++)
{
ADD(p[i],a[i]);
ADD(p[i]+1,-a[i]);
}
char ord;
int a,b,c;
while(q--)
{
scanf("%*c %c",&ord);
if(ord=='Q')
{
scanf("%d",&a);
printf("%d\n",SUM(p[a]));
}
else
{
scanf("%d%d%d",&a,&b,&c);
if(ord=='D') c=-c;
Change(a,b,c);
}
}
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
HDOJ 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 Aragorn's Story 题目链接 树抛入门裸题,这题是区间改动单点查询,于是套树状数组就OK了 代码: #include <cstdio> #include <cst ...
- HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树
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 树链剖分
Link: http://acm.hdu.edu.cn/showproblem.php?pid=3966 这题注意要手动扩栈. 这题我交g++无限RE,即使手动扩栈了,但交C++就过了. #pragm ...
- 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 树链剖分的模版,成段更新单点查询.熟悉线段树的成段更新的话就小case啦. //树链剖分 边权修 ...
随机推荐
- 分布式MySQL数据库TDSQL架构分析
摘要:腾讯计费平台部为了解决基于内存的NoSQL解决方式HOLD平台在应对多种业务接入时的不足.结合团队在MySQL领域多年应用和优化经验,终于在MySQL存储引擎基础上,打造一套分布式SQL系统TD ...
- poj 2309 BST 使用树阵lowbit
假设领悟了树阵lowbit,这个问题很简单,底部是奇数,使用lowbit(x)寻找x父亲,然后x父亲-1是的最大数量 至于lowbit问题是如何计算,寻找x父亲,事实上x+2^x二进制结束0的数量. ...
- 工作经常使用的SQL整理,实战篇(一)
原文:工作经常使用的SQL整理,实战篇(一) 工作经常使用的SQL整理,实战篇,地址一览: 工作经常使用的SQL整理,实战篇(一) 工作经常使用的SQL整理,实战篇(二) 工作经常使用的SQL整理,实 ...
- Spring常见问题解决办法汇总
解决The prefix 'context' for element 'context:component-scan' is not bound<beans xmlns="http:/ ...
- C# WinForm多线程(二)ThreadPool 与 Timer
本文接上文,继续探讨WinForm中的多线程问题,再次主要探讨threadpool 和timer 一 ThreadPool 线程池(ThreadPool)是一种相对较简单的方法,它适应于一些需要多个 ...
- 《数据结构、算法及应用》9.(C++实施订单)
最近阅读<数据结构.算法及应用>这本书,书中的习题汇总,用自己的方法来实现这些问题.可能效率.等方面存在着非常多的问题,也可能是错误的实现.假设大家在看这本书的时候有更优更好的方法来实现, ...
- Unity3D方法来隐藏和显示对象
Unity3D作 在使用unity3d开发游戏的过程中.我们经常会遇到须要隐藏或者显示的操作,针对这一点,以下做了一些总结. 一.设置Renderer状态 在游戏的开发中,全部可以被渲染的物体都包括有 ...
- javascript动画中的“帧”
在写游戏的时候,动画移动的速度需要保持一致,为了在各个软硬件环境中速度的一致,需要考虑帧频的不同. 计算时间系数: 时间系数 = 目标FPS / 实际FPS 计算实际FPS actualFPS = 1 ...
- js运动动画
原文:js运动动画 今天简单的学了一下js运动动画,再此感谢慕课网的这位老师http://www.imooc.com/view/167,讲的很不错. 下面是我整理出来的结果. 知识点一:速度动画. 1 ...
- PLSQL Developer下报错信息显示乱码问题
PLSQL Developer下报错信息显示乱码问题 连接环境:win 7 数据库版本号:oracle 11g 模拟一个错误,查看错误提示显示"????"乱码问题,例如以下: 检查 ...