bzoj4034: [HAOI2015]树上操作(树剖)
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]树上操作(树剖)的更多相关文章
- bzoj4034[HAOI2015]树上操作 树链剖分+线段树
4034: [HAOI2015]树上操作 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 6163 Solved: 2025[Submit][Stat ...
- BZOJ4034 [HAOI2015]树上操作 树链剖分
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4034 题意概括 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三 ...
- BZOJ4034[HAOI2015]树上操作——树链剖分+线段树
题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中所有点的点权都 ...
- bzoj4034 [HAOI2015]树上操作——树链剖分
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4034 树剖裸题: 一定要注意 long long !!! update 的时候别忘了 pus ...
- [bzoj4034][HAOI2015]树上操作——树状数组+dfs序
Brief Description 您需要设计一种数据结构支持以下操作: 把某个节点 x 的点权增加 a . 把某个节点 x 为根的子树中所有点的点权都增加 a . 询问某个节点 x 到根的路径中所有 ...
- 【BZOJ4034】[HAOI2015]树上操作 树链剖分+线段树
[BZOJ4034][HAOI2015]树上操作 Description 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 ...
- bzoj千题计划242:bzoj4034: [HAOI2015]树上操作
http://www.lydsy.com/JudgeOnline/problem.php?id=4034 dfs序,树链剖分 #include<cstdio> #include<io ...
- bzoj 4034: [HAOI2015]树上操作 树链剖分+线段树
4034: [HAOI2015]树上操作 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4352 Solved: 1387[Submit][Stat ...
- BZOJ 4034[HAOI2015]树上操作(树链剖分)
Description 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种:操作 1 :把某个节点 x 的点权增加 a .操作 2 :把某个节点 x 为根的子树中所有点 ...
随机推荐
- 【Oracle】DG三种保护模式及切换方式
一.三种保护方式 Required Redo Transport Attributes for Data Protection Modes Maximum Availability Maximum P ...
- mongoose 操作 mongodb 笔记 (自己的笔记,自己看的)
mongodb下载/安装 mongoose npm install --save mongoose mongoose 数据库连接 const mongoose = require('mongoos ...
- iOS-如何返回某个字符串的拼音助记码
我也是看了网上的一个示例代码后,在它的基础上进行的修改.因为项目上会用到,我相信很多人的项目上也会用到.所以实现后,也赶紧分享出来,希望后来人不需要花费时间了. 提示:这里用到了正则表达式,使用了一个 ...
- 【sqli-labs】 less35 GET- Bypass Add Slashes(we dont need them) Integer based (GET型绕过addslashes() 函数的整型注入)
整型注入不用闭合引号,那就更简单了 http://192.168.136.128/sqli-labs-master/Less-35/?id=0 union select 1,database(),3% ...
- vue系列---identify(生成图片验证码)插件
identify 这是一个vue的插件,使用canvas来生成图形验证码. 具体参数如下: identify.vue组件(主要用于定义参数和方法) <template> <div c ...
- MySql数据库多表操作
一.连接查询[连表查询.多表查询] 当查询结果的列来源于多张表时,需要将多张表连接成一个大的数据集,再选择合适的列返回 mysql支持三种类型的连接查询,分别为: 内连接查询(inner join) ...
- 线程同步、信号量、system v IPC
一.线程同步 条件变量 什么是条件变量? 线程A等待某个条件成立,条件成立,线程A才继续向下执行.线程B的执行使条件成立,条件成立以后唤醒线程A,以继续执行.这个条件就是条件变量. pthread_c ...
- Linux下进程与线程的区别
https://www.cnblogs.com/fah936861121/articles/8043187.html https://my.oschina.net/cnyinlinux/blog/36 ...
- VMware虚拟机上安装CentOS 7
下载CentOS7,点击网址下载,地址:http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1804.iso ...
- Spring MVC学习总结(2)——Spring MVC常用注解说明
使用Spring MVC的注解及其用法和其它相关知识来实现控制器功能. 02 之前在使用Struts2实现MVC的注解时,是借助struts2-convention这个插件,如今我们使 ...