BZOJ5379: Tree

Description

题目大意就是:1. 换根。2. 对$LCA(u,v)$的子树修改。3. 求$x$子树的权值和。
如果没有操作一,这就是一道沙茶题对吧。。。
于是考虑换根的影响。
具体可以看这题:链接
但是那个换根后的$LCA(u,v)$怎么解?
其实换完根的$LCA(u,v)$就是以$1$为根的$LCA(u,v),LCA(u,root),LCA(v,root)$中深度最大的那个。
这题就愉快地解决了。。。
原来写的那个板子有点$BUG$,导致我这题$WA$了无数发。。。
药丸。。。
附代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#define LSON rt<<1
#define RSON rt<<1|1
#define DATA(x) b[x].data
#define SIGN(x) b[x].c
#define LSIDE(x) b[x].l
#define RSIDE(x) b[x].r
#define WIDTH(x) (RSIDE(x)-LSIDE(x)+1)
#define MAXN 300010
using namespace std;
int n,m,c=1,d=1,root=1;
int val[MAXN],head[MAXN],deep[MAXN],son[MAXN],size[MAXN],fa[MAXN],id[MAXN],pos[MAXN],top[MAXN];
struct Tree{
int next,to;
}a[MAXN<<1];
struct Segment_Tree{
long long data,c;
int l,r;
}b[MAXN<<2];
inline int read(){
int date=0,w=1;char c=0;
while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}
return date*w;
}
inline void add(int x,int y){
a[c].to=y;a[c].next=head[x];head[x]=c++;
a[c].to=x;a[c].next=head[y];head[y]=c++;
}
void dfs1(int rt){
son[rt]=0;size[rt]=1;
for(int i=head[rt];i;i=a[i].next){
int will=a[i].to;
if(!deep[will]){
deep[will]=deep[rt]+1;
fa[will]=rt;
dfs1(will);
size[rt]+=size[will];
if(size[son[rt]]<size[will])son[rt]=will;
}
}
}
void dfs2(int rt,int f){
id[rt]=d++;pos[id[rt]]=rt;top[rt]=f;
if(son[rt])dfs2(son[rt],f);
for(int i=head[rt];i;i=a[i].next){
int will=a[i].to;
if(will!=fa[rt]&&will!=son[rt])dfs2(will,will);
}
}
inline void pushup(int rt){
DATA(rt)=DATA(LSON)+DATA(RSON);
}
inline void pushdown(int rt){
if(!SIGN(rt)||LSIDE(rt)==RSIDE(rt))return;
SIGN(LSON)+=SIGN(rt);
DATA(LSON)+=SIGN(rt)*WIDTH(LSON);
SIGN(RSON)+=SIGN(rt);
DATA(RSON)+=SIGN(rt)*WIDTH(RSON);
SIGN(rt)=0;
}
void buildtree(int l,int r,int rt){
LSIDE(rt)=l;RSIDE(rt)=r;SIGN(rt)=0;
if(l==r){
DATA(rt)=val[pos[l]];
return;
}
int mid=l+r>>1;
buildtree(l,mid,LSON);
buildtree(mid+1,r,RSON);
pushup(rt);
}
void update(int l,int r,long long c,int rt){
if(l<=LSIDE(rt)&&RSIDE(rt)<=r){
SIGN(rt)+=c;
DATA(rt)+=c*WIDTH(rt);
return;
}
pushdown(rt);
int mid=LSIDE(rt)+RSIDE(rt)>>1;
if(l<=mid)update(l,r,c,LSON);
if(mid<r)update(l,r,c,RSON);
pushup(rt);
}
long long query(int l,int r,int rt){
long long ans=0;
if(l<=LSIDE(rt)&&RSIDE(rt)<=r)return DATA(rt);
pushdown(rt);
int mid=LSIDE(rt)+RSIDE(rt)>>1;
if(l<=mid)ans+=query(l,r,LSON);
if(mid<r)ans+=query(l,r,RSON);
return ans;
}
int LCA(int x,int y){
while(top[x]!=top[y]){
if(deep[top[x]]<deep[top[y]])swap(x,y);
x=fa[top[x]];
}
if(deep[x]>deep[y])swap(x,y);
return x;
}
int check(int x){
if(x==root)return -1;
if(id[x]<=id[root]&&id[root]<=id[x]+size[x]-1){
int y=root;
while(deep[y]>deep[x]){
if(fa[top[y]]==x)return top[y];
y=fa[top[y]];
}
return son[x];
}
return 0;
}
void update_subtree(int x,int k){
int y=check(x);
if(y==-1)update(1,n,k,1);
else if(y==0)update(id[x],id[x]+size[x]-1,k,1);
else{
update(1,n,k,1);
update(id[y],id[y]+size[y]-1,-k,1);
}
}
void query_subtree(int x){
long long s;
int y=check(x);
if(y==-1)s=query(1,n,1);
else if(y==0)s=query(id[x],id[x]+size[x]-1,1);
else s=query(1,n,1)-query(id[y],id[y]+size[y]-1,1);
printf("%lld\n",s);
}
void work(){
int f,x,y,k;
while(m--){
f=read();x=read();
if(f==1)root=x;
else if(f==2){
y=read();k=read();
int lca=LCA(x,y),lca1=LCA(x,root),lca2=LCA(y,root);
if(deep[lca1]>deep[lca])lca=lca1;
if(deep[lca2]>deep[lca])lca=lca2;
update_subtree(lca,k);
}
else query_subtree(x);
}
}
void init(){
int x,y;
n=read();m=read();
for(int i=1;i<=n;i++)val[i]=read();
for(int i=1;i<n;i++){
x=read();y=read();
add(x,y);
}
deep[1]=1;
dfs1(1);
dfs2(1,1);
buildtree(1,n,1);
}
int main(){
init();
work();
return 0;
}

