Aragorn's Story

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

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
 
基于点权 单点查询 修改路径上的点权 模板
 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
#define ll long long
#define mod 998244353
const int N=;
const int INF=0x3f3f3f3f;
struct Edge
{
int to,next;
} edge[*N];
int head[N];
int top[N];
int fa[N];
int deep[N];
int num[N];
int p[N];
int fp[N];
int son[N];
int pos;
int tot;
void init()
{
tot=;
memset(head,-,sizeof(head));
pos=;
memset(son,-,sizeof(son));
}
void addedge(int u,int v)
{
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void dfs1(int u,int pre,int d)
{
deep[u]=d;
fa[u]=pre;
num[u]=;
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].to;
if(v!=pre)
{
dfs1(v,u,d+);
num[u]+=num[v];
if(son[u]==-||num[v]>num[son[u]])
son[u]=v;
}
}
}
void getpos(int u,int sp)
{
top[u]=sp;
p[u]=pos++;
fp[p[u]]=u;
if(son[u]==-) return ;
getpos(son[u],sp);
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].to;
if(v!=son[u]&&v!=fa[u])
getpos(v,v);
}
} int lowbit(int x)
{
return x&(-x);
}
int c[N];
int n;
int sum(int i)
{
int s=;
while(i>)
{
s+=c[i];
i-=lowbit(i);
}
return s;
}
void add(int i,int val)
{
while(i<=n)
{
c[i]+=val;
i+=lowbit(i);
}
}
void change(int u,int v,int val)
{
int f1=top[u],f2=top[v];
int tmp=;
while(f1!=f2)
{
if(deep[f1]<deep[f2])
{
swap(f1,f2);
swap(u,v);
}
add(p[f1],val);
add(p[u]+,-val);
u=fa[f1];
f1=top[u];
}
if(deep[u]>deep[v]) swap(u,v);
add(p[u],val);
add(p[v]+,-val);
}
int a[N];
int main()
{
int M,P;
while(scanf("%d %d %d",&n,&M,&P)!=EOF)
{
int u,v;
int C1,C2,K;
char op[];
init();
for(int i=; i<=n; i++)
scanf("%d",&a[i]);
while(M--)
{
scanf("%d %d",&u,&v);
addedge(u,v);
addedge(v,u);
} dfs1(,,);
getpos(,);
memset(c,,sizeof(c));
for(int i=; i<=n; i++)
{
add(p[i],a[i]);
add(p[i]+,-a[i]);
} while(P--)
{
scanf("%s",op);
if(op[]=='Q')
{
scanf("%d",&u);
printf("%d\n",sum(p[u]));
}
else
{
scanf("%d%d%d",&C1,&C2,&K);
if(op[]=='D')
K=-K;
change(C1,C2,K);
}
}
}
return ;
}

HDU 3966 树链剖分+树状数组 模板的更多相关文章

  1. hdu 3966 Aragorn's Story(树链剖分+树状数组/线段树)

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

  2. Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组

    Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...

  3. 洛谷 P3384 【模板】树链剖分-树链剖分(点权)(路径节点更新、路径求和、子树节点更新、子树求和)模板-备注结合一下以前写的题目,懒得写很详细的注释

    P3384 [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节 ...

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

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

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

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

  6. HDU 3966 /// 树链剖分+树状数组

    题意: http://acm.hdu.edu.cn/showproblem.php?pid=3966 给一棵树,并给定各个点权的值,然后有3种操作: I x y z : 把x到y的路径上的所有点权值加 ...

  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 5044 (树链剖分+树状数组+点/边改查)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5044 题目大意:修改链上点,修改链上的边.查询所有点,查询所有边. 解题思路: 2014上海网赛的变 ...

  9. HDU 5293 Train chain Problem - 树链剖分(树状数组) + 线段树+ 树型dp

    传送门 题目大意: 一颗n个点的树,给出m条链,第i条链的权值是\(w_i\),可以选择若干条不相交的链,求最大权值和. 题目分析: 树型dp: dp[u][0]表示不经过u节点,其子树的最优值,dp ...

  10. bzoj1146整体二分+树链剖分+树状数组

    其实也没啥好说的 用树状数组可以O(logn)的查询 套一层整体二分就可以做到O(nlngn) 最后用树链剖分让序列上树 #include<cstdio> #include<cstr ...

随机推荐

  1. # 《网络对抗》Exp1 PC平台逆向破解20155337祁家伟

    <网络对抗>Exp1 PC平台逆向破解20155337祁家伟 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会 ...

  2. [BZOJ2138]stone[霍尔定理+线段树]

    题意 一共有 \(n\) 堆石子,每堆石子有一个数量 \(a\) ,你要进行 \(m\) 次操作,每次操作你可以在满足前 \(i-1\) 次操作的回答的基础上选择在 \([L_i,R_i]\) 区间中 ...

  3. node.js学习笔记(四)——EventEmitter

    error 事件 EventEmitter 定义了一个特殊的事件 error,它包含了错误的语义,我们在遇到异常的时候通常会触发 error 事件.当 error 被触发时,EventEmitter ...

  4. chrome播放m3u8視頻失败

    由于项目后台需要播放m3u8视频,但此视频格式在移动端和Safari支持比较友善但是PC浏览器中都不太尽如人意,所以想在Chrome中播放只能借助第三方插件来播放. 有一款Video.js插件极大的简 ...

  5. 之前专门为IE6、7开发的网站如何迁移到IE10及可能遇到的问题和相应解决方案汇总

    由于周末,早晨起来的比较晚,打开博客园转转,看到这样的一篇博文,内容大致是说,服务器由于升级导致的用Asp.NET的UpdatePanel写的下拉联动失效了,这让我联想到了前段时间看到的一份资料,关于 ...

  6. uwsgi+django架构程序内部无法获取全局变量

    近期开发了一个djangoi程序,用django自带的python manage.py runserver 0.0.0.0:80 运行方式无任何问题,但用django+nginx+uwsg部署运行有时 ...

  7. UI Recorder 安装教程(二)

    前言: UI Recorder支持无线native app(Android, iOS)录制, 基于macaca实现:https://macacajs.com/ 本次教程只针对无线native app( ...

  8. 第二阶段冲刺——four

    个人任务: 季方:实现团队博客作业查询. 王金萱:优化统计团队博客结果界面的显示. 马佳慧:选择功能界面的背景设计. 司宇航:用servlet完成名单打印功能. 站立会议: 任务看板和燃尽图:

  9. oracle union

    union 取并集,去重不仅去掉两个集合之间的重复,也会去掉集合的自重复

  10. 2017-8-20 HTTP协议

    http协议 http协议是一种超文本传输协议(一种约定) 三大特性:无状态,媒体独立,无连接: HTTP 工作原理 HTTP协议工作于客户端-服务端架构为上.浏览器作为HTTP客户端通过URL向HT ...