题面

Description

有一棵点数为N的树,以点1为根,且树点有边权。然后有M个操作,分为三种:

操作1:把某个节点x的点权增加a。

操作2:把某个节点x为根的子树中所有点的点权都增加a。

操作3:询问某个节点x到根的路径中所有点的点权和。

Input

第一行两个整数N,M,表示点数和操作数。

接下来一行N个整数,表示树中节点的初始权值。

接下来N-1行每行两个正整数fr,to,表示该树中存在一条边(fr,to)。

再接下来M行,每行分别表示一次操作。其中第一个数表示该操作的种类(1~3),之后接这个操作的参数(x或者x a)。

Output

对于每个询问操作,输出该询问的答案。答案之间用换行隔开。

Sample Input

5 5

1 2 3 4 5

1 2

1 4

2 3

2 5

3 3

1 2 1

3 5

2 1 2

3 3

Sample Output

6

9

13

Hint

数据范围:

对于30%的数据,N,M<=1000。

对于50%的数据,N,M<=100000且数据随机。

对于100%的数据,N,M<=100000,且所有输入数据的绝对值都不会超过10^6。

题解

依旧是很显然的树链剖分,要用longlong存答案

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
#define MAX 101000
#define lson (now<<1)
#define rson ((now<<1)|1)
inline int read()
{
register int x=0,t=1;
register char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-'){t=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*t;
}
struct Line
{
int v,next;
}e[MAX*2];
struct Node
{
long long v,lazy;
}c[MAX*5];
int h[MAX],cnt=1,tim,V[MAX];
int dfn[MAX],low[MAX],f[MAX],hson[MAX],line[MAX],size[MAX],top[MAX];
int N,Q,dep[MAX];
inline void Add(int u,int v)
{
e[cnt]=(Line){v,h[u]};
h[u]=cnt++;
}
void DFS1(int u,int ff)
{
size[u]=1;f[u]=ff;hson[u]=0;
for(int i=h[u];i;i=e[i].next)
{
int v=e[i].v;
if(v==ff)continue;
DFS1(v,u);
if(size[v]>size[hson[u]])hson[u]=v;
size[u]+=size[v];
}
}
void DFS2(int u,int tp)
{
top[u]=tp;dfn[u]=++tim;line[tim]=u;
if(hson[u])DFS2(hson[u],tp);
for(int i=h[u];i;i=e[i].next)
{
int v=e[i].v;
if(v==f[u]||v==hson[u])continue;
DFS2(v,v);
}
low[u]=tim;
}
void Build(int now,int l,int r)
{
if(l==r){c[now].v=V[line[l]];return;}
int mid=(l+r)>>1;
Build(lson,l,mid);Build(rson,mid+1,r);
c[now].v=c[lson].v+c[rson].v;
}
void pushdown(int now,int l,int r)
{
c[now].v+=1LL*(r-l+1)*c[now].lazy;
c[lson].lazy+=c[now].lazy;
c[rson].lazy+=c[now].lazy;
c[now].lazy=0;
}
void update(int now,int l,int r,int al,int ar,int w)
{
if(l==al&&r==ar){c[now].lazy+=w;return;}
int mid=(l+r)>>1;
c[now].v+=1LL*(ar-al+1)*w;
if(ar<=mid)update(lson,l,mid,al,ar,w);
else if(al>mid)update(rson,mid+1,r,al,ar,w);
else {update(lson,l,mid,al,mid,w);update(rson,mid+1,r,mid+1,ar,w);}
}
long long Query(int now,int l,int r,int al,int ar)
{
pushdown(now,l,r);
if(l==al&&r==ar)return c[now].v;
int mid=(l+r)>>1;
if(ar<=mid)return Query(lson,l,mid,al,ar);
if(al>mid)return Query(rson,mid+1,r,al,ar);
return Query(lson,l,mid,al,mid)+Query(rson,mid+1,r,mid+1,ar);
}
long long Answer(int u)
{
int v=1,tp1=top[u],tp2=top[v];
long long ans=0;
while(tp1!=tp2)
{
if(dep[tp1]<dep[tp2])
{
swap(tp1,tp2);
swap(u,v);
}
ans+=Query(1,1,N,dfn[tp1],dfn[u]);
u=f[tp1];tp1=top[u];
}
if(dep[u]<dep[v])swap(u,v);
ans+=Query(1,1,N,dfn[v],dfn[u]);
return ans;
}
int main()
{
N=read();Q=read();
for(int i=1;i<=N;++i)V[i]=read();
for(int i=1;i<N;++i)
{
int u=read(),v=read();
Add(u,v);Add(v,u);
}
DFS1(1,0);DFS2(1,1);
Build(1,1,N);
while(Q--)
{
int kk=read();
if(kk==1)
{
int a=read(),b=read();
update(1,1,N,dfn[a],dfn[a],b);
}
if(kk==2)
{
int a=read(),b=read();
update(1,1,N,dfn[a],low[a],b);
}
if(kk==3)
{
int a=read();
printf("%lld\n",Answer(a));
}
}
return 0;
}

