【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 ...
随机推荐
- VS2012打包部署教程
前言 通常我们只是写一些系统,然后想要运行功能的时候就打开代码点击启动,这样只适用于开发人员或者局部开发人员这样做,软件开发的大多数意义上就是拿出开发的软件让用户放心的去点.用户无需知道代码,无需知道 ...
- 我的省选 Day -5
Day -5 时间载着我们,一天又一天,呼啸而过. 已经记不清今天是Day 负几了,总之还有不到一个星期就要去参加选拔赛了. 写一下今晚做NOI2009的心路历程. T1题意有点绕,但很快看出是个二分 ...
- QPS,TPS,吞吐量,响应时间详解及关系
并发数 并发数是指系统同时能处理的请求数量,这个也是反应了系统的负载能力 吞吐量吞吐量是指单位时间内系统能处理的请求数量,体现系统处理请求的能力,这是目前最常用的性能测试指标 响应时间RT(Respo ...
- javascript数组常用的遍历方法
本篇文章给大家带来的内容是关于javascript数组常用的遍历方法(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 前言 本文主要介绍数组常见遍历方法:forEach.m ...
- Spring @CacheEvict 无效。。。。
使用 此 注解 时...需要 配置 它 的 key .... @CacheEvict(value = { "adminFunc", "groupFunc" }, ...
- myeclipse集成svn客户端
转载大神 https://blog.csdn.net/tandeng19901222/article/details/5979075
- spark_learn
package chapter03 import org.apache.spark.sql.DataFrame import org.apache.spark.sql.hive.HiveContext ...
- LeetCode 088 Merge Sorted Array 合并两个有序数组
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:You ...
- HDU 5785 Interesting manacher + 延迟标记
题意:给你一个串,若里面有两个相邻的没有交集的回文串的话,设为S[i...j] 和 S[j+1...k],对答案的贡献是i*k,就是左端点的值乘上右端点的值. 首先,如果s[x1....j].s[x2 ...
- CentOS 7 iptables 开放8080端口
# 安装iptables-services [root@localhost bin]# yum install iptables-services [root@localhost bin]# /bin ...