4034: [HAOI2015]树上操作

题目:传送门


题解:

   树剖裸题:

   麻烦一点的就只有子树修改(其实一点也不),因为子树编号连续啊,直接改段(记录编号最小和最大)

   开个long long 水模版

  


代码:

 #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
struct node
{
int x,y,next;
}a[];int len,last[];
void ins(int x,int y)
{
len++;a[len].x=x;a[len].y=y;
a[len].next=last[x];last[x]=len;
}
struct trnode
{
int l,r,lc,rc;LL c,lz;
trnode(){lz=;}
}tr[];int trlen;
void update(int now)
{
if(tr[now].lz!=)
{
int lc=tr[now].lc,rc=tr[now].rc;
if(lc!=-)tr[lc].c+=LL(tr[lc].r-tr[lc].l+)*tr[now].lz,tr[lc].lz+=tr[now].lz;
if(rc!=-)tr[rc].c+=LL(tr[rc].r-tr[rc].l+)*tr[now].lz,tr[rc].lz+=tr[now].lz;
tr[now].lz=;
}
}
void bt(int l,int r)
{
int now=++trlen;
tr[now].l=l;tr[now].r=r;tr[now].c=;
tr[now].lc=tr[now].rc=-;
if(l<r)
{
int mid=(l+r)/;
tr[now].lc=trlen+;bt(l,mid);
tr[now].rc=trlen+;bt(mid+,r);
}
}
void change(int now,int l,int r,LL c)
{
if(tr[now].l==l && r==tr[now].r){tr[now].c+=LL(r-l+)*c;tr[now].lz+=c;return ;}
int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/;
update(now);
if(r<=mid)change(lc,l,r,c);
else if(mid+<=l)change(rc,l,r,c);
else change(lc,l,mid,c),change(rc,mid+,r,c);
tr[now].c=tr[lc].c+tr[rc].c;
}
LL getsum(int now,int l,int r)
{
if(tr[now].l==l && r==tr[now].r)return tr[now].c;
int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/;
update(now);
if(r<=mid)return getsum(lc,l,r);
else if(mid+<=l)return getsum(rc,l,r);
return getsum(lc,l,mid)+getsum(rc,mid+,r);
}
int n,m,fa[],tot[],son[],dep[];
void pre_tree_node(int x)
{
son[x]=;tot[x]=;
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(y!=fa[x])
{
fa[y]=x;dep[y]=dep[x]+;
pre_tree_node(y);
if(tot[y]>tot[son[x]])son[x]=y;
tot[x]+=tot[y];
}
}
}
int tp,id,ys[],top[],L[],R[];
void pre_tree_edge(int x,int tp)
{
ys[x]=++id;top[x]=tp;L[x]=id;
if(son[x]!=)pre_tree_edge(son[x],tp);
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(y!=son[x] && y!=fa[x])pre_tree_edge(y,y);
}
R[x]=id;
}
LL sol(int x,int y)
{
LL ans=;int tx=top[x],ty=top[y];
while(tx!=ty)
{
if(dep[tx]>dep[ty])swap(tx,ty),swap(x,y);
ans+=getsum(,ys[ty],ys[y]);
y=fa[ty];ty=top[y];
}
if(x==y)return ans+getsum(,ys[x],ys[x]);
else
{
if(dep[x]>dep[y])swap(x,y);
return ans+getsum(,ys[x],ys[y]);
}
}
LL d[];
int main()
{
scanf("%d%d",&n,&m);len=;memset(last,,sizeof(last));
for(int i=;i<=n;i++)scanf("%lld",&d[i]);
for(int i=;i<n;i++)
{
int x,y;scanf("%d%d",&x,&y);
ins(x,y);ins(y,x);
}
fa[]=;dep[]=;pre_tree_node();
id=;pre_tree_edge(,);
trlen=;bt(,id);for(int i=;i<=n;i++)change(,ys[i],ys[i],d[i]);
while(m--)
{
int opt,x;LL y;scanf("%d",&opt);
if(opt==)scanf("%d%lld",&x,&y),change(,ys[x],ys[x],y);
else if(opt==)
{
scanf("%d%lld",&x,&y);
change(,L[x],R[x],y);
}
else {scanf("%d",&x);printf("%lld\n",sol(,x));}
}
return ;
}

