HYSBZ 1036 树的统计Count(树链剖分)题解
思路:
树链剖分,不知道说什么...我连模板都不会用
代码:
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define ll long long
const int maxn = 60000+5;
const int INF = 0x3f3f3f3f;
using namespace std;
int n,m,tol;
int cnt;
struct Node{
int u,v,next;
}node[maxn << 1];
int head[maxn];
int fa[maxn],siz[maxn],dep[maxn],son[maxn],id[maxn],top[maxn];
//父节点,子树大小,深度,重儿子,dfs执行顺序,重链的起始点
void addnode(int u,int v){
node[tol].u = u;
node[tol].v = v;
node[tol].next = head[u];
head[u] = tol++;
}
void dfs1(int u,int father,int depth){ //确定fa,siz,dep,son
son[u] = 0;
dep[u] = depth;
fa[u] = father;
siz[u] = 1;
for(int i = head[u];~i;i = node[i].next){
int v = node[i].v;
if(v == father) continue; //储存无向边,需判断
dfs1(v,u,depth + 1);
siz[u] += siz[v];
if(siz[v] > siz[son[u]]) //更新重儿子
son[u] = v;
}
}
void dfs2(int u,int tp){ //按dfs执行顺序编号
top[u] = tp; //重链的起始点
id[u] = ++cnt; //编号
if(!son[u]) return;
//有重儿子,是重链,继续往下
dfs2(son[u],tp);
for(int i = head[u];~i;i = node[i].next){
int v = node[i].v;
if(v == fa[u] || v == son[u]) continue;
dfs2(v,v);
}
}
/*
线段树
*/
int sum[maxn<<2],mx[maxn<<2];
void push_up(int rt){
sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
mx[rt] = max(mx[rt << 1],mx[rt << 1 | 1]);
}
/*void build(int l,int r,int rt){
sum[rt] = 0;
mx[rt] = -INF; //注意
if(l == r){
sum[rt] = mx[rt] = val[l];
return;
}
int m = (l + r) >> 1;
build(l,m,rt << 1);
build(m + 1,r,rt << 1 | 1);
push_up(rt);
}*/
void update(int pos,int val,int l,int r,int rt){
if(l == r){
sum[rt] = mx[rt] = val;
return;
}
int m = (l + r) >> 1;
if(pos <= m)
update(pos,val,l,m,rt << 1);
else
update(pos,val,m + 1,r,rt << 1 | 1);
push_up(rt);
}
int queryS(int L,int R,int l,int r,int rt){
if(L <= l && R >= r){
return sum[rt];
}
int m = (l + r) >> 1;
int ans = 0;
if(L <= m)
ans += queryS(L,R,l,m,rt << 1);
if(R > m)
ans += queryS(L,R,m + 1,r,rt << 1 | 1);
return ans;
}
int queryM(int L,int R,int l,int r,int rt){
if(L <= l && R >= r){
return mx[rt];
}
int m = (l + r) >> 1;
int ans = -INF;
if(L <= m)
ans = max(ans,queryM(L,R,l,m,rt << 1));
if(R > m)
ans = max(ans,queryM(L,R,m + 1,r,rt << 1 | 1));
return ans;
}
//////////////////////////////////////////
int FindS(int u,int v){
int ans = 0;
while(top[u] != top[v]){ //不在同一条链上,就从u所在转移到v所在
if(dep[top[u]] < dep[top[v]]){
swap(u,v);
}
ans += queryS(id[top[u]],id[u],1,n,1);
u = fa[top[u]];
}
if(u == v){
return (ans + queryS(id[u],id[v],1,n,1));
}
else{
if(dep[u] > dep[v]){
swap(u,v);
}
return (ans + queryS(id[u],id[v],1,n,1));
}
}
int FindM(int u,int v){
int ans = -INF;
while(top[u] != top[v]){ //不在同一条链上,就从u所在转移到v所在
if(dep[top[u]] < dep[top[v]]){
swap(u,v);
}
ans = max(ans,queryM(id[top[u]],id[u],1,n,1));
u = fa[top[u]];
}
if(u == v){
return max(ans,queryM(id[u],id[v],1,n,1));
}
else{
if(dep[u] > dep[v]){
swap(u,v);
}
return max(ans,queryM(id[u],id[v],1,n,1));
}
}
///////////////////////////////////////////////////
void init() {
tol = cnt = 0;
memset(fa,0,sizeof fa);
memset(siz,0,sizeof siz);
memset(son,0,sizeof son);
memset(dep,0,sizeof dep);
memset(top,0,sizeof top);
memset(id,0,sizeof id);
memset(sum,0,sizeof sum);
memset(node,0,sizeof node);
memset(mx,0,sizeof mx);
memset(head,-1,sizeof head);
}
int main(){
int q;
while(~scanf("%d",&n)){
int u,v;
init();
for(int i = 1;i < n;i++){
scanf("%d%d",&u,&v);
addnode(u,v);
addnode(v,u);
}
dfs1(1,0,1);
dfs2(1,1);
for(int i = 1;i <= n;i++){
int w;
scanf("%d",&w);
update(id[i],w,1,n,1);
}
scanf("%d",&q);
char s[20];
while(q--){
scanf("%s%d%d",s,&u,&v);
if(s[0] == 'C'){
update(id[u],v,1,n,1);
}
else if(s[1] == 'S'){
printf("%d\n",FindS(u,v));
}
else{
printf("%d\n",FindM(u,v));
}
}
}
return 0;
}
J
HYSBZ 1036 树的统计Count(树链剖分)题解的更多相关文章
- BZOJ 1036: [ZJOI2008]树的统计Count [树链剖分]【学习笔记】
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 14302 Solved: 5779[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: 16294 Solved: 6645[Submit ...
- BZOJ 1036: [ZJOI2008]树的统计Count (树链剖分模板题)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 14982 Solved: 6081[Submit ...
- BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分)(线段树单点修改)
[ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 14968 Solved: 6079[Submit][Stat ...
- 【BZOJ1036】[ZJOI2008]树的统计Count 树链剖分
[BZOJ1036][ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. ...
- bzoj1036 [ZJOI2008]树的统计Count 树链剖分模板题
[ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成 一些操作: I. CHANGE u ...
- Cogs 1688. [ZJOI2008]树的统计Count(树链剖分+线段树||LCT)
[ZJOI2008]树的统计Count ★★★ 输入文件:bzoj_1036.in 输出文件:bzoj_1036.out 简单对比 时间限制:5 s 内存限制:162 MB [题目描述] 一棵树上有n ...
- BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分 - 点权剖分 - 单点权修改)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 树链剖分模版题,打的时候注意点就行.做这题的时候,真的傻了,单词拼错检查了一个多小时 ...
随机推荐
- windows下的mysql迁移到linux下
最近做毕业设计,需要把windows下的mysql移植到linux下 曾经有过在window下移植mysql数据库的经验,只需要把msql的数据文件复制到另一台安装mysql的机器的数据存放位置,然后 ...
- nodejs 环境搭建
一 下载nodejs 官网:http://nodejs.cn/ 有时官网有点慢,可以去其他地方下载 统一下载站:http://www.3987.com/xiazai/2/43/57188.html 二 ...
- 关于wcf三大工具的使用(wsdl.exe svcutil.exe disco.exe)
首先,我们必须创建一个wcf服务.并部署到IIS中.这里我已经将一个StudentService服务部署到我自己的电脑了. (1)svcutil.exe svcutil.exe工具的作用是通过服务地址 ...
- linux :故障提示:Error:No suitable device found: no device found for connection "System eth0"
解决:1./etc/udev/rules.d/70-persistent-net.rules 中的mac地址2.网卡配置文件ifcfg-eth0中的mac地址3.ifconfig中的mac地址以上三个 ...
- LayoutSimple简易响应式CSS布局框架
开发这个css布局的目的是为了少做一些重复的工作,一是前端或多或少会开发一些很小的响应式项目, 二是UI设计的出来的界面总是各种布局各种样式,这个时候如果前端去使用Bootstrap或者Foundat ...
- JS原生ajax
原文链接:http://caibaojian.com/ajax-jsonp.html 一.JS原生ajax ajax:一种请求数据的方式,不需要刷新整个页面: ajax的技术核心是 XMLHttpRe ...
- 【Loadrunner】性能测试:通过服务器日志获取性能需求
性能测试:通过服务器日志获取性能需求 接触过性能测试的童鞋都知道,想要做好一个项目的性能测试,性能需求的获取至关重要~!如果公司有做过性能测试还好,大家可以拿之前的性能测试数据作为参 ...
- centos LAMP第三部分php,mysql配置 php配置文件 配置php的error_log 配置php的open_basedir 安装php的扩展模块 phpize mysql配置第二十一节课
centos LAMP第三部分php,mysql配置 php配置文件 配置php的error_log 配置php的open_basedir 安装php的扩展模块 phpize mysql配 ...
- Django REST Framework简单入门(一)
Django REST Framework(简称DRF),是一个用于构建Web API的强大且灵活的工具包. REST这个词,是Roy Thomas Fielding在他2000年的博士论文中提出的. ...
- 谷歌百度以图搜图 "感知哈希算法" C#简单实现
/// <summary> /// 感知哈希算法 /// </summary> public class ImageComparer { /// <summary> ...