【HAOI2015】树上操作(树链剖分)的更多相关文章

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

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

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

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

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

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

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

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

  5. P3178 [HAOI2015]树上操作 树链剖分

    这个题就是一道树链剖分的裸题,但是需要有一个魔性操作___编号数组需要开longlong!!!震惊!真的神奇. 题干: 题目描述 有一棵点数为 N 的树,以点 为根,且树点有边权.然后有 M 个操作, ...

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

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

  7. bzoj 4034: [HAOI2015]树上操作——树链剖分

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

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

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

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

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

  10. [HAOI2015]树上操作-树链剖分

    #include<bits/stdc++.h> using namespace std; const int maxn = 1e6+5; #define mid ((l+r)>> ...

随机推荐

  1. CentOS下安装XAMPP详细教程(转)

    [原文]http://blog.csdn.net/hel12he/article/details/49781813 现在PHP的集成运行环境越来越多,个人比较喜欢XAMPP,更新速度快,好用,安装便捷 ...

  2. openwrt pptpd客户端

    步骤 opkg update opkg install ppp-mod-pptp opkg install luci-proto-ppp 在OpenWRT安裝PPTP Client端 首先用ssh登陆 ...

  3. 【JavaWeb】客户关系管理系统

    前言 为了巩固开发的流程,我们再拿一个客户关系管理系统来练手...! 成果图 我们完成的就是下面的项目! 搭建配置环境 配置Tomcat 导入开发包 建立开发用到的程序包 在数据库创建相对应的表 CR ...

  4. Redis入门_上

    Redis是基于内存的Key-Value数据库,包含Set.String.SortedSet.List.Hash等数据结构,可用于缓存.排名.爬虫去重等应用场景. 1.思维导图 2.安装与配置 2.1 ...

  5. Tomcat8+Spring-Security 启用安全通道(https)的一步步实现

    近日学习Spring Security框架,学习到利用安全框架完成系统的安全通道控制时,来来回回遇到了不少问题.spring教程上写的略简单,对于我等小白来讲不足以支撑看书编码,好在网络上有资料可以查 ...

  6. Yii高级模板的安装

    1,如果你使用composer来安装的话,执行下边两条命令. composer global require "fxp/composer-asset-plugin:^1.2.0" ...

  7. iOS开发引入第三方类库的问题

    在开发iOS程序的过程中,通常在导入第三方的类库(.a/.o)文件会报出一系列的错误: Undefined symbols for architecture i386: "std::stri ...

  8. Python 关于super 的 用法和原理(挖坑)

    一.前言 Python 面向对象中有继承这个概念,初学时感觉很牛逼,里面也有个super类,经常见到,最近做一些题才算是理解了.特地记录分享给后来研究的小伙伴,毕竟现在小学生都开始学了(滑稽脸) 二. ...

  9. UVA129

    坑点在于输出格式. 四个字母一个空格,行末没有空格,64个字母换行重新打印. AC代码 #include<cstdio> const int maxn=200; int cnt; int ...

  10. 在SpringBoot使用Druid进行数据监控

    前言 之前在构建项目初始设计的时候在选择数据库连接的时候就看到Druid有这样的强大的功能.数据监控.对于一个项目来说,数据监控特别重要,之前使用对于数据库的监控都是通过mysql的日志等系统来完成的 ...