BZOJ 4034 线段树+DFS序
思路:
先搞出来每个点的DFS序 (要有入栈和出栈两种状态的)
处理出来 线段树区间有多少入栈的和多少出栈的
加区间的时候就加(入-出)*wei
查前缀和
//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 200050
#define int long long
int n,m,Wei[N],v[N],first[N],next[N],tot,start[N],end[N],cnt;
int xx,yy,ww,op,tree[N*20],mark[N*20],marka[N*20],vv[N];
void add(int x,int y){v[tot]=y,next[tot]=first[x],first[x]=tot++;}
void dfs(int x,int fa){
start[x]=++cnt;vv[cnt]=1;
for(int i=first[x];~i;i=next[i])
if(v[i]!=fa)
dfs(v[i],x);
end[x]=++cnt;vv[cnt]=-1;
}
void push_down(int pos){
int lson=pos<<1,rson=pos<<1|1;
tree[lson]+=mark[lson]*marka[pos];
tree[rson]+=mark[rson]*marka[pos];
marka[lson]+=marka[pos];
marka[rson]+=marka[pos];
marka[pos]=0;
}
void build(int l,int r,int pos){
if(l==r){mark[pos]=vv[l];return;}
int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
build(l,mid,lson),build(mid+1,r,rson);
mark[pos]=mark[lson]+mark[rson];
}
void insert(int l,int r,int pos,int wei,int x){
if(l==r){tree[pos]+=wei;return;}
if(marka[pos])push_down(pos);
int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
if(mid<x)insert(mid+1,r,rson,wei,x);
else insert(l,mid,lson,wei,x);
tree[pos]=tree[lson]+tree[rson];
}
int query(int l,int r,int pos,int x){
if(r<=x){return tree[pos];}
if(marka[pos])push_down(pos);
int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
if(mid>=x)return query(l,mid,lson,x);
else return query(l,mid,lson,x)+query(mid+1,r,rson,x);
}
void Change(int l,int r,int pos){
if(l>=xx&&r<=yy){
marka[pos]+=ww;
tree[pos]+=ww*mark[pos];
return;
}
if(marka[pos])push_down(pos);
int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
if(mid<xx)Change(mid+1,r,rson);
else if(mid>=yy)Change(l,mid,lson);
else Change(l,mid,lson),Change(mid+1,r,rson);
tree[pos]=tree[lson]+tree[rson];
}
signed main(){
memset(first,-1,sizeof(first));
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++)scanf("%lld",&Wei[i]);
for(int i=1;i<n;i++)
scanf("%lld%lld",&xx,&yy),add(xx,yy),add(yy,xx);
dfs(1,-1);
build(1,cnt,1);
for(int i=1;i<=n;i++)
insert(1,cnt,1,Wei[i],start[i]),insert(1,cnt,1,-Wei[i],end[i]);
for(int i=1;i<=m;i++){
scanf("%lld",&op);
if(op==1){
scanf("%lld%lld",&xx,&ww);
insert(1,cnt,1,ww,start[xx]);
insert(1,cnt,1,-ww,end[xx]);
}
else if(op==2){
scanf("%lld%lld",&xx,&ww);
yy=end[xx],xx=start[xx];
Change(1,cnt,1);
}
else if(op==3){
scanf("%lld",&xx);
printf("%lld\n",query(1,cnt,1,start[xx]));
}
}
}
BZOJ 4034 线段树+DFS序的更多相关文章
- Tsinsen A1505. 树(张闻涛) 倍增LCA,可持久化线段树,DFS序
题目:http://www.tsinsen.com/A1505 A1505. 树(张闻涛) 时间限制:1.0s 内存限制:512.0MB 总提交次数:196 AC次数:65 平均分: ...
- BZOJ_3252_攻略_线段树+dfs序
BZOJ_3252_攻略_线段树+dfs序 Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏.今天他得到了一款新游戏< ...
- 【XSY2534】【BZOJ4817】树点涂色 LCT 倍增 线段树 dfs序
题目大意 Bob有一棵\(n\)个点的有根树,其中\(1\)号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜 ...
- 【bzoj4817】树点涂色 LCT+线段树+dfs序
Description Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路 径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色. ...
- S - Query on a tree HDU - 3804 线段树+dfs序
S - Query on a tree HDU - 3804 离散化+权值线段树 题目大意:给你一棵树,让你求这棵树上询问的点到根节点直接最大小于等于val的长度. 这个题目和之前写的那个给你一棵 ...
- HDU 5692 线段树+dfs序
Snacks Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序
3779: 重组病毒 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 224 Solved: 95[Submit][Status][Discuss] ...
- bzoj4034 线段树+dfs序
https://www.lydsy.com/JudgeOnline/problem.php?id=4034 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 ...
- 【BZOJ-3306】树 线段树 + DFS序
3306: 树 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 792 Solved: 262[Submit][Status][Discuss] De ...
随机推荐
- DES 加密
package com.cloudunicomm.utils; import java.io.UnsupportedEncodingException; import java.security.Se ...
- Windows下安装Linux虚拟机的用途和好处
Windows一般是办公界面,主要做代码编辑查看,资料查找,还有发邮件,也可以用Windows下的其他的有用软件,Linux主要作为编译工具,基本上开发都是在Linux平台下编译,例如编译驱动就需要在 ...
- 【codeforces 239B】Easy Tape Programming
[题目链接]:http://codeforces.com/contest/239/problem/B [题意] 给你一个长度为n的字符串,只包括'<">'以及数字0到9; 给你q ...
- CodeForces - 552E Vanya and Brackets
Vanya and Brackets Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u ...
- NYIST 1019 G.亲戚来了
G.亲戚来了 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 Bob 一家人要去下馆子,为什么呢?因为他姑姑的大爷的叔叔的孙子的表叔的婶婶的儿子来了,亲戚来了当然要下 ...
- Android之——拦截短信
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46994097 这里.向大家简介通过BroadcastReceiver来拦截短信的方 ...
- iOS9适配小结
前言 最新公布的app版本号适配了iOS9.总结一下适配过程的几个要点. Bitcode iOS9此番推出了新的特性:Bitcode,关于Bitcode的资料大家能够在网上找.Bitcode要求pro ...
- HTML5 界面元素 Canvas 參考手冊
HTML5 界面元素 Canvas 參考手冊 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协 ...
- WebAPI的自动化监控和预警
Metrics.net + influxdb + grafana 构建WebAPI的自动化监控和预警 前言 这次主要分享通过Metrics.net + influxdb + grafana 构建Web ...
- Metasploit学习笔记(博主推荐)
不多说,直接上干货! 连接后台的数据库,当然不是必须品. 连接数据库的好处:1.可以攻击和扫描的结果,保存起来 2.将一些搜索结果做个缓存 默认数据库是postgresql. 同时要注意的是 ...