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 为根的子树中所有点 ...
随机推荐
- elk日志使用
elasticsearch +log4net.ElasticSearch+kibana(windows) 需要的东西(目前用的5.6版本) 1.先安装jdk和jre 配置java环境 2. ...
- webpack实践——DLLPlugin 和 DLLReferencePlugin的使用
DLLPlugin 和 DLLReferencePlugin的使用 DLLPlugin 和 DLLReferencePlugin 用某种方法实现了拆分 bundles,同时还大大提升了构建的速度. 1 ...
- eclipse中使用postgreSQL报错( Cannot load JDBC driver class )
需求: 使用Maven插件调用PostgreSQL数据库 环境: eclipse_4.5.0+JDK_1.7+Tomcat_7.0+Maven+postgresql-9.1-901.jdbc4.jar ...
- 【转】HTML5+WebGL:构建 3D 网页新世界
今年下半年, HTML5 和 WebGL 变成极热门词语,3D 网页来势汹汹.主流的浏览器 Google Chrome 以及 Mozilla Firefox 均致力于 HTML5+WebGL 的 3D ...
- json中含有换行符'\r','\n'的处理
一.josn简易说明 json是一种轻量级的数据交换格式,是一系列格式字符串.在数据交换中,经常会使用到,具有易读性,轻量级.很多地方会使用到,用处广泛.如下:(截取的一段json体) " ...
- go微服务框架kratos学习笔记四(kratos warden-quickstart warden-direct方式client调用)
目录 go微服务框架kratos学习笔记四(kratos warden-quickstart warden-direct方式client调用) warden direct demo-server gr ...
- git 其它补充(版本)
1.给源码贡献力量(当自己比较牛逼时) pull request 2..gitignore文件 在创建仓库的时候可以进行设置 3.版本 git tag -a v1.0 -m '版本介绍' 本地创建Ta ...
- [洛谷P3621] [APIO2007] 风铃
Description 你准备给弟弟 Ike 买一件礼物,但是,Ike 挑选礼物的方式很特别:他只喜欢那些能被他排成有序形状的东西. 你准备给 Ike 买一个风铃.风铃是一种多层的装饰品,一般挂在天花 ...
- RocketMQ 实战之快速入门
原文地址:https://www.jianshu.com/p/824066d70da8 最近 RocketMQ 刚刚上生产环境,闲暇之时在这里做一些分享,主要目的是让初学者能快速上手RocketMQ. ...
- 异想家Win10系统安装的软件与配置
1.C盘推荐一个硬盘,256G,安装好驱动,显卡配置好高性能,激活Win10,屏蔽WIn10驱动更新(Show or hide updates.diagcab),改电脑名称为Sandeepin-PC. ...