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 为根的子树中所有点 ...
随机推荐
- 清晰架构(Clean Architecture)的Go微服务: 程序容器(Application Container)
清晰架构(Clean Architecture)的一个理念是隔离程序的框架,使框架不会接管你的应用程序,而是由你决定何时何地使用它们.在本程序中,我特意不在开始时使用任何框架,因此我可以更好地控制程序 ...
- 小白学 Python 爬虫(36):爬虫框架 Scrapy 入门基础(四) Downloader Middleware
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 如何修改Docker已运行实例的端口映射
如何修改Docker已运行实例的端口映射 Docker的端口映射,往往出现在两个阶段需要处理: 1.是在docker启动前就已经确定好,哪个docker实例映射哪个端口(往往这个情况比较,需要提前做规 ...
- 搭建nginx
Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二的R ...
- 关于Element对话框组件Dialog在使用时的一些问题及解决办法
Element对话框组件Dialog在我们的实际项目开发中可以说是一个使用频率较高的组件,它能为我们展示提示的功能,如:业务模块提交前展示我们曾经输入或选择过的业务信息,或者展示列表信息中某项业务的具 ...
- 微信小程序之豆瓣电影
此文是学习小程序第二天做出的一个小demo,调用了豆瓣电影的api,但是需要填上自己appId,现在项目的 目录如下图: 效果图如下: 在这个demo里面,我更改了小程序的navigationBar, ...
- Go语言教程之结构体
Hello,大家好,我是小栈君,最近因为工作的事情延误了一点分享的进度,但是我会尽量抽时间分享关于IT干货知识,还希望大家能够持续关注"IT干货栈"哦. 闲话不多说,今天给大家继续 ...
- QGIS WGS84转其它坐标系并计算坐标
需求: 将带有经度.纬度(WGS84坐标系)坐标的文本(*.txt)转换成指定投影坐标系的shp文件并计算x,y坐标. 环境和工具: WIN10.QGIS2.16.带有经纬度坐标的文本.格式如下图: ...
- MQ使用:apollo和rabbitmq
apollo apollo 是一个更快.更可靠.更容易维护的消息代理,它是由最初的ActiveMQ的基础构建的.它使用一个完全不同的线程和消息调度架构来实现这一点.与ActiveMQ一样,apollo ...
- 在 Vue 中使用 Typescript
前言 恕我直言,用 Typescript 写 Vue 真的很难受,Vue 对 ts 的支持一般,如非万不得已还是别在 Vue 里边用吧,不过听说 Vue3 会增强对 ts 的支持,正式登场之前还是期待 ...