BZOJ5379: Tree的更多相关文章

  1. [bzoj5379]Tree_dfs序_线段树_倍增lca

    Tree bzoj-5379 题目大意:给定一棵$n$节点的树.支持:换根.把节点$u$和$v$的$lca$的子树加.询问$u$的子树和. 注释:$1\le n,q\le 3\times 10^5$. ...

  2. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  3. SAP CRM 树视图(TREE VIEW)

    树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...

  4. 无限分级和tree结构数据增删改【提供Demo下载】

    无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...

  5. 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. Shell脚本学习指南 [ 第三、四章 ] 查找与替换、文本处理工具

    摘要:第三章讨论的是编写Shell脚本时经常用到的两个基本操作.第四章总共介绍了约30种处理文本文件的好用工具. 第三章 查找与替换 概括:本章讨论的是编写Shell脚本时经常用到的两个基本操作:文本 ...

  2. BZOJ 1185 [HNOI2007]最小矩形覆盖 ——计算几何

    程序写的太垃圾,卡不过去. GG,甘拜下风. #include <map> #include <cmath> #include <queue> #include & ...

  3. 做运动(Dijkstra+并查集+MST)

    上面的题解是这样,这道题我真的脑残,其实打代码的时候就意识到了许多,可以用Dfs+Dij+二分,这样还可以卡一卡 但是我打了spfa+spfa+二分,这个显然很慢,类似的题目我好像还做过一道的,就是在 ...

  4. 插头DP--URAL1519Formula 1

    去年的老朋友.挺怀念的,回来看看. $n \leq 12,m \leq 12$,$n*m$的01矩形,问在0中走路的欧拉回路数.答案保证longlong范围. 先设计插头:左右括号和空插头:然后分3* ...

  5. iOS 取应用版本

    
// 应用网址 返回字典中有多种数据 NSString *urlString2 = [NSString stringWithFormat: @"%@", @"http: ...

  6. Oracle外键级联删除和级联更新

    https://www.2cto.com/database/201507/417496.html

  7. (44)C#网络2

    一.用SmtpClient类发送邮件 允许应用程序使用简单邮件传输协议 (SMTP) 发送电子邮件 using System.Net.Mail; SmtpClient smtpClient = new ...

  8. Paul Graham:梦寐以求的编程语言

    我的朋友曾对一位著名的操作系统专家说他想要设计一种真正优秀的编程语言.那位专家回答,这是浪费时间,优秀的语言不一定会被市场接受,很可能无人使用,因为语言的流行不取决于它本身.至少,那位专家设计的语言就 ...

  9. centos 关于防火墙的命令

    CentOS7默认的防火墙不是iptables,而是firewalle. 安装iptable iptable-service #先检查是否安装了iptables service iptables st ...

  10. Neo4j 第七篇:模式(Pattern)

    模式和模式匹配是Cypher的核心,使用模式来描述所需数据的形状,该模式使用属性图的结构来描述,通常使用小括号()表示节点,-->表示关系,-[]->表示关系和关系的类型,箭头表示关系的方 ...