HDU 3966 Aragorn's Story 树链剖分+BIT区间修改/单点询问
Aragorn's Story
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.
题意:
给一棵树,并给定各个点权的值,然后有3种操作:
I C1 C2 K: 把C1与C2的路径上的所有点权值加上K
D C1 C2 K:把C1与C2的路径上的所有点权值减去K
Q C:查询节点编号为C的权值
题解:
先划分树链再套 树状数组就可以了
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5+, M = , mod = 1e9 + , inf = 0x3f3f3f3f;
typedef long long ll; int head[N],t=,sz,v[N],pos[N],belong[N],siz[N],vis[N],n,m,p,C[N],deep[N],fa[N][];
struct data{int to,next;}e[N*];
//树链
void add(int u,int v) {e[t].to = v;e[t].next = head[u];head[u]=t++;}
void dfs1(int x) {
siz[x] = vis[x] =;
for(int i=;i<=;i++) {
if(deep[x] < (<<i)) break;
fa[x][i] = fa[fa[x][i-]][i-];
}
for(int i=head[x];i;i=e[i].next) {
if(vis[e[i].to]) continue;
deep[e[i].to] = deep[x]+;
fa[e[i].to][] = x;
dfs1(e[i].to);
siz[x]+=siz[e[i].to];
}
}
void dfs2(int x,int chain) {
int k=;
sz++;
pos[x] =sz;
belong[x] = chain;
for(int i=head[x];i;i=e[i].next)
if(deep[x]<deep[e[i].to]&&siz[k]<siz[e[i].to]) k = e[i].to;
if(k==)return ;
dfs2(k,chain);
for(int i=head[x];i;i=e[i].next)
if(deep[x]<deep[e[i].to]&&k!=e[i].to) dfs2(e[i].to,e[i].to);
}
int lca(int x,int y) {
if(deep[x] < deep[y]) swap(x,y);
int t = deep[x] - deep[y];
for(int i=;i<=;i++)
if(t&(<<i)) x = fa[x][i];
for(int i=;i>=;i--)
if(fa[x][i]!=fa[y][i]) {
x = fa[x][i];
y = fa[y][i];
}
if(x==y) return x;
return fa[x][];
}
//数据结构
void update(int x,int c) {
while(x<=n) {
C[x]+=c;
x+=x&-x;
}
}
int ask(int x) {
int sum=;
while(x>) {
sum+=C[x];
x-=x&-x;
}
return sum;
}
void solvechange(int x,int f,int c) {
while(belong[x]!=belong[f]) {
update(pos[belong[x]],c);
update(pos[x]+,-c);
x = fa[belong[x]][];
}
update(pos[f],c);
update(pos[x]+,-c);
}
void scan() {
for(int i=;i<=n;i++) scanf("%d",&v[i]);
for(int i=;i<n;i++) {
int x,y;
scanf("%d%d",&x,&y);
add(x,y);add(y,x);
}
dfs1();
dfs2(,); for(int i=;i<=n;i++) {
update(pos[i],v[i]);
update(pos[i]+,-v[i]);
}
char ch[];
int x,y,c;
for(int i=;i<=p;i++) {
scanf("%s",ch);
if(ch[] == 'I') {
scanf("%d%d%d",&x,&y,&c);
int t = lca(x,y);
solvechange(x,t,c);
solvechange(y,t,c);
solvechange(t,t,-c);
}
else if(ch[] == 'D') {
scanf("%d%d%d",&x,&y,&c);
int t = lca(x,y);
solvechange(x,t,-c);
solvechange(y,t,-c);
solvechange(t,t,c);
}
else {
scanf("%d",&x);
printf("%d\n",ask(pos[x]));
}
}
}
void init() {
memset(head,,sizeof(head));
t=;
sz=;
memset(fa,,sizeof(fa));
memset(deep,,sizeof(deep));
memset(v,,sizeof(v));
memset(vis,,sizeof(vis));
memset(C,,sizeof(C));
memset(pos,,sizeof(pos));
}
int main() {
while(scanf("%d%d%d",&n,&m,&p)!=EOF) {
init();
scan();
}
}
HDU 3966 Aragorn's Story 树链剖分+BIT区间修改/单点询问的更多相关文章
- HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树
HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...
- 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 : 树链剖分 O(nlogn)建树 O((logn)²)修改与查询
/** problem: http://acm.hdu.edu.cn/showproblem.php?pid=3966 裸板 **/ #include<stdio.h> #include& ...
- 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 (树链剖分入门题)
树上路径区间更新,单点查询. 线段树和树状数组都可以用于本题的维护. 线段树: #include<cstdio> #include<iostream> #include< ...
- HDU 3966 Aragorn's Story (树链点权剖分,成段修改单点查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 树链剖分的模版,成段更新单点查询.熟悉线段树的成段更新的话就小case啦. //树链剖分 边权修 ...
- HDU 3966 Aragorn's Story 树链拋分
一.写在前面 终于开始开坑link-cut-tree这个了,对于网上找到的大佬的前进路线,进行了一番研发,发现实际上可以实现对于树链拋分的制作.经历了若干长时间之后终于打了出来(为什么每次学什么东西都 ...
随机推荐
- IntelliJ IDEA 10 配置 Tomcat7
http://blog.csdn.net/neutrojan/article/details/8064145
- Exception in thread "main" java.lang.IllegalArgumentException: Illegal character in query at index 189......
Exception in thread "main" java.lang.IllegalArgumentException: Illegal character in query ...
- BZOJ:3441 乌鸦喝水
bzoj:3441 乌鸦喝水 题目传送门 Description 一只乌鸦在自娱自乐,它在面前放了n个有魔力的水缸,水缸里装有无限的水. 他准备从第1个水缸飞到第n个水缸,共m次.在飞过一个水缸的过程 ...
- 卷积操作中的矩阵乘法(gemm)—— 为什么矩阵乘法是深度学习的核心所在
1. 全连接 k 个输入: n 个神经元: 每个神经元都会学到一组权值向量,以和输入进行内积运算: n 个输出: 2. 卷积 卷积操作对于高维(多个平面)的输入,单个卷积核的深度应和输入的深度(dep ...
- 【BZOJ 2724】 蒲公英
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2724 [算法] 分块算法在线维护区间众数 分块算法的精髓就在于 : 大段维护,局部朴 ...
- [jzoj 3175] 数树数 解题报告 (树链剖分)
interlinkage: https://jzoj.net/senior/#main/show/3175 description: 给定一棵N 个节点的树,标号从1~N.每个点有一个权值.要求维护两 ...
- ROS-导航功能-RVIZ
前言:slam使用激光雷达完成了地图构建,现在介绍一下自主导航.move_base用于实现最优路径规划,amcl用于实现机器人定位. 前提:已下载并编译了相关功能包集,如还未下载,可通过git下载:h ...
- 关于Angular官网《英雄指南》教程几点问题修正(此问题在2018年4月份有效,以后可能就订正了)
1.官网中在导入“of”关键字时的引用为: import { Observable, of } from 'rxjs'; 应该改为: import { Observable } from 'rxjs/ ...
- requests模块的高级用法
SSL Cert Verification #证书验证(大部分网站都是https) import requests respone=requests.get('https://www.12306.cn ...
- SPOJ CIRU
SPOJ CIRU 题意 给出n个圆,求他们覆盖的面积. 解法 自适应Simpson,但需要将圆离散化一下,以保证我们查询的是一个连续的有圆的区间. 奇怪的是我没有离散化,样例都没有过,却把题给A了 ...