P3178 [HAOI2015]树上操作

思路

板子嘛,其实我感觉树剖没啥脑子

就是debug

代码

#include <bits/stdc++.h>
#define int long long
#define ll long long
#define ls rt<<1
#define rs rt<<1|1
#define FOR(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
const int maxn=1e6+7;
int read() {
int x=0,f=1;char s=getchar();
for(;s>'9'||s<'0';s=getchar()) if(s=='-') f=-1;
for(;s>='0'&&s<='9';s=getchar()) x=x*10+s-'0';
return x*f;
}
int n,m,w[maxn];
int dep[maxn],son[maxn],fa[maxn],siz[maxn];
int a[maxn],idx[maxn],top[maxn],cnt;
struct node {
int v,nxt;
}e[maxn<<1];
int head[maxn<<1],tot;
void add_edge(int u,int v) {
e[++tot].v=v;
e[tot].nxt=head[u];
head[u]=tot;
}
namespace seg_tree {
struct node {
int l,r,siz;
ll tot,lazy;
}e[maxn<<2];
void pushup(int rt) {
e[rt].tot=e[ls].tot+e[rs].tot;
}
void pushdown(int rt) {
if(e[rt].lazy) {
e[ls].tot+=e[ls].siz*e[rt].lazy;
e[rs].tot+=e[rs].siz*e[rt].lazy;
e[ls].lazy+=e[rt].lazy;
e[rs].lazy+=e[rt].lazy;
e[rt].lazy=0;
}
}
void build(int l,int r,int rt) {
e[rt].l=l,e[rt].r=r,e[rt].siz=r-l+1;
if(l==r) {
e[rt].tot=a[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,ls);
build(mid+1,r,rs);
pushup(rt);
}
void modify(int L,int R,int k,int rt) {
if(L<=e[rt].l&&e[rt].r<=R) {
e[rt].tot+=e[rt].siz*k;
e[rt].lazy+=k;
return;
}
pushdown(rt);
int mid=(e[rt].l+e[rt].r)>>1;
if(L<=mid) modify(L,R,k,ls);
if(R>mid) modify(L,R,k,rs);
pushup(rt);
}
ll query(int L,int R,int rt) {
if(L<=e[rt].l&&e[rt].r<=R) return e[rt].tot;
pushdown(rt);
int mid=(e[rt].l+e[rt].r)>>1;
ll ans=0;
if(L<=mid) ans+=query(L,R,ls);
if(R>mid) ans+=query(L,R,rs);
return ans;
}
}
void dfs1(int u,int f) {
siz[u]=1;
fa[u]=f;
son[u]=0;
dep[u]=dep[f]+1;
for(int i=head[u];i;i=e[i].nxt) {
int v=e[i].v;
if(v==f) continue;
dfs1(v,u);
if(siz[v] > siz[son[u]]) son[u]=v;
siz[u]+=siz[v];
}
}
void dfs2(int u,int ttt) {
idx[u]=++cnt;
a[cnt]=w[u];
top[u]=ttt;
if(!son[u]) return;
dfs2(son[u],ttt);
for(int i=head[u];i;i=e[i].nxt) {
if(!idx[e[i].v])
dfs2(e[i].v,e[i].v);
}
}
ll SUM(int x,int y) {// 路径
ll ans=0;
while(top[x]!=top[y]) {
if(dep[top[x]]<dep[top[y]]) swap(x,y);
ans+=seg_tree::query(idx[top[x]],idx[x],1);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
ans+=seg_tree::query(idx[x],idx[y],1);
return ans;
}
main() {
n=read(),m=read();
FOR(i,1,n) w[i]=read();
FOR(i,2,n) {
int x=read(),y=read();
add_edge(x,y);
add_edge(y,x);
}
dfs1(1,0);
dfs2(1,1);
seg_tree::build(1,n,1); FOR(i,1,m) {
int opt=read(),x=read(),a;
if(opt==1) {
a=read();
seg_tree::modify(idx[x],idx[x],a,1);
} else if(opt==2) {
a=read();
seg_tree::modify(idx[x],idx[x]+siz[x]-1,a,1);
} else if(opt==3) {
cout<<SUM(1,x)<<"\n";
}
}
return 0;
}

P3178 [HAOI2015]树上操作的更多相关文章

  1. 洛谷P3178 [HAOI2015]树上操作(dfs序+线段树)

    P3178 [HAOI2015]树上操作 题目链接:https://www.luogu.org/problemnew/show/P3178 题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边 ...

  2. 洛谷P3178 [HAOI2015]树上操作 题解 树链剖分+线段树

    题目链接:https://www.luogu.org/problem/P3178 这道题目是一道树链剖分的模板题. 但是在解决这道问题的同事刷新了我的两个认识: 第一个认识是:树链剖分不光可以处理链, ...

  3. 洛谷P3178 [HAOI2015]树上操作

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

  4. 【luogu P3178 [HAOI2015]树上操作】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3178 模板题 菜 #include <cstdio> #include <cstring& ...

  5. 洛谷P3178 [HAOI2015]树上操作(线段树)

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

  6. 洛谷 P3178 [HAOI2015]树上操作

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

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

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

  8. 洛谷——P3178 [HAOI2015]树上操作

    https://www.luogu.org/problem/show?pid=3178#sub 题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种:操作 1 ...

  9. LUOGU P3178 [HAOI2015]树上操作

    传送门 解题思路 树链剖分裸题,线段树维护. 代码 #include<iostream> #include<cstdio> #include<cstring> #d ...

随机推荐

  1. 依赖反转Ioc和unity,autofac,castle框架教程及比较

    1.依赖倒置的相关概念 http://www.cnblogs.com/fuchongjundream/p/3873073.html IoC模式(依赖.依赖倒置.依赖注入.控制反转) 2.依赖倒置的方式 ...

  2. python windows环境下安装

    下载python安装包,双击安装后, 在cmd中输入python 若无反应, 在cmd设置环境变量 变量 : set PATH=C:\...\...\...[python的编译器的路径]:%PATH% ...

  3. spring 框架的优点

    谈spring 框架的优点就是说spring 框架2大核心技术的优点 1. 控制反转:控制反转是将对象的创建和管理交给spring容器,已经管理对象之间的依赖关系, 那么将对象的创建和生命周期的管理交 ...

  4. 10.for

    要遍历一个范围(如数组或表),使用 for 循环.在数组上迭代时,当前数组元素存储在循环变量中.在遍历字典时, index 存储在循环变量中. (in 内容测试) for x in [5, 7, 11 ...

  5. html5-hgroup和address元素

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  6. 【转】SQL Server 运行状况监控SQL语句

    SQL Server 运行状况监控SQL语句   Microsoft SQL Server 2005 提供了一些工具来监控数据库.方法之一是动态管理视图.动态管理视图 (DMV) 和动态管理函数 (D ...

  7. Python - 3. Input and Output

    from:http://interactivepython.org/courselib/static/pythonds/Introduction/InputandOutput.html Input a ...

  8. 抓取biqukan

    #python3.7 ''' 功能:实现www.biqukan.com/1_1094/5403177.html小说下载为txtv1.0 ''' import requests,sys,time fro ...

  9. 20165305 苏振龙《Java程序设计》第二周学习总结

    代码托管(ch2,ch3) 脚本截图 教材内容总结 类型.变量与运算符 基本类型 整数(short.int.long) 字节(byte) 浮点数(float/double) 字符(char)将一个数字 ...

  10. 底层代码创建GUI

    %底层代码创建GUI hf = figure(... 'Units','Normalized',... 'Color','w',... 'Position',[0.1 0.1 0.8 0.8]); h ...