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 为根的子树中所有点 ...
随机推荐
- bootstrap-paginator基于bootstrap的分页插件
bootstrap-paginator基于bootstrap的分页插件 GitHub 官网地址:https://github.com/lyonlai/bootstrap-paginator 步骤 引包 ...
- Intellij IDEA 2018.3.5版安装详解及破解
几个参考链接: 软件下载链接:https://www.jetbrains.com/idea/ 破解补丁:链接:https://pan.baidu.com/s/1xUbil5jq_DyTbXJWUUsM ...
- response.getWriter().write()乱码问题
前台代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> & ...
- ASP.NET访问网络驱动器(映射磁盘)
也许很多朋友在做WEB项目的时候都会碰到这样一个需求: 当用户上传文件时,需要将上传的文件保存到另外一台专门的文件服务器. 要实现这样一个功能,有两种解决方案: 方案一.在文件服务器上新建一站点,用来 ...
- MVC返回400 /404/...
return new HttpStatusCodeResult(HttpStatusCode.BadRequest); //HttpStatusCode statusCode 枚举 // HttpSt ...
- php常用字符串和例子
//输出一个或多个字符串 //注:echo 不是一个函数(它是一个语言结构), 因此你不一定要使用小括号来指明参数,单引号,双引号都可以 $a = "admin1"; $b = & ...
- BZOJ4756 [USACO17JAN]Promotion Counting晋升者计数
Description The cows have once again tried to form a startup company, failing to remember from past ...
- html第九节课
正则表达式和marquee 1.表单验证<form></form> (1).非空验证(去空格) (2).对比验证(跟一个值对比) (3).范围验证(根据一个范围进行判断) (4 ...
- Problem 25
Problem 25 The Fibonacci sequence is defined by the recurrence relation: Fn = Fn−1 + Fn−2, where F1 ...
- python爬虫05 | 年轻人,不会正则表达式你睡得着觉?有点出息没有?
现在 你已经会使用 python 模拟浏览器 进行一些 Http 的请求了 那么请求完之后 服务器返回给我们一堆源代码 我们可不是啥都要的啊 我们是有原则的 我们想要的东西 怎么能一股脑的啥都往自己兜 ...