bzoj4034: [HAOI2015]树上操作(树剖)的更多相关文章

  1. bzoj4034[HAOI2015]树上操作 树链剖分+线段树

    4034: [HAOI2015]树上操作 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 6163  Solved: 2025[Submit][Stat ...

  2. BZOJ4034 [HAOI2015]树上操作 树链剖分

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4034 题意概括 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三 ...

  3. BZOJ4034[HAOI2015]树上操作——树链剖分+线段树

    题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中所有点的点权都 ...

  4. bzoj4034 [HAOI2015]树上操作——树链剖分

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4034 树剖裸题: 一定要注意 long long !!! update 的时候别忘了 pus ...

  5. [bzoj4034][HAOI2015]树上操作——树状数组+dfs序

    Brief Description 您需要设计一种数据结构支持以下操作: 把某个节点 x 的点权增加 a . 把某个节点 x 为根的子树中所有点的点权都增加 a . 询问某个节点 x 到根的路径中所有 ...

  6. 【BZOJ4034】[HAOI2015]树上操作 树链剖分+线段树

    [BZOJ4034][HAOI2015]树上操作 Description 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 ...

  7. bzoj千题计划242:bzoj4034: [HAOI2015]树上操作

    http://www.lydsy.com/JudgeOnline/problem.php?id=4034 dfs序,树链剖分 #include<cstdio> #include<io ...

  8. bzoj 4034: [HAOI2015]树上操作 树链剖分+线段树

    4034: [HAOI2015]树上操作 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4352  Solved: 1387[Submit][Stat ...

  9. BZOJ 4034[HAOI2015]树上操作(树链剖分)

    Description 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种:操作 1 :把某个节点 x 的点权增加 a .操作 2 :把某个节点 x 为根的子树中所有点 ...

随机推荐

  1. spring注解、aop(二)

    使用注解配置spring 1.导入 spring-aop-5.0.6.RELEASE.jar包 2.为主配置文件引入新的命名空间 xmlns:context="http://www.spri ...

  2. 后端springmvc,前端html5的FormData实现文件断点上传

    前言 最近项目中有使用到文件断点上传,得空便总结总结,顺便记录一下,毕竟“好记性不如烂笔头”. 后端代码: package com.test.controller; import java.io.Bu ...

  3. 【SQL】多表查询

    多表查询,即查询可以从两个或多个表中获取数据.在Oracle中,有两种类型的连接格式:ANSI SQL连接格式和Oracle特有的连接格式.Oracle建议采用符合ANSI标准的连接格式. 1.内连接 ...

  4. (转载) ORA-12537:TNS连接已关闭

    今天在远程客户端配置EBS数据库连接的时候发生“ORA-12537:TNS连接已关闭”的错误.进入服务器运行如下命令:$tnsping VIS 这里VIS如果定义服务名,可以写成 $ tnsping ...

  5. Assembly之instruction之Indirect Autoincrement Mode

    Assembler Code Content of ROMMOV @R10+,0(R11)   MOV @R10+,0(R11) Length: One or two words Operation: ...

  6. MyEclipse获取注册码

    最近刚装上MyEclipse,一直弹窗提示注册码过期,开始还能接受,到最后,每发布一个项目便弹窗提醒,顿时感觉烦了,得治理治理这个烦人的注册码,下面是一段自动生成注册名和注册码的代码,只需要直接拿来用 ...

  7. 安卓桌布显示的dip和px

    安卓程序设计界面显示设置图像大小,在layout.xml里面有dip和px选项,dip为 什么 暂时还不知道,或许是设计桌布的设定像素比率,px为像素值: 比如我的手机是 Lenovo K920,屏幕 ...

  8. Memcached 之内存管理与删除机制

    一.内存的碎片化 如果用c语言直接 malloc,free 来向操作系统申请和释放内存时,在不断的申请和释放过程中,形成了一些很小的内存片断,无法再利用,这种空闲,但无法利用内存的现象称为内存的碎片化 ...

  9. 不能访问windows installer 服务,可能你在安全模式下运行 windows ,或者windows installer

    windows installer服务解决方案 很多朋友在安装MSI格式的文件包时,经常会遇到windows installer出错的情况,有如下几种现象: 1.所有使用windows install ...

  10. CentOS7 笔记 (一) .NETCore

    安装系统CentOS,虚拟机好麻烦,直接在阿里云开了一个6个月免费的ECS. 熟悉Linux 基本命令 登录,exit,vi ,vim,vi保存关闭,w,ls,mkdir,df,ip addr,修改系 ...