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 ...
随机推荐
- 转:程序员最值得关注的10个C开源项目
程序员最值得关注的10个C开源项目 1. Webbench Webbench 是一个在 linux 下使用的非常简单的网站压测工具.它使用 fork ()模拟多个客户端同时访问我们设定的 URL,测试 ...
- OpenGL的GLUT事件处理(Event Processing)窗口管理(Window Management)函数[转]
GLUT事件处理(Event Processing)窗口管理(Window Management)函数 void glutMainLoop(void) 让glut程序进入事件循环.在一个glut程序中 ...
- AngularJS的directive(指令)配置选项说明
js代码如下: var appModule = angular.module("appModule", []); appModule.controller("Ctrl&q ...
- android 属性
RelativeLayout 第一类:属性值为true可false android:layout_centerHrizontal 水平居中 android:layout_centerVe ...
- [转]Android 使用Fragment界面向下跳转并一级级返回
1.首先贴上项目结构图: 2.先添加一个接口文件BackHandledInterface.java,定义一个setSelectedFragment方法用于设置当前加载的Fragment在栈顶,主界 ...
- [你必须知道的.NET] 第八回:品味类型---值类型与引用类型(上)-内存有理
原文地址:http://kb.cnblogs.com/page/42318/ 系列文章导航: [你必须知道的.NET] 开篇有益 [你必须知道的.NET] 第一回:恩怨情仇:is和as [你必须知道的 ...
- angular 零碎知识
各种服务: $location:可以监听事件的改变 link 在没有设置template的情况下,指令作为标签使用的时候,ele是指令(伪数组的形式); * 如果指令作为属性使用的话,ele是使用该指 ...
- Django开发博客 入门篇
Django是神马? Django是一个开源免费的Web框架,使用Python编写.能够让你快速写出一个Web应用, 因为它包含了绝大部分的组件,比如认证,表单,ORM,Session,安全,文件上传 ...
- arcgis API for javascript 学习笔记
ArcGis Server地图是缓存的,意味着它有服务器管理员简历提示性能的一组预先渲染的切片.由于这个原因地图通过ArcGISTiledMapServiceLayer表示 如果地图服务没有一个可用的 ...
- 获取txt文件指定行内容
#!/usr/bin/python num=0; ni=open("C:\Python34\ceshi.txt") for line in ni: num=num+1; #表示行 ...