题目链接:戳我

树链剖分。

注意一点就是维护最大值的时候最好写成下面代码里那个样子,要不然会因为可能左右区间没有的问题有奇奇怪怪的锅。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 100010
using namespace std;
int n,m,t,tot;
int a[MAXN],wt[MAXN],head[MAXN],sum[MAXN],maxx[MAXN];
int son[MAXN],siz[MAXN],fa[MAXN],id[MAXN],dep[MAXN],top[MAXN];
char cur[10];
struct Edge{int nxt,to;}edge[MAXN<<1];
inline void add(int from,int to){edge[++t].nxt=head[from];edge[t].to=to;head[from]=t;}
inline void dfs1(int now,int pre)
{
siz[now]=1;
fa[now]=pre;
dep[now]=dep[pre]+1;
int maxx=-1;
for(int i=head[now];i;i=edge[i].nxt)
{
int v=edge[i].to;
if(v==pre) continue;
dfs1(v,now);
siz[now]+=siz[v];
if(siz[v]>maxx) maxx=siz[v],son[now]=v;
}
}
inline void dfs2(int now,int topf)
{
id[now]=++tot;
top[now]=topf;
wt[tot]=a[now];
if(son[now]) dfs2(son[now],topf);
for(int i=head[now];i;i=edge[i].nxt)
{
int v=edge[i].to;
if(v==fa[now]||v==son[now]) continue;
dfs2(v,v);
}
}
inline int ls(int x){return x<<1;}
inline int rs(int x){return x<<1|1;}
inline void push_up(int x)
{
sum[x]=sum[ls(x)]+sum[rs(x)];
maxx[x]=max(maxx[ls(x)],maxx[rs(x)]);
}
inline void build(int x,int l,int r)
{
if(l==r) {sum[x]=wt[l],maxx[x]=wt[l];return;}
int mid=(l+r)>>1;
build(ls(x),l,mid);
build(rs(x),mid+1,r);
push_up(x);
}
inline void update(int x,int l,int r,int pos,int k)
{
if(l==r) {sum[x]=k,maxx[x]=k;return;}
int mid=(l+r)>>1;
if(pos<=mid) update(ls(x),l,mid,pos,k);
else update(rs(x),mid+1,r,pos,k);
push_up(x);
}
inline int query_sum(int x,int l,int r,int ll,int rr)
{
if(ll<=l&&r<=rr) return sum[x];
int mid=(l+r)>>1;
int cur_ans=0;
if(ll<=mid) cur_ans+=query_sum(ls(x),l,mid,ll,rr);
if(mid<rr) cur_ans+=query_sum(rs(x),mid+1,r,ll,rr);
return cur_ans;
}
inline int query_max(int x,int l,int r,int ll,int rr)
{
if(ll<=l&&r<=rr) return maxx[x];
int mid=(l+r)>>1;
int ans=-2147483647;
if(ll<=mid) ans=max(ans,query_max(ls(x),l,mid,ll,rr));
if(mid<rr) ans=max(ans,query_max(rs(x),mid+1,r,ll,rr));
return ans;
}
inline int solve_max(int x,int y)
{
int maxx=(int)-1e9;
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
maxx=max(maxx,query_max(1,1,n,id[top[x]],id[x]));
x=fa[top[x]];
}
if(dep[x]<dep[y]) swap(x,y);
maxx=max(maxx,query_max(1,1,n,id[y],id[x]));
return maxx;
}
inline int solve_sum(int x,int y)
{
int cur_ans=0;
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
cur_ans+=query_sum(1,1,n,id[top[x]],id[x]);
x=fa[top[x]];
}
if(dep[x]<dep[y]) swap(x,y);
cur_ans+=query_sum(1,1,n,id[y],id[x]);
return cur_ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
scanf("%d",&n);
for(int i=1;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v),add(v,u);
}
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
dfs1(1,1);
dfs2(1,1);
build(1,1,n);
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%s%d%d",cur,&x,&y);
if(cur[1]=='M') printf("%d\n",solve_max(x,y));
else if(cur[1]=='S') printf("%d\n",solve_sum(x,y));
else update(1,1,n,id[x],y);
}
return 0;
}

