HAOI2015 树上操作
HAOI2015 树上操作
题目描述
有一棵点数为 N 的树,以点 1 为根,且树点有边权。然后有 M 个操作,分为三种:操作 1 :把某个节点 x 的点权增加 a 。操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 a 。操作 3 :询问某个节点 x 到根的路径中所有点的点权和。
输入输出格式
输入格式:
第一行包含两个整数 N, M 。表示点数和操作数。接下来一行 N 个整数,表示树中节点的初始权值。接下来 N-1 行每行三个正整数 fr, to , 表示该树中存在一条边 (fr, to) 。再接下来 M 行,每行分别表示一次操作。其中第一个数表示该操作的种类( 1-3 ) ,之后接这个操作的参数( x 或者 x a ) 。
输出格式:
对于每个询问操作,输出该询问的答案。答案之间用换行隔开。
输入输出样例
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
6 9 13
说明
对于 100% 的数据, N,M<=100000 ,且所有输入数据的绝对值都不
会超过 10^6 。
模板题就不多说啦,http://blog.sina.com.cn/s/blog_6974c8b20100zc61.html 有介绍,我就是看这个懂得
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
const int maxn = 100000 + 10;
int cnt = 0,n,m,map[maxn],val[maxn],num[maxn],size[maxn],father[maxn],son[maxn],top[maxn],dep[maxn],w[maxn];
vector<int> edges[maxn];
inline void dfs1(int now,int f) {
size[now] = 1;
father[now] = f;
dep[now] = dep[father[now]]+1;
for (size_t i = 0;i < edges[now].size();i++)
if (edges[now][i] != f) {
dfs1(edges[now][i],now);
size[now] += size[edges[now][i]];
if (size[son[now]] < size[edges[now][i]] || !son[now]) son[now] = edges[now][i];
}
}
inline void dfs2(int now,int ntop) {
top[now] = ntop;
num[now] = ++cnt;
map[num[now]] = now;
if (son[now]) dfs2(son[now],ntop);
for (size_t i = 0;i < edges[now].size();i++)
if (edges[now][i] != father[now] && edges[now][i] != son[now]) dfs2(edges[now][i],edges[now][i]);
}
struct seg { long long sum,mark,l,r; } tree[maxn*4];
inline void BuildTree(int l,int r,int root) {
if (l == r) {
tree[root].l = l;
tree[root].r = r;
tree[root].sum = val[map[l]];
tree[root].mark = 0;
return;
}
int mid = l+r>>1;
BuildTree(l,mid,root<<1);
BuildTree(mid+1,r,(root<<1)+1);
tree[root].l = l;
tree[root].r = r;
tree[root].mark = 0;
tree[root].sum = tree[root<<1].sum+tree[(root<<1)+1].sum;
}
inline void pushdown(int root) {
if (tree[root].mark) {
tree[root<<1].mark += tree[root].mark;
tree[root<<1|1].mark += tree[root].mark;
tree[root<<1].sum += tree[root].mark*(tree[root<<1].r-tree[root<<1].l+1);
tree[root<<1|1].sum += tree[root].mark*(tree[root<<1|1].r-tree[root<<1|1].l+1);
tree[root].mark = 0;
}
}
inline void Update(int l,int r,int ql,int qr,int root,long long x) {
if (ql > r || qr < l) return;
if (ql <= l && qr >= r) {
tree[root].mark += x;
tree[root].sum += x*(r-l+1);
return;
}
pushdown(root);
int mid = l+r>>1;
Update(l,mid,ql,qr,root<<1,x);
Update(mid+1,r,ql,qr,root<<1|1,x);
tree[root].sum = tree[root<<1].sum+tree[root<<1|1].sum;
}
inline long long Query(int l,int r,int ql,int qr,int root) {
if (ql > r || qr < l) return 0;
if (ql <= l && qr >= r) return tree[root].sum;
pushdown(root);
int mid = l+r>>1;
return Query(l,mid,ql,qr,root<<1)+Query(mid+1,r,ql,qr,root<<1|1);
}
inline long long QueryEdges(int u,int v) {
int topu = top[u];
int topv = top[v];
long long sum = 0;
while (topu != topv) {
if (dep[topu] < dep[topv]) {
swap(topu,topv);
swap(u,v);
}
sum += Query(1,cnt,num[topu],num[u],1);
u = father[topu];
topu = top[u];
}
if (dep[u] > dep[v]) swap(u,v);
return sum+Query(1,cnt,num[u],num[v],1);
}
int main() {
scanf("%d%d",&n,&m);
for (int i = 1;i <= n;i++) scanf("%d",&val[i]);
for (int i = 1,u,v;i < n;i++) {
scanf("%d%d",&u,&v);
edges[u].push_back(v);
edges[v].push_back(u);
}
dfs1(1,0);
dfs2(1,1);
BuildTree(1,cnt,1);
while (m--) {
long long dispose,x,y;
scanf("%lld%lld",&dispose,&x);
if (dispose == 1) {
scanf("%lld",&y);
Update(1,cnt,num[x],num[x],1,y);
} else if (dispose == 2) {
scanf("%lld",&y);
Update(1,cnt,num[x],num[x]+size[x]-1,1,y);
} else if (dispose == 3) printf("%lld\n",QueryEdges(1,x));
}
return 0;
}
HAOI2015 树上操作的更多相关文章
- 【BZOJ4034】[HAOI2015]树上操作 树链剖分+线段树
[BZOJ4034][HAOI2015]树上操作 Description 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 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 ...
- 树剖||树链剖分||线段树||BZOJ4034||Luogu3178||[HAOI2015]树上操作
题面:P3178 [HAOI2015]树上操作 好像其他人都嫌这道题太容易了懒得讲,好吧那我讲. 题解:第一个操作和第二个操作本质上是一样的,所以可以合并.唯一值得讲的点就是:第二个操作要求把某个节点 ...
- 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 :把某个节点 ...
- 洛谷P3178 [HAOI2015]树上操作(dfs序+线段树)
P3178 [HAOI2015]树上操作 题目链接:https://www.luogu.org/problemnew/show/P3178 题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边 ...
随机推荐
- Eclipse 修改 创建的Jsp的默认格式
Eclipse 的jsp模板修改 打开 eclipse 选择 Window -- Preferences
- Java读取property配置文件
读取配置文件已经成了Java程序员工作的一项必备技能. 配置文件的优点: 可维护性好 怎么个可维护性好呢? 它会让程序中变化的地方很灵活的配置,不需要修改代码. Java程序部署到服务器上去之后就变成 ...
- 启动Tomcat提示:指定的服务未安装
新下载的Tomcat7.0 解压缩完了运行tomcat7.exe屏幕一闪就没了 运行tomcat7w.exe弹出个筐 指定的服务并未以已安装的服务存在 Unable to open the Servi ...
- vim的tab键设定
多在windows上编程的童鞋可能习惯于感受tab键为4个空格的长度,不过在linux系统中一般默认设定tab键为8个空格长度来显示.事实上tab也确实是8个空格的长度.不过由于习惯问题,某些童鞋还是 ...
- 一大波jQuery事件即将来袭!
一.jQuery事件 1.focus()元素获得焦点 2.blur()元素失去焦点 3.change() 表单元素的值发生变化(可用于验证用户名是否存在) 4.click() 鼠标单击 5.dbcli ...
- 封装游戏配表读取和存储(xml格式);支持行列存取,标题存取
做服务器肯定会涉及到游戏配表,而读取游戏配表是一个必备技能; 之前一直都是采用TinyXml直接读,匹配字符串,每次加个表都是一大坨代码,看着也是心累; 因此利用了闲暇的时间封装了一个 xml配置表 ...
- [bzoj 1468][poj 1741]Tree [点分治]
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
- Java-认识变量、注释并能及时发现错误
package com;//变量的演示public class VarDemo { public static void main(String[] args) { /* * 1)题目不用抄 2)注释 ...
- (转)mq经验总结-转
场景:学习mq相关的知识,发现这是一篇总结性很强的文章,转过来学习学习! 1 mq经验总结 首先了解什么是mq?mq的作用是什么? mq是通讯中间件.他的作用是省去开发人员开发通讯工具的时间,节省开发 ...
- Spring源码情操陶冶-AbstractApplicationContext#initMessageSource
承接前文Spring源码情操陶冶-AbstractApplicationContext#registerBeanPostProcessors 约定web.xml配置的contextClass为默认值X ...