裸题

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
int n, m, ecnt, hea[100005], dep[100005], top[100005], idx[100005], cnt;
int fa[100005], siz[100005], son[100005], uu, vv;
ll w[100005], wt[100005], ww;
struct Edge{
int too, nxt;
}edge[200005];
void add_edge(int fro, int too){
edge[++ecnt].nxt = hea[fro];
edge[ecnt].too = too;
hea[fro] = ecnt;
}
void dfs1(int x, int f){
dep[x] = dep[f] + 1;
fa[x] = f;
siz[x] = 1;
int maxSon=-1;
for(int i=hea[x]; i; i=edge[i].nxt){
int t=edge[i].too;
if(t==f) continue;
dfs1(t, x);
siz[x] += siz[t];
if(siz[t]>maxSon){
maxSon = siz[t];
son[x] = t;
}
}
}
void dfs2(int x, int topf){
top[x] = topf;
idx[x] = ++cnt;
wt[cnt] = w[x];
if(!son[x]) return ;
dfs2(son[x], topf);
for(int i=hea[x]; i; i=edge[i].nxt){
int t=edge[i].too;
if(t==fa[x] || t==son[x]) continue;
dfs2(t, t);
}
}
struct SGT{
ll sum[400005];
ll tag[400005];
void build(int o, int l, int r){
if(l==r) sum[o] = wt[l];
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
if(l<=mid) build(lson, l, mid);
if(mid<r) build(rson, mid+1, r);
sum[o] = sum[lson] + sum[rson];
}
}
void pushDown(int o, int l, int r, int lson, int rson, int mid){
tag[lson] += tag[o];
tag[rson] += tag[o];
sum[lson] += tag[o] * (mid-l+1);
sum[rson] += tag[o] * (r-mid);
tag[o] = 0;
}
void update(int o, int l, int r, int x, int y, ll k){
if(l>=x && r<=y){
sum[o] += (r-l+1) * k;
tag[o] += k;
}
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
if(tag[o]) pushDown(o, l, r, lson, rson, mid);
if(x<=mid) update(lson, l, mid, x, y, k);
if(mid<y) update(rson, mid+1, r, x, y, k);
sum[o] = sum[lson] + sum[rson];
}
}
ll query(int o, int l, int r, int x, int y){
if(l>=x && r<=y) return sum[o];
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
ll ans=0;
if(tag[o]) pushDown(o, l, r, lson, rson, mid);
if(x<=mid) ans += query(lson, l, mid, x, y);
if(mid<y) ans += query(rson, mid+1, r, x, y);
return ans;
}
}
}sgt;
ll queryRange(int uu, int vv){
ll ans=0;
while(top[uu]!=top[vv]){
if(dep[top[uu]]<dep[top[vv]]) swap(uu, vv);
ans += sgt.query(1, 1, n, idx[top[uu]], idx[uu]);
uu = fa[top[uu]];
}
if(dep[uu]>dep[vv]) swap(uu, vv);
ans += sgt.query(1, 1, n, idx[uu], idx[vv]);
return ans;
}
signed main(){
cin>>n>>m;
for(int i=1; i<=n; i++)
scanf("%lld", &w[i]);
for(int i=1; i<n; i++){
scanf("%d %d", &uu, &vv);
add_edge(uu, vv);
add_edge(vv, uu);
}
dep[1] = 1;
dfs1(1, 0);
dfs2(1, 1);
sgt.build(1, 1, n);
while(m--){
scanf("%d", &uu);
if(uu==1){
scanf("%d %lld", &vv, &ww);
sgt.update(1, 1, n, idx[vv], idx[vv], ww);
}
if(uu==2){
scanf("%d %lld", &vv, &ww);
sgt.update(1, 1, n, idx[vv], idx[vv]+siz[vv]-1, ww);
}
if(uu==3){
scanf("%d", &vv);
printf("%lld\n", queryRange(1, vv));
}
}
return 0;
}

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

  1. 树剖||树链剖分||线段树||BZOJ4034||Luogu3178||[HAOI2015]树上操作

    题面:P3178 [HAOI2015]树上操作 好像其他人都嫌这道题太容易了懒得讲,好吧那我讲. 题解:第一个操作和第二个操作本质上是一样的,所以可以合并.唯一值得讲的点就是:第二个操作要求把某个节点 ...

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

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

  3. HAOI2015 树上操作

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

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

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

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

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

  6. P3178 [HAOI2015]树上操作

    P3178 [HAOI2015]树上操作 思路 板子嘛,其实我感觉树剖没啥脑子 就是debug 代码 #include <bits/stdc++.h> #define int long l ...

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

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

  8. bzoj 4034: [HAOI2015]树上操作 (树剖+线段树 子树操作)

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

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

    BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 ) 题意分析 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 ...

随机推荐

  1. filter配置多个url-pattern和排除个别servlet

    转载自:https://blog.csdn.net/hanghangde/article/details/51298221 侵删 最近做项目遇到一个Filter需要配置多个url-pattern,上网 ...

  2. canvas雪花特效-jQuery插件实现

    这是一款效果十分逼真的html5 canvas下雪场景动画特效插件.这款下雪特效是基于Jason Brown的Snowfall jquery plugin的基础上制作的.在Snowfall jquer ...

  3. Linux命令-4类

    一.系统管理与维护   1. pwd:print working directory    打印工作目录   2. cd:  change directory    改变或进入路径       ● c ...

  4. SVN合并步骤

    1.trunk->branch/tag 分支路径在分支文件夹中,选择右键检出 2.合并分支到主干分支新增 1.txt 文件 需要合并到主干 在trunck->鼠标右键合并->合并到不 ...

  5. 使用jmeter测试数据库性能

    出现如图所示的问题 解决办法: 1.下载驱动包,将mysql-connector-Java.jar分别放到Jmeter和Java安装目录的lib和ext目录下 链接:http://pan.baidu. ...

  6. SqlServer中嵌套事务使用--事务计数指示 BEGIN 和 COMMIT 语句的数目不匹配 --根本问题

    转自  :SqlServer中嵌套事务使用--事务计数指示 BEGIN 和 COMMIT 语句的数目不匹配 --根本问题 问题: 1. System.Data.SqlClient.SqlExcepti ...

  7. CodeForces 66C Petya and File System (实现)

    模拟题,map搞一搞.要想清楚一个结点应该是要通过一个字符串找到下一个结点,题目保证所以文件夹非空,所以只要判断一个结点是不是叶子结点就可以判断它是不是文件,用了点c11的特性. #include&l ...

  8. ovx openVirtex的阅读文档

    由于flowvisor只有4个版本, 最新更新都是2013年的, 跟底层ovs版本不跟进, 最近斯坦福post一个 ovx, 猜测是flowvisor的加强版, 所以看一下文档说明 文档详见http: ...

  9. c++作业:求N的阶乘。

    N的阶乘就是n.(n-1)! 5的阶乘是什么?5*4*3*2*1 #include <iostream> using namespace std; int jiecheng(int num ...

  10. NSURL初始化失败

    服务端给返回的网页加载不出来,仔细一看,url是空的!!为什么呢. 示例: NSString *urlStr = @"http://服务器返回带有汉字的url字符串.com"; N ...