[ZJOI2008] 树的统计Count的更多相关文章

  1. BZOJ 1036: [ZJOI2008]树的统计Count [树链剖分]【学习笔记】

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 14302  Solved: 5779[Submit ...

  2. bzoj1036 [ZJOI2008]树的统计Count

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 12646  Solved: 5085 [Subm ...

  3. BZOJ 1036: [ZJOI2008]树的统计Count

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 14354  Solved: 5802 [Subm ...

  4. 【BZOJ1036】[ZJOI2008]树的统计Count 树链剖分

    [BZOJ1036][ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. ...

  5. bzoj 1036 [ZJOI2008]树的统计Count(树链剖分,线段树)

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 10677  Solved: 4313[Submit ...

  6. Bzoj 1036: [ZJOI2008]树的统计Count 树链剖分,LCT

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 11102  Solved: 4490[Submit ...

  7. 数据结构(LCT动态树):BZOJ 1036: [ZJOI2008]树的统计Count

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 12266  Solved: 4945[Submit ...

  8. BZOJ 1036: [ZJOI2008]树的统计Count( 树链剖分 )

    树链剖分... 不知道为什么跑这么慢 = = 调了一节课啊跪.. ------------------------------------------------------------------- ...

  9. 1036: [ZJOI2008]树的统计Count

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 7496  Solved: 3078[Submit] ...

  10. bzoj1036 [ZJOI2008]树的统计Count 树链剖分模板题

    [ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成 一些操作: I. CHANGE u ...

随机推荐

  1. hibernate 错误 could not determine type for

    今天配置实体类注解时,出现以下错误: org.hibernate.MappingException: Could not determine type for: com.oneToOne.IdCard ...

  2. 【poj1743】Musical Theme 【后缀自动机】

    题意 给出一个n个数字的序列,找出相同变化趋势且不重叠的两个最长子串. 分析 这个题以前应该用后缀数组+二分做过.学了后缀自动机后可以用后缀自动机搞一下. 先差分,然后把查分后的数组建SAM.然后对于 ...

  3. 671. Second Minimum Node In a Binary Tree 非递减二叉树中第二小的元素

    [抄题]: Given a non-empty special binary tree consisting of nodes with the non-negative value, where e ...

  4. linux系统/proc/stat信息与top的cup信息的联系及区别

    一. /proc 目录 Linux系统上的/proc目录是一种文件系统,即proc文件系统,与其它常见的文件系统不同的是,/proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以文 ...

  5. Paradox

    克己博伦 当一个无法阻挡的力量,碰到了一个无法移动的物体?如果这个力量移动了物体,那么这个物体就不是无法移动的.如果这个力量没有移动物体,那么这个无法阻挡的力量就被挡了下来. 上帝能造出一个重到他自己 ...

  6. RotbotFrameWork接口测试

    一.添加接口测试Library 二.参数说明 三.关键字使用说明 Set Varibae: 参数化接口地址 Connect To Databse Using Custom Param: 连接数据库(需 ...

  7. meshconverters

    https://github.com/RobotLocomotion/meshConverters meshconverters $ mkdir build && cd build $ ...

  8. Python自然语言处理工具NLTK的安装FAQ

    1 下载Python 首先去python的主页下载一个python版本http://www.python.org/,一路next下去,安装完毕即可 2 下载nltk包 下载地址:http://www. ...

  9. Linux Basic学习笔记01

    介绍课程: 中级: 初级:系统基础 中级:系统管理.服务安全及服务管理.Shell脚本: 高级: MySQL数据库: cache & storage 集群: Cluster lb: 4laye ...

  10. 2 Python之编程语言介绍及变量

    一: 编程语言介绍 1.机器语言 直接用二进制编程,直接控制硬件,需要掌握硬件的操作细节 优点:执行效率高 缺点:开发效率低 2 汇编语言: 用英文标签取代二级制指令去编写程序,直接控制硬件,需要掌握 ...