HYSBZ1036-树链剖分-点权
树链剖分,点权,单点更改,路径查询。学树链剖分下面这个博文不错
http://blog.csdn.net/y990041769/article/details/40348013
线段树必须写的很熟练才行。注意负数
#include <cstdio>
#include <vector>
#include <algorithm> using namespace std; const int maxn = 3e4+;
const int INF = 0x3f3f3f3f; /////////////////////////////////////
int topw;
int son[maxn],top[maxn],fa[maxn],siz[maxn],id[maxn],dep[maxn];
int val[maxn],pre_val[maxn]; vector<int> G[maxn]; void dfs_1(int u,int f,int d)
{
son[u] = ;
dep[u] = d;
fa[u] = f;
siz[u] = ;
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
if(v == f) continue;
dfs_1(v,u,d+);
siz[u] += siz[v];
if(siz[son[u]] < siz[v])
{
son[u] = v;
}
}
} void dfs_2(int u,int tp)
{
top[u] = tp;
id[u] = ++topw;
if(son[u]) dfs_2(son[u],tp);
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
if(v == fa[u] || v == son[u]) continue;
dfs_2(v,v);
}
} ////////////////////////////////////
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define root 1,topw,1 int sum[maxn<<];
int mx[maxn<<]; void push_up(int rt)
{
sum[rt] = sum[rt<<]+sum[rt<<|];
mx[rt] = max(mx[rt<<],mx[rt<<|]);
} void build(int l,int r,int rt)
{
sum[rt] = ;
mx[rt] = ;
if(l == r)
{
sum[rt] = val[l];
mx[rt] = val[l];
return ;
}
int m = (l+r)>>;
build(lson);
build(rson);
push_up(rt);
} void update(int pos,int add,int l,int r,int rt)
{
if(l == r)
{
sum[rt] = add;
mx[rt] = add;
return ;
}
int m = (l+r)>>;
if(pos <= m) update(pos,add,lson);
else update(pos,add,rson);
push_up(rt);
} int query_sum(int L,int R,int l,int r,int rt)
{
if(L <= l && R >= r)
{
return sum[rt];
}
int m = (l+r)>> , ans = ;
if(L <= m) ans += query_sum(L,R,lson);
if(R > m) ans += query_sum(L,R,rson);
return ans;
} int query_max(int L,int R,int l,int r,int rt)
{
if(L <= l && R >= r)
{
//printf("[%d,%d] rt:%d mx:%d\n",l,r,rt,mx[rt]);
return mx[rt];
}
int m = (l+r)>> , ans = -INF;
if(L <= m) ans = max(ans,query_max(L,R,lson));
if(R > m) ans = max(ans,query_max(L,R,rson));
return ans;
} int Find_sum(int u,int v)
{
int fu = top[u],fv = top[v];
int ans = ;
while(fu != fv)
{
//printf("ans:%d %d %d %d %d\n",ans,u,fu,v,fv);
if(dep[fu] < dep[fv])
{
swap(u,v);
swap(fu,fv);
}
ans += query_sum(id[fu],id[u],root);
u = fa[fu];
fu = top[u];
}
if(u == v)
{
return ans+query_sum(id[u],id[v],root);
}
else{
if(dep[u] < dep[v]) swap(u,v);
return ans+query_sum(id[v],id[u],root);
}
} int Find_max(int u,int v)
{
int fu = top[u],fv = top[v];
int ans = -INF;
while(fu != fv)
{
if(dep[fu] < dep[fv])
{
swap(u,v);
swap(fu,fv);
}
//printf("ans:%d %d %d %d %d\n",ans,u,fu,v,fv);
//printf("q:%d\n",query_max(id[fu],id[u],root));
ans = max(ans,query_max(id[fu],id[u],root));
u = fa[fu];
fu = top[u];
}
if(u == v)
{
return max(ans,query_max(id[u],id[v],root));
}
else{
if(dep[u] < dep[v]) swap(u,v);
return max(ans,query_max(id[v],id[u],root));
}
} ////////////////////////////////////////// int N,Q;
int main()
{
freopen("input.in","r",stdin);
while(~scanf("%d",&N))
{
for(int i=,u,v;i<N;i++)
{
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
for(int i=;i<=N;i++) scanf("%d",&pre_val[i]);
topw = ;
dfs_1(,,);
dfs_2(,);
for(int i=;i<=N;i++) val[id[i]] = pre_val[i];
build(root); scanf("%d",&Q);
char op[];
int a,b;
while(Q--)
{
scanf("%s%d%d",op,&a,&b);
if(op[] == 'Q')
{
if(op[] == 'M')
{
printf("%d\n",Find_max(a,b));
}else
{
printf("%d\n",Find_sum(a,b));
}
}else
{
update(id[a],b,root);
}
}
for(int i=;i<=N;i++) G[i].clear();
}
}
HYSBZ1036-树链剖分-点权的更多相关文章
- BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分 - 点权剖分 - 单点权修改)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 树链剖分模版题,打的时候注意点就行.做这题的时候,真的傻了,单词拼错检查了一个多小时 ...
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
- POJ3237 Tree 树链剖分 边权
POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...
- POJ2763 Housewife Wind 树链剖分 边权
POJ2763 Housewife Wind 树链剖分 边权 传送门:http://poj.org/problem?id=2763 题意: n个点的,n-1条边,有边权 修改单边边权 询问 输出 当前 ...
- HDU3669 Aragorn's Story 树链剖分 点权
HDU3669 Aragorn's Story 树链剖分 点权 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: n个点的,m条边,每个点都 ...
- BZOJ 1984: 月下“毛景树” [树链剖分 边权]
1984: 月下“毛景树” Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1728 Solved: 531[Submit][Status][Discu ...
- SPOJ 375 (树链剖分 - 边权剖分 - 修改单边权)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28982#problem/I 给你一棵有边权的树,有两个操作:一个操作是输出l到 ...
- 树链剖分边权模板spoj375
树链剖分是树分解成多条链来解决树上两点之间的路径上的问题 如何求出树链:第一次dfs求出树上每个结点的大小和深度和最大的儿子,第二次dfs就能将最大的儿子串起来并hash(映射)到线段树上(或者其他数 ...
- 洛谷 P3384 【模板】树链剖分-树链剖分(点权)(路径节点更新、路径求和、子树节点更新、子树求和)模板-备注结合一下以前写的题目,懒得写很详细的注释
P3384 [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节 ...
- POJ 3237.Tree -树链剖分(边权)(边值更新、路径边权最值、区间标记)贴个板子备忘
Tree Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 12247 Accepted: 3151 Descriptio ...
随机推荐
- Wechart 饼图
预览 Preview | Usage Source | Pie Source | Tutorial Wechart by Cax Cax 众所周知 Cax 既能开发游戏.又能开发图表.本文将从饼图开始 ...
- UWP 自定义控件:了解模板化控件 系列文章
UWP自定义控件的入门文章 [UWP 自定义控件]了解模板化控件(1):基础知识 [UWP 自定义控件]了解模板化控件(2):模仿ContentControl [UWP 自定义控件]了解模板化控件(2 ...
- H5 36-背景定位属性
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- H5 marquee标签
39-marquee标签 内容 属性: direction: 设置滚动方向 left/right/up/down scrollamount: 设置滚动速度, 值越大就越快 loop: 设置滚动次数, ...
- fun = [lambda x: x*i for i in range(4)] 本质解析/原理,LEGB规则 闭包原理
命名空间,闭包原理,参考点击本文 一.问题描述 fun = [lambda x: x*i for i in range(4)] for item in fun: print(item(1)) 上述式子 ...
- ansible jenkins war
Ansible is Simple IT Automationhttps://www.ansible.com/ Ansible中文权威指南- 国内最专业的Ansible中文官方学习手册http://a ...
- swagger 指定字段不显示到文档里
Swagger UI 隐藏指定接口类或方法 - 宁静致远 - CSDN博客https://blog.csdn.net/lqh4188/article/details/53538201 swagger ...
- C#复习笔记(5)--C#5:简化的异步编程(异步编程的基础知识)
异步编程的基础知识 C#5推出的async和await关键字使异步编程从表面上来说变得简单了许多,我们只需要了解不多的知识就可以编写出有效的异步代码. 在介绍async和await之前,先介绍一些基础 ...
- 4 HttpServletResponse 与 HttpServletRequest
Web 服务器收到一个http请求,会针对每个请求创建一个HttpServletRequest 和 HttpServletReponse 对象,response用于向客户端发送数据,request用于 ...
- [转帖]Linux 的静态库与动态库
Linux下的静态库与动态库 2017年02月18日 09:17:13 LLZK_ 阅读数:10257 标签: linux动态库静态库区别使用 更多 个人分类: Linux学习笔记 所属专栏: Lin ...