BZOJ 4034 [HAOI2015]树上操作(欧拉序+线段树)
题意:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = ;
const int maxn = 2e6+;
const int maxm = 2e6+;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0); int n, m;
ll a[maxn];
vector<int>v[maxn];
int tot;
int in[maxn],out[maxn];//树上i的出、入在rk位置
ll rk[maxn];
int vis[maxn];//in 1 , out 0
void dfs(int x, int fa){
++tot;in[x]=tot;rk[tot]=a[x];vis[tot]=;
for(int i = ; i < (int)v[x].size(); i++){
int y = v[x][i];
if(y!=fa)dfs(y,x);
}
++tot;out[x]=tot;rk[tot]=-a[x];vis[tot]=;
}
ll flg[maxn],lazy[maxn];
ll sum[maxn];
void build(int l, int r, int root){
if(l==r){
if(vis[l])flg[root]=;
else flg[root]=-;
sum[root]=rk[l];
return;
}
int mid = (l+r)>>;
build(lson);
build(rson);
sum[root]=sum[lc]+sum[rc];
flg[root]=flg[lc]+flg[rc];
}
void pushdown(int l, int r, int root){
if(!lazy[root])return;
lazy[lc]+=lazy[root];
lazy[rc]+=lazy[root];
sum[lc]+=lazy[root]*flg[lc];
sum[rc]+=lazy[root]*flg[rc];
lazy[root]=;
return;
}
void update(int x, int y, int val, int l, int r, int root){
int mid = (l+r)>>;
if(x<=l&&r<=y){
lazy[root]+=val;
sum[root]+=val*flg[root];
return;
}
pushdown(l, r, root);
if(x<=mid)update(x,y,val,lson);
if(y>mid)update(x,y,val,rson);
sum[root]=sum[lc]+sum[rc];
return;
}
ll query(int x, int y, int l, int r, int root){
int mid = (l+r)>>;
if(x<=l&&r<=y)return sum[root];
pushdown(l, r, root);
ll ans = ;
if(x<=mid)ans+=query(x,y,lson);
if(y>mid)ans+=query(x,y,rson);
return ans;
} int main() {
scanf("%d %d", &n, &m);
for(int i = ; i <= n; i++){
scanf("%lld", &a[i]);
}
for(int i = ; i <= n-; i++){
int x, y;
scanf("%d %d" ,&x, &y);
v[x].pb(y);
v[y].pb(x);
}
dfs(,-);
build(,tot,);
while(m--){
int op, x, y;
scanf("%d", &op);
if(op==){
scanf("%d %d" ,&x ,&y);
update(in[x],in[x],y,,tot,);
update(out[x],out[x],y,,tot,);
}
else if(op==){
scanf("%d %d", &x ,&y);
update(in[x],out[x],y,,tot,);
}
else{
scanf("%d", &x);
printf("%lld\n",query(,in[x],,tot,));
}
}
return ;
}
/*
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
*/
BZOJ 4034 [HAOI2015]树上操作(欧拉序+线段树)的更多相关文章
- BZOJ 4034: [HAOI2015]树上操作 [欧拉序列 线段树]
题意: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 a . 操作 3 :询问某个节点 x 到根的路径中所有点的点权和. 显然树链剖分可做 ...
- 洛谷P3178 [HAOI2015]树上操作(dfs序+线段树)
P3178 [HAOI2015]树上操作 题目链接:https://www.luogu.org/problemnew/show/P3178 题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边 ...
- [luoguP3178] [HAOI2015]树上操作(dfs序 + 线段树 || 树链剖分)
传送门 树链剖分固然可以搞. 但还有另一种做法,可以看出,增加一个节点的权值会对以它为根的整棵子树都有影响,相当于给整棵子树增加一个值. 而给以某一节点 x 为根的子树增加一个权值也会影响当前子树,节 ...
- BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 )
BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 ) 题意分析 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 ...
- 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]树上操作
[HAOI2015]树上操作 传送门 题目大意:三个操作 1:a,b,c b节点权值+c 2:a,b,c 以b为根的子树节点权值全部+c 3:a,b 查询b到根路径的权值和. 题解:树链剖分 操作1 ...
- 洛谷 P3178 BZOJ 4034 [HAOI2015]树上操作
题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种:操作 1 :把某个节点 x 的点权增加 a .操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 ...
- BZOJ 4034[HAOI2015]树上操作(树链剖分)
Description 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种:操作 1 :把某个节点 x 的点权增加 a .操作 2 :把某个节点 x 为根的子树中所有点 ...
随机推荐
- 06_URL参数截取
1:如何获取URL传给子页面的参数: //获得参数(只对字母数字等有效,参数值为中文则不能传) function getQueryString(name) { var reg = new RegExp ...
- Vector人工智能机器人SDK使用笔记
Cozmo是2016年推出的,2两年后的2018年Vector上市,具备语音助手和更多功能,元件数由300+升级到700+. Vector的SDK具体说明在:developer.anki.com/ve ...
- 杂谈.netcore的Buffer相关新类型
1 文章范围 本文将.netcore新出现的与Buffer操作相关的类型进行简单分析与讲解,由于资料有限,一些见解为个人见解,可能不是很准确.这些新类型将包括BinaryPrimitives.Span ...
- C++类中的重载
函数重载回顾 函数重载的本质为相互独立的不同函数 C++中通过函数名和函数参数确定函数调用 无法直接通过函数名得到重载函数的入口地址 函数重载必然发生在同一个作用域 类中的成员函数可以进行重载 构造函 ...
- 在ubuntu 18上安装MongoDB
本文介绍 MongoDB Community Edition 在 Ubuntu 下的安装和常见配置方法.文中操作基于 MongoDB Community Edition 4.2.2 和 Ubuntu ...
- c++ 贪心讲解大礼包
贪心是什么? 它其实类似一种思想 就是总问题可以分成许多的子问题 子问题的最优解可以直接推出整个问题 它和动态规划有一定的不同之处 动态规划不能由子问题的最优解推出整个问题的最优解 所以你看都要有一个 ...
- Tarjan算法伪代码
伪代码: 栈:当前dfs路径上的点low[x]:x能到达的点中最小的dfn dfs(x,t) 将x入栈 dfn[x]=t low[x]=t for(x,y) i ...
- 第三篇python用户登录程序实现
需求: 1.通过注册输入用户名和密码 2.能够验证用户名和密码是否正确 3.限制输入一定错误次数后退出程序 4.利用格式化输出方式输出信息 分析: 使用username=input()和passwor ...
- jQuery, 文本框获得焦点后, placeholder提示文字消失
文本框获得焦点后, 提示文字消失, 基于jQuery, 兼容性: html5 //所有文本框获得焦点后, 提示文字消失 $('body').on('focus', 'input[placeholder ...
- 设置Linux主机SSH访问服务
检查是否开启22端口访问权限. 检查是否安装openssh-server 开启ssh服务:sudo service sshd start 使用ssh客户端进行访问:ssh userName@IP