洛谷P3178[HAOI]2015 树上操作
树剖裸题,这个题更可以深刻的理解树剖中把树上的节点转换为区间的思想。
要注意在区间上连续的节点,一定是在一棵子树中。
#include <bits/stdc++.h>
#define int long long
#define ls left, mid, root << 1
#define rs mid + 1, right, root << 1 | 1
#define N 600100
using namespace std;
int n, m, rot, mod, tot, cnt;
int data[N], id[N], dep[N], size[N], lin[N], ans[N * 8], lazy[N * 8], dp[N], fa[N], top[N], son[N];
struct edg {
int to, nex;
} e[N];
inline void add(int f, int t)
{
e[++cnt].to = t;
e[cnt].nex = lin[f];
lin[f] = cnt;
}
inline void pushup(int root)
{
ans[root] = (ans[root << 1] + ans[root << 1 | 1]);
}
inline void pushdown(int root, int left, int right)
{
int mid = (left + right) >> 1;
if (lazy[root])
{
ans[root << 1] += (mid - left + 1) * lazy[root];
ans[root << 1];
ans[root << 1 | 1] += (right - mid) * lazy[root];
ans[root << 1 | 1];
lazy[root << 1] += lazy[root];
lazy[root << 1 | 1] += lazy[root];
lazy[root] = 0;
}
}
void build(int left, int right, int root)
{
if (left == right)
{
ans[root] = dp[left], ans[root];
return;
}
int mid = (left + right) >> 1;
build(ls), build(rs);
pushup(root);
}
inline void update(int left, int right, int root, int add, int ql, int qr)
{
if (left >= ql && right <= qr)
{
ans[root] += (right - left + 1) * add;
lazy[root] += add;
ans[root];
return;
}
int mid = (left + right) >> 1;
pushdown(root, left, right);//线段树的pushdown操作是为了弥补之前没向下传递标记的坑。
if (ql <= mid)
update(ls, add, ql, qr);
if (qr > mid)
update(rs, add, ql, qr);
pushup(root);
}
inline int query(int left, int right, int root, int ql, int qr)
{
int res = 0;
if (left >= ql && right <= qr)
return ans[root];
int mid = (left + right) >> 1;
pushdown(root, left, right);
if (ql <= mid)
res = ( res + query(ls, ql, qr) );
if (qr > mid)
res = res + query(rs, ql, qr) ;
return res;
}
void dfs1(int now, int f, int de)
{
fa[now] = f, dep[now] = de, size[now] = 1;
int maxsize = -1;
for (int i = lin[now]; i; i = e[i].nex)
{
if (e[i].to == f) continue;
dfs1(e[i].to, now, de + 1);
size[now] += size[e[i].to];
if (size[e[i].to] > maxsize)
{
maxsize = size[e[i].to];
son[now] = e[i].to;
}
}
}
void ulca(int x, int y, int z)
{
while (top[x] != top[y])
{
if (dep[top[x]] < dep[top[y]])
swap(x, y);
update(1, n, 1, z, id[top[x]], id[x]);
x = fa[top[x]];
}
if (dep[x] > dep[y])
swap(x, y);
update(1, n, 1, z, id[x], id[y]);
}
int qlca(int x, int y)
{
int res = 0;
while (top[x] != top[y])
{
if (dep[top[x]] < dep[top[y]])
swap(x, y);
res = ( res + query(1, n, 1, id[top[x]], id[x]) );
x = fa[top[x]];
}
if (dep[x] > dep[y])
swap(x, y);
res = ( res + query(1, n, 1, id[x], id[y]) );
return res;
}
void upd(int x, int y)
{
update(1, n, 1, y, id[x], id[x] + size[x] - 1);
}
int que(int x)
{
return query(1, n, 1, id[x], id[x] + size[x] - 1);
}
void dfs2(int now, int t)
{
top[now] = t;
dp[++tot] = data[now];
id[now] = tot;
if (!son[now])
return;
dfs2(son[now], t);
for (int i = lin[now]; i; i = e[i].nex)
{
int to = e[i].to;
if (to != fa[now] && to != son[now])
dfs2(to, to);
}
}
signed main()
{
scanf("%lld%lld", &n, &m);
rot = 1;
for (int i = 1; i <= n; i++)
scanf("%lld", &data[i]);
for (int i = 1; i < n; i++)
{
int a, b;
scanf("%lld%lld", &a, &b);
add(a, b);
add(b, a);
}
dfs1(rot, 0, 1);
dfs2(rot, rot);
build(1, n, 1);
for (int i = 1; i <= m; i++)
{
int flag;
scanf("%lld", &flag);
if (flag == 1)
{
int x, a;
scanf("%lld%lld", &x, &a);
update(1, n, 1, a, id[x], id[x]);
}
if (flag == 2)
{
int x, a;
scanf("%lld%lld", &x, &a);
update(1, n, 1, a, id[x], id[x] + size[x] - 1);
}
if (flag == 3)
{
int a;
scanf("%lld", &a);
printf("%lld\n", qlca(1, a));//不能写成id[1],id[a]因为id子树之间是连续的,所以a到1之间并不是连续边号。
}
}
return 0;
}
/*
5 5 2 24
7 3 7 8 0
1 2
1 5
3 1
4 1
3 4 2
3 2 2
4 5
1 5 1 3
2 1 3
*/
洛谷P3178[HAOI]2015 树上操作的更多相关文章
- cogs 1963. [HAOI 2015] 树上操作 树链剖分+线段树
1963. [HAOI 2015] 树上操作 ★★★☆ 输入文件:haoi2015_t2.in 输出文件:haoi2015_t2.out 简单对比时间限制:1 s 内存限制:256 M ...
- [bzoj 4034][HAOI 2015]树上操作
Description 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中 ...
- 洛谷 P4665 [BalticOI 2015]Network
洛谷 P4665 [BalticOI 2015]Network 你有一棵 $ n $ 个节点的树,你可以在树上加一些边,使这棵树变成一张无重边.自环的图,且删掉任意一条边它仍然联通.求最少要加多少条边 ...
- 洛谷P3178 [HAOI2015]树上操作(dfs序+线段树)
P3178 [HAOI2015]树上操作 题目链接:https://www.luogu.org/problemnew/show/P3178 题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边 ...
- 洛谷 P3178 BZOJ 4034 [HAOI2015]树上操作
题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种:操作 1 :把某个节点 x 的点权增加 a .操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 ...
- 洛谷P1273 有线电视网 (树上分组背包)
洛谷P1273 有线电视网 题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端,其他中转站为该树的内部节 ...
- 洛谷P3177||bzoj4033 [HAOI2015]树上染色
洛谷P3177 bzoj4033 根本不会做... 上网查了题解,发现只要在状态定义的时候就考虑每一条边全局的贡献就好了? 考虑边的贡献和修改状态定义我都想到了,然而并不能想到要结合起来 ans[i] ...
- bzoj2333[SCOI2011]棘手的操作 洛谷P3273 [SCOI2011]棘手的操作
2333? 先记一下吧,这题现在全部都是照着题解做的,因为怎么改都改不出来,只好对着题解改,以后还要再做过 以后再也不用指针了!太恶心了!空指针可不止直接特判那么简单啊,竟然还要因为空指针写奇怪的分类 ...
- 洛谷P3178 [HAOI2015]树上操作 题解 树链剖分+线段树
题目链接:https://www.luogu.org/problem/P3178 这道题目是一道树链剖分的模板题. 但是在解决这道问题的同事刷新了我的两个认识: 第一个认识是:树链剖分不光可以处理链, ...
随机推荐
- ubuntu 16.04 循环登陆问题
换了个titan x重装显卡驱动失败之后一直循环登陆,试了N种处理显卡驱动的方法,并没有啥用. 最后查看了一下.Xerrer文件(具体的文件名我给忘记了),发现是.Xauthority. 现象:在Ub ...
- mysql开启二进制日志
打开xhell进入系统 进入mysql配置文件目录 执行 cd /etc/mysql 首先找到my.cnf这个配置文件,然后使用vim进入文件编辑 放开我标记的地方. 注意我标记的地方,其实这个就是在 ...
- Java隐式类型转换和强制类型转换
一.强制类型转换 char 和 整型之间的类型转换 char a7 = 'a'; System.out.println(a7); System.out.println( (int)a7 ); Syst ...
- Java调用WebService方法总结(9,end)--Http方式调用WebService
Http方式调用WebService,直接发送soap消息到服务端,然后自己解析服务端返回的结果,这种方式比较简单粗暴,也很好用:soap消息可以通过SoapUI来生成,也很方便.文中所使用到的软件版 ...
- Html form表单大全(一)
在前后端交互的过程中,除了ajax请求之外,最常见的就是表单请求了. 由于form表单属性多,表单标签内容多且复杂,不深究的话很难全面的弄明白. 接下来就来详细的说一说整个form表单都有些什么,并且 ...
- React Native 开发豆瓣评分(六)添加字体图标
添加依赖 yarn add react-native-vector-icons Link 依赖 react-native link react-native-vector-icons 使用默认字体图标 ...
- windows下git创建本地分支并建立对应远程分支
在对应项目目录下打开命令提示符 git branch -a 查看所有本地和远程分支 git checkout -b [newBranch] 建立本地分支newBranch git p ...
- JavaScript之运算符
(1)赋值运算符 // c+=1; // 相当于c=c+1; // console.log(a++); // 先将a的值赋值给表达式,a再加1 // console.log(++a); // a先加1 ...
- iview Carousel 轮播图自适应宽高;iview 轮播图 图片重叠问题;iview tabs 高度互相影响问题;vue this问题;
最终效果图: 一.轮播图中图片自适应宽高: <Carousel loop v-bind:height="imgHeight+'px'" v-model="caro ...
- Django-admin数据库记录展示调整
Django-admin数据库记录展示调整 admin.py from django.contrib import admin from user import models # Register y ...