bzoj 1036 Tree Count
题目大意:给出一棵树,每个点有一个权值,要求三种操作:1.修改某个点的权值,2.询问x到y路径上各点的权值最大值,3.询问x到y路径上各点的权值之和。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 30010
#define ls o<<1
#define rs o<<1|1
#define define_m int m=(l+r)>>1 int first[N] , k;
struct Edge{
int y , next;
}e[N<<]; void add_edge(int x , int y)
{
e[k].y = y , e[k].next = first[x];
first[x] = k++;
} int sz[N] , dep[N] , fa[N] , son[N] , top[N] , id[N] , num;
void dfs(int u , int f , int d)
{
fa[u] = f , sz[u] = , dep[u]= d , son[u]=;
int mx = ;
for(int i=first[u] ; ~i ; i=e[i].next){
int v = e[i].y;
if(v == f) continue;
dfs(v , u , d+);
sz[u]+=sz[v];
if(sz[v]>mx) mx=sz[v] , son[u]=v;
}
} void dfs1(int u , int f , int head)
{
top[u]=head , id[u]=++num;
if(son[u]) dfs1(son[u] , u , head);
for(int i=first[u] ; ~i ; i=e[i].next){
int v = e[i].y;
if(v == f || v == son[u]) continue;
dfs1(v , u , v);
}
} int sum[N<<] , mx[N<<] , val[N];
void push_up(int o)
{
sum[o] = sum[ls]+sum[rs];
mx[o] = max(mx[ls] , mx[rs]);
} void build(int o , int l , int r)
{
if(l==r){
sum[o]=mx[o]=val[l];
return ;
}
define_m;
build(ls , l , m);
build(rs , m+ , r);
push_up(o);
} void update(int o , int l , int r , int p , int v)
{
if(l==r){
sum[o]=mx[o]=v;
return;
}
define_m;
if(m>=p) update(ls , l , m , p , v);
else update(rs , m+ , r , p , v);
push_up(o);
} int qSum(int o , int l , int r , int s , int t)
{
if(l>=s && r<=t) return sum[o];
define_m;
int res = ;
if(m>=s) res+=qSum(ls , l , m , s , t);
if(m<t) res+=qSum(rs , m+ , r , s , t);
return res;
} int qMax(int o , int l , int r , int s , int t)
{
if(l>=s && r<=t) return mx[o];
define_m;
int res = -1e9;
if(m>=s) res = max(res , qMax(ls , l , m , s , t));
if(m<t) res = max(res , qMax(rs , m+ , r , s , t));
return res;
} int calPath(int u , int v , int flag)
{
int top1 = top[u] , top2 = top[v] , ret=-1e9;
if(flag) ret=;
while(top1!=top2){
if(dep[top1]<dep[top2]){
swap(top1 , top2);
swap(u , v);
}
if(flag) ret+=qSum( , , num , id[top1] , id[u]);
else ret=max(ret , qMax(,,num,id[top1] , id[u]));
u = fa[top1];
top1 = top[u];
}
if(dep[u]<dep[v]) swap(u,v);
if(flag) ret+=qSum(,,num,id[v],id[u]);
else ret=max(ret , qMax(,,num,id[v],id[u]));
return ret;
}
char str[];
int main()
{
//freopen("in.txt" , "r" , stdin);
int n , x , y , q;
while(scanf("%d" , &n)!=EOF)
{
memset(first , - , sizeof(first));
k = ;
for(int i= ; i<n ; i++){
scanf("%d%d" , &x , &y);
add_edge(x , y);
add_edge(y , x);
}
dfs( , , );
num = ;
dfs1( , , );
int tmp[N];
for(int i= ; i<=n ; i++) scanf("%d" , val+i) , tmp[i]=val[i];
for(int i= ; i<=n ; i++) val[id[i]]=tmp[i];
build( , , num);
// for(int i=1 ; i<=7 ; i++) cout<<i<<" "<<mx[i]<<" "<<sum[i]<<endl;
scanf("%d" , &q);
while(q--){
scanf("%s%d%d", str , &x , &y);
if(str[]=='C'){
update( , , num , id[x] , y);
}
else if(str[]=='M'){
int ret = calPath(x , y , );
printf("%d\n" , ret);
}else{
int ret = calPath(x , y , );
printf("%d\n" , ret);
}
}
}
return ;
}
bzoj 1036 Tree Count的更多相关文章
- [BZOJ 1036] [ZJOI2008] 树的统计Count 【Link Cut Tree】
题目链接:BZOJ - 1036 题目分析 这道题可以用树链剖分,块状树等多种方法解决,也可以使用 LCT. 修改某个点的值时,先将它 Splay 到它所在的 Splay 的根,然后修改它的值,再将它 ...
- BZOJ.1036 [ZJOI2008]树的统计Count ( 点权树链剖分 线段树维护和与最值)
BZOJ.1036 [ZJOI2008]树的统计Count (树链剖分 线段树维护和与最值) 题意分析 (题目图片来自于 这里) 第一道树链剖分的题目,谈一下自己的理解. 树链剖分能解决的问题是,题目 ...
- [Tree]Count Complete Tree Nodes
Total Accepted: 22338 Total Submissions: 97234 Difficulty: Medium Given a complete binary tree, coun ...
- bzoj 2212 Tree Rotations
bzoj 2212 Tree Rotations 考虑一个子树 \(x\) 的左右儿子分别为 \(ls,rs\) .那么子树 \(x\) 内的逆序对数就是 \(ls\) 内的逆序对数,\(rs\) 内 ...
- bzoj 1036 [ZJOI2008]树的统计Count(树链剖分,线段树)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 10677 Solved: 4313[Submit ...
- Bzoj 1036: [ZJOI2008]树的统计Count 树链剖分,LCT
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 11102 Solved: 4490[Submit ...
- BZOJ 1036: [ZJOI2008]树的统计Count( 树链剖分 )
树链剖分... 不知道为什么跑这么慢 = = 调了一节课啊跪.. ------------------------------------------------------------------- ...
- BZOJ 1036: [ZJOI2008]树的统计Count (树链剖分模板题)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 14982 Solved: 6081[Submit ...
- AC日记——[ZJOI2008]树的统计Count bzoj 1036
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 15007 Solved: 6092[Submit ...
随机推荐
- 【CDN】海外免费加速CDN:Incapsula,CloudFare
最近服务器要搬迁到香港,因为后续有国外用户使用,基于此要使用大陆和海外都比较好的cdn才好 一开始国外同事推荐CloudFare,后来看看效果开始使用Incapsula CloudFare 官网:ht ...
- MYSQL 编码方式 ------导入 .sql 文件 报编码错误
在做计量泵上位机时,利用MYSQL存储数据,建表hisruninfo(计量泵历史运行数据表). 表格式为utf8,查过数据库 格式 同样为utf8 导入该hisruninfo.sql文件时,并没有报错 ...
- commonJS — 函数操作(for Function)
for Function github: https://github.com/laixiangran/commonJS/blob/master/src/forFunction.js 代码 /** * ...
- 深入理解JVM虚拟机-2垃圾收集器
这里讨论的收集器基于JDK 1.7 Update 14之后的HotSpot虚拟机. 如果两个收集器之间存在连线,说明可以搭配使用.虚拟机所处的区域,则表示它是属于新生代收集器还是年老代收集器.在这里我 ...
- js生成验证码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- WebViewJavascriptBridge-Obj-C和JavaScript互通消息的桥梁
转载至:http://www.cocoachina.com/ios/20150629/12248.html 译者:@coderyi9 本文翻译自Marcus Westin的开源框架WebViewJav ...
- python 练习 27
ython continue 语句跳出本次循环,而break跳出整个循环. continue 语句用来告诉Python跳过当前循环的剩余语句,然后继续进行下一轮循环. continue语句用在whil ...
- [Hadoop 周边] 浅谈大数据(hadoop)和移动开发(Android、IOS)开发前景【转】
原文链接:http://www.d1net.com/bigdata/news/345893.html 先简单的做个自我介绍,我是云6期的,黑马相比其它培训机构的好偶就不在这里说,想比大家都比我清楚: ...
- 《javascript高级程序设计》第七章 递归recursion
7.1 递归7.2 闭包 7.2.1 闭包与变量 7.2.2 关于this 对象 7.2.3 内存泄漏 7.3 模仿块级作用域7.4 私有变量 7.4.1 静态私有变量 7.4.2 模块模式 7.4. ...
- python list对象
list对象 1.list定义l=['first','second'] 2.list追加对象list.append('aa');append的方法总是把元素追加到末尾 insert(索引号,'项目') ...