HDU 3966 树链剖分+树状数组 模板
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
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
#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 树链剖分+树状数组 模板的更多相关文章
- hdu 3966 Aragorn's Story(树链剖分+树状数组/线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: 给出一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路 ...
- Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组
Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...
- 洛谷 P3384 【模板】树链剖分-树链剖分(点权)(路径节点更新、路径求和、子树节点更新、子树求和)模板-备注结合一下以前写的题目,懒得写很详细的注释
P3384 [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节 ...
- 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 /// 树链剖分+树状数组
题意: http://acm.hdu.edu.cn/showproblem.php?pid=3966 给一棵树,并给定各个点权的值,然后有3种操作: I x y z : 把x到y的路径上的所有点权值加 ...
- HDU 3966 Aragorn's Story (树链剖分+树状数组)
Aragorn's Story Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 5044 (树链剖分+树状数组+点/边改查)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5044 题目大意:修改链上点,修改链上的边.查询所有点,查询所有边. 解题思路: 2014上海网赛的变 ...
- HDU 5293 Train chain Problem - 树链剖分(树状数组) + 线段树+ 树型dp
传送门 题目大意: 一颗n个点的树,给出m条链,第i条链的权值是\(w_i\),可以选择若干条不相交的链,求最大权值和. 题目分析: 树型dp: dp[u][0]表示不经过u节点,其子树的最优值,dp ...
- bzoj1146整体二分+树链剖分+树状数组
其实也没啥好说的 用树状数组可以O(logn)的查询 套一层整体二分就可以做到O(nlngn) 最后用树链剖分让序列上树 #include<cstdio> #include<cstr ...
随机推荐
- 20155334 《网络攻防》Exp5 MSF基础应用
一.基础问题回答 解释exploit,payload,encode是什么: 项目 作用 exploit 是负载有用代码的交通工具,让代码到达目的地,并作用 payload 是有具体功能的代码,能够完成 ...
- 洛咕 P2336 [SCOI2012]喵星球上的点名
洛咕 P2336 [SCOI2012]喵星球上的点名 先求出SA和height,一个点名串对应的就是一段区间,还有很多个点,就转化成了 有很多个区间,很多个点集,对每个区间计算和多少个点集有交,对每个 ...
- docker之网络管理
一.安装网桥管理工具 [root@node03 web]# yum install bridge-utils -y 二.docker网络的4种模式 host模式,使用--net=host指定. con ...
- 萌新程序媛的首个作品,基于NoSQL的内容管理及低码开发平台
尽管入行有一段时间了,但之前还从来没想过要开发一款完整的软件产品.这个我跟朋友开发的第一款软件,希望大家帮我们多宣传推广.首个版本肯定有很多的不足,大家也给我们多提意见,还有很多规划中的功能要在之后的 ...
- B1022. D进制的A+B
除基取余法 #include<bits/stdc++.h> using namespace std; stack<int> s; int main(){ long long a ...
- 实验---反汇编一个简单的C程序(杨光)
反汇编一个简单的C程序 攥写人:杨光 学号:20135233 ( *原创作品转载请注明出处*) ( 学习课程:<Linux内核分析>MOOC课程http://mooc.study.163 ...
- C++的OOP特性
内存模型和名称空间 存储持续性,作用域和链接性 C++有三种方案来存储数据 自动存储持续性:在函数定义中声明的变量,包括函数参数.在函数或代码块开始执行时创建.执行完函数或者代码块,内存自动释放. 静 ...
- 05 方法与数组笔记【JAVA】
---恢复内容开始--- 1:方法(掌握) (1)方法:就是完成特定功能的代码块. 注意:在很多语言里面有函数的定义,而在Java中,函数被称为方法. (2)格式: 修饰符 返回值类型 方法名(参数类 ...
- spring整合redis(jedis)
真是一步一个坑阿,学点新技术,这么难,这个异常: java.lang.IllegalStateException: Could not load TestContextBootstrapper [nu ...
- qemu-img.exe 工具 简介
1. 下载地址 https://cloudbase.it/qemu-img-windows/ 2. 解压缩 然后扔到 system32目录下 或者是 修改环境变量-- 我很懒,我决定扔到system3 ...