luogu3178 [HAOI2015]树上操作
裸题
#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]树上操作的更多相关文章
- 树剖||树链剖分||线段树||BZOJ4034||Luogu3178||[HAOI2015]树上操作
题面:P3178 [HAOI2015]树上操作 好像其他人都嫌这道题太容易了懒得讲,好吧那我讲. 题解:第一个操作和第二个操作本质上是一样的,所以可以合并.唯一值得讲的点就是:第二个操作要求把某个节点 ...
- 【BZOJ4034】[HAOI2015]树上操作 树链剖分+线段树
[BZOJ4034][HAOI2015]树上操作 Description 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 ...
- HAOI2015 树上操作
HAOI2015 树上操作 题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种:操作 1 :把某个节点 x 的点权增加 a .操作 2 :把某个节点 x 为根 ...
- bzoj千题计划242:bzoj4034: [HAOI2015]树上操作
http://www.lydsy.com/JudgeOnline/problem.php?id=4034 dfs序,树链剖分 #include<cstdio> #include<io ...
- bzoj4034[HAOI2015]树上操作 树链剖分+线段树
4034: [HAOI2015]树上操作 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 6163 Solved: 2025[Submit][Stat ...
- P3178 [HAOI2015]树上操作
P3178 [HAOI2015]树上操作 思路 板子嘛,其实我感觉树剖没啥脑子 就是debug 代码 #include <bits/stdc++.h> #define int long l ...
- bzoj 4034: [HAOI2015]树上操作 树链剖分+线段树
4034: [HAOI2015]树上操作 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4352 Solved: 1387[Submit][Stat ...
- bzoj 4034: [HAOI2015]树上操作 (树剖+线段树 子树操作)
4034: [HAOI2015]树上操作 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 6779 Solved: 2275[Submit][Stat ...
- BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 )
BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 ) 题意分析 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 ...
随机推荐
- It is not the destination so much as the journey, they say.
It is not the destination so much as the journey, they say. 人家说目的地不重要,重要的是旅行的过程.<加勒比海盗>
- UIButton 图片文字位置
在实际开发过程中经常在按钮上添加文字和图片,位置和图片的位置根据需求放置也是不一样的.下面实现了各种显示方式,如下图: UIButton+LSAdditions.h // // UIButton+LS ...
- RK3288开发过程中遇到的问题点和解决方法之Framework
删除小电池图标及百分比 a.SystemUI/.../statusbar/policy/BatteryController.java mBatteryPercentageView.setVisibil ...
- 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
- UVA 1600 Patrol Robert 巡逻机器人 (启发搜索BFS)
非常适合A*的一道题. 比普通的迷宫问题加一个信息k表示当前穿过的障碍物的数量. #include<cstdio> #include<cstring> #include< ...
- Netbackup驱动器常用命令vmoprcmd
1.vmoprcmd vmoprcmd – 对驱动器执行操作员功能 大纲 vmoprcmd -devmon [pr | ds | hs] [-h device_host] default_operat ...
- 陆教授浅谈5G毫米波手机天线技术的发展现状和未来的应用场景
近日,香港城大电子工程学系讲座教授陆贵文教授荣获英国皇家工程院院士荣衔,以表彰他在推动天线研究的卓越贡献.他研发的天线由L形探针馈电微带天线.磁电耦极天线,以至5G毫米波手机天线等技术,均在天线领域影 ...
- JavaScript实现页面到滚动到指定位置执行某些操作
比如 页面中 某个DOM, 希望点击按钮后页面直接跳转到 这个DOM所在的位置, 1. 获取DOM离屏幕的高度 var hTop = $('#box').offset().top; $('body,h ...
- 使用filter函数筛选出素数
function getPrimeNumber(arr) { return arr.filter(function (number) { if (typeof number !== 'number' ...
- 【转】Matlab的regionprops详解
matlab函数_连通区域 1. matlab函数bwareaopen──删除小面积对象格式:BW2 = bwareaopen(BW,P,conn)作用:删除二值图像BW中面积小于P的对象,默认情况下 ...