【BZOJ1036】[ZJOI2008] 树的统计Count(一道可怕的模板题:树剖+线段树)
题解
这真的只是一道模板题:一个树链剖分套上一个线段树(令我窒息的组合)。
既然是模板题,那就直接上代码吧。
代码
#include<bits/stdc++.h>
#define N 30000
using namespace std;
int n,ee=0,tot=0,a[N+5],lnk[N+5],fa[N+5],Size[N+5],Depth[N+5],Wson[N+5],Top[N+5],Pos[N+5],Num[N+5];
int Sum[N<<2],Max[N<<2];
struct edge
{
int to,nxt;
}e[2*N+5];
inline char tc()
{
static char ff[100000],*A=ff,*B=ff;
return A==B&&(B=(A=ff)+fread(ff,1,100000,stdin),A==B)?EOF:*A++;
}
inline void read(int &x)
{
x=0;int f=1;char ch;
while(!isdigit(ch=tc())) if(ch=='-') f=-1;
while(x=(x<<3)+(x<<1)+ch-'0',isdigit(ch=tc()));
x*=f;
}
inline void write(int x)
{
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
inline void read_string(string &st)
{
st="";char ch;
while((ch=tc())<'A'||ch>'Z');
while(st+=ch,(ch=tc())>='A'&&ch<='Z');
}
inline void add(int x,int y)
{
e[++ee]=(edge){y,lnk[x]},lnk[x]=ee;
}
inline void dfs1(int x)//第一遍DFS,预处理出每个节点的父亲、深度、重儿子以及子树大小
{
register int i;Size[x]=1;
for(i=lnk[x];i;i=e[i].nxt)
if(e[i].to^fa[x]) fa[e[i].to]=x,Depth[e[i].to]=Depth[x]+1,dfs1(e[i].to),Size[x]+=Size[e[i].to],(Size[e[i].to]>Size[Wson[x]]?Wson[x]=e[i].to:0);
}
inline void dfs2(int x,int tp)//第二遍DFS,根据先前预处理出的重儿子,剖分出轻重链
{
register int i;Top[Num[Pos[x]=++tot]=x]=tp;
if(Wson[x]) dfs2(Wson[x],tp);
for(i=lnk[x];i;i=e[i].nxt)
if(e[i].to!=fa[x]&&e[i].to!=Wson[x]) dfs2(e[i].to,e[i].to);
}
inline void PushUp(int rt)
{
Sum[rt]=Sum[rt<<1]+Sum[rt<<1|1],Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);
}
inline void Build(int l,int r,int rt)//一个朴素的建树过程
{
if(l==r)
{
Sum[rt]=Max[rt]=a[Num[l]];
return;
}
int mid=l+r>>1;
Build(l,mid,rt<<1),Build(mid+1,r,rt<<1|1),PushUp(rt);
}
inline void Update(int l,int r,int rt,int x,int v)//单点修改
{
if(l==r)
{
Sum[rt]=Max[rt]=v;
return;
}
int mid=l+r>>1;
(mid>=x?Update(l,mid,rt<<1,x,v):Update(mid+1,r,rt<<1|1,x,v)),PushUp(rt);
}
inline int Query_Max(int l,int r,int rt,int L,int R)//查询区间最大值
{
if(L>R) L^=R,R^=L,L^=R;
if(L<=l&&r<=R) return Max[rt];
int mid=l+r>>1,res=-1e9;
if(L<=mid) res=max(res,Query_Max(l,mid,rt<<1,L,R));
if(R>mid) res=max(res,Query_Max(mid+1,r,rt<<1|1,L,R));
return res;
}
inline int Query_Sum(int l,int r,int rt,int L,int R)//查询区间和
{
if(L>R) L^=R,R^=L,L^=R;
if(L<=l&&r<=R) return Sum[rt];
int mid=l+r>>1,res=0;
if(L<=mid) res+=Query_Sum(l,mid,rt<<1,L,R);
if(R>mid) res+=Query_Sum(mid+1,r,rt<<1|1,L,R);
return res;
}
inline int Qmax(int s1,int s2)//对数据进行处理,并调用线段树的Query_Max()函数来求出答案
{
int res=-1e9;
while(Top[s1]^Top[s2])
{
if(Depth[Top[s1]]<Depth[Top[s2]]) s1^=s2,s2^=s1,s1^=s2;
res=max(res,Query_Max(1,n,1,Pos[Top[s1]],Pos[s1])),s1=fa[Top[s1]];
}
if(Depth[s1]<Depth[s2]) s1^=s2,s2^=s1,s1^=s2;
return max(res,Query_Max(1,n,1,Pos[s1],Pos[s2]));
}
inline int Qsum(int s1,int s2)//对数据进行处理,并调用线段树的Query_Sum()函数来求出答案
{
int res=0;
while(Top[s1]^Top[s2])
{
if(Depth[Top[s1]]<Depth[Top[s2]]) s1^=s2,s2^=s1,s1^=s2;
res+=Query_Sum(1,n,1,Pos[Top[s1]],Pos[s1]),s1=fa[Top[s1]];
}
if(Depth[s1]<Depth[s2]) s1^=s2,s2^=s1,s1^=s2;
return res+Query_Sum(1,n,1,Pos[s1],Pos[s2]);
}
int main()
{
freopen("a.in","r",stdin);
register int i;int x,y;
for(read(n),i=1;i<n;++i)
read(x),read(y),add(x,y),add(y,x);
for(i=1;i<=n;++i)
read(a[i]);
dfs1(1),dfs2(1,1),Build(1,n,1);
int Q;read(Q);
while(Q--)
{
string st;
read_string(st),read(x),read(y);
if(st=="CHANGE") Update(1,n,1,Pos[x],y);
else if(st=="QMAX") write(Qmax(x,y)),putchar('\n');
else if(st=="QSUM") write(Qsum(x,y)),putchar('\n');
}
return 0;
}
【BZOJ1036】[ZJOI2008] 树的统计Count(一道可怕的模板题:树剖+线段树)的更多相关文章
- [置顶] bzoj 1036 树的统计Count 点权值模板
树链剖分 点权型可做模板,链路剖分的思想把点hash到线段树的上,然后可通过n*(log(n)*log(n))的复杂度在树上操作,在线段树上能操作的在链路上都能操作. #include<cstd ...
- 【BZOJ1036】[ZJOI2008]树的统计Count 树链剖分
[BZOJ1036][ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. ...
- [BZOJ1036][ZJOI2008]树的统计Count 解题报告|树链剖分
树链剖分 简单来说就是数据结构在树上的应用.常用的为线段树splay等.(可现在splay还不会敲囧) 重链剖分: 将树上的边分成轻链和重链. 重边为每个节点到它子树最大的儿子的边,其余为轻边. 设( ...
- bzoj1036 [ZJOI2008]树的统计Count
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MB Submit: 12646 Solved: 5085 [Subm ...
- bzoj1036 [ZJOI2008]树的统计Count 树链剖分模板题
[ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成 一些操作: I. CHANGE u ...
- bzoj千题计划124:bzoj1036: [ZJOI2008]树的统计Count
http://www.lydsy.com/JudgeOnline/problem.php?id=1036 树链剖分板子题 #include<cstdio> #include<iost ...
- 【lct】bzoj1036 [ZJOI2008]树的统计Count
题意:给你一棵树,点带权,支持三种操作:单点修改:询问链上和:询问链上max. 这里的Query操作用了与上一题不太一样的做法(上一题用那种做法,因为在边带权的情况下换根太困难啦): 先ChangeR ...
- 题解 [ZJOI2008]树的统计Count
[ZJOI2008]树的统计Count Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u ...
- BZOJ 1036: [ZJOI2008]树的统计Count [树链剖分]【学习笔记】
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 14302 Solved: 5779[Submit ...
随机推荐
- 51nod1049(最大子段和2)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1049 题意:中文题诶- 思路:本题和51nod1049(题解 ...
- DNS 网关 路由 交换机 网桥 协议 服务器 这些都是什么关系?
来源:知乎 服务器:为你提供服务的机器.相当于马路边上的各种店面.虽然理论上任何一户人家都能开店为你提供服务,但是因为各种硬件资源限制而不适合开店.比如:小区道路比较窄(宽带带宽比较窄).家里地方太小 ...
- Jmeter用BeanShell Sampler调用java写的jar包进行MD5加密
[前言] 在工作中,有时候我们请求的参数可能需要加密,比如登录接口中的密码做了加密操作,今天我就给大家介绍一种方法:Jmeter用BeanShell Sampler调用java写的jar包进行MD5加 ...
- react native ios打包到真机,即生产包
参考文章:http://www.devio.org/2017/02/09/React-Native%E5%8F%91%E5%B8%83APP%E4%B9%8B%E6%89%93%E5%8C%85iOS ...
- 洛谷P4116 Qtree3
题目描述 给出\(N\)个点的一棵树(\(N-1\)条边),节点有白有黑,初始全为白 有两种操作: \(0\) \(i\) : 改变某点的颜色(原来是黑的变白,原来是白的变黑) \(1\) \(v\) ...
- centos7安装配置docker
1. 安装/升级Docker客户端 Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker . uname -r 从 ...
- iOS无线真机调试
打开xcode,选择Window > Devices and Simulators 用数据线连接设备 选择 Connect via network
- CentOS下ganglia监控部署
第一步:CentOS环境准备 1.yum -y install apr-devel apr-util check-devel cairo-devel pango-devel libxml2-devel ...
- 修改Gradle本地仓库
问题描述 Gradle 默认的本地仓库为 C:\Users\用户名\.gradle,不想让其一直下载东西占用 C 盘资源. 解决方法 新建环境变量名:GRADLE_USER_HOME,变量值为:D:\ ...
- JD孔_20160920
1. 2. 3.