Codeforces 384E 线段树+dfs序
来源:互联网 发布:java文件下载 前台 编辑:程序博客网 时间:2024/05/17 09:18
题目链接:点击打开链接
题意:
给定n个点,m个询问的无向树(1为根)
下面n个数表示每个点的权值
下面n-1行给出树
操作1:x点权值+v, x的第 i & 1 的儿子-v, 第 !(i&1) 的儿子+v
操作2:询问x点权值
dfs把树转成序列
根据深度把点分成2组
分别用线段树维护。。
然后Y一下
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<math.h>#include<set>#include<queue>#include<vector>#include<map>using namespace std;#define ll __int64#define L(x) (x<<1)#define R(x) (x<<1|1)#define N 201000ll n ,m;inline ll Mid(ll a,ll b){return (a+b)>>1;}struct Edge{ ll from, to, nex;}edge[N<<1];ll head[N], edgenum;void add(ll u,ll v){ Edge E = {u,v,head[u]}; edge[edgenum] = E; head[u] = edgenum++;}ll in[N], out[N], fa[N], Time, dep[N], V[N];void dfs(ll u, ll father, ll deep){ fa[u] = father; dep[u] = deep; in[u] = ++Time; for(ll i = head[u];~i; i = edge[i].nex){ ll v = edge[i].to; if(v==father)continue; dfs(v,u,deep+1); } out[u] = Time;}struct node{ struct E{ ll l, r, val, lazy; }t[N<<2]; void push_down(ll id){ if(t[id].l==t[id].r || t[id].lazy == 0)return ; t[L(id)].val += t[id].lazy; t[R(id)].val += t[id].lazy; t[L(id)].lazy+=t[id].lazy; t[R(id)].lazy+=t[id].lazy; t[id].lazy = 0; } void build(ll l, ll r, ll id){ t[id].l = l; t[id].r = r; t[id].val = 0; t[id].lazy = 0; if(l==r)return; ll mid = Mid(l,r); build(l,mid,L(id));build(mid+1,r,R(id)); } void update(ll l, ll r,ll val,ll id){ push_down(id); if(l == t[id].l && t[id].r == r) { t[id].val += val; t[id].lazy = val; return ;} ll mid = Mid(t[id].l, t[id].r); if(mid<l)update(l,r,val,R(id)); else if(r<=mid)update(l,r,val,L(id)); else { update(l,mid,val,L(id)); update(mid+1,r,val,R(id)); } } ll query(ll l, ll r, ll id){ push_down(id); if(l == t[id].l && t[id].r == r)return t[id].val; ll mid = Mid(t[id].l, t[id].r); if(mid<l)return query(l,r,R(id)); else if(r<=mid)return query(l,r,L(id)); return query(l,mid,L(id))+query(mid+1,r,R(id)); }}tree[2];ll query(ll u){ ll ans = tree[dep[u]&1].query(in[u],in[u],1); return ans;}void init(){Time = 0; memset(head, -1, sizeof head); edgenum = 0;}int main(){ ll i, j, u, v; while(cin>>n>>m) { init(); for(i=1;i<=n;i++)cin>>V[i]; for(i=1;i<n;i++){ cin>>u>>v; add(u,v); add(v,u); } dfs(1,-1,1); tree[0].build(1,n,1); tree[1].build(1,n,1); while(m--){ cin>>u; if(u==1) { cin>>u>>v; tree[dep[u]&1].update(in[u],out[u],v,1); if(in[u]<out[u]) tree[!(dep[u]&1)].update(in[u]+1,out[u],-v,1); } else { cin>>u; cout<<query(u)+V[u]<<endl; } } } return 0; }
1 0
- Codeforces 384E 线段树+dfs序
- Educational Codeforces Round 6(E)DFS序,线段树
- CF Educational Codeforces Round 6 E题 dfs+线段树
- Educational Codeforces Round 6 E. New Year Tree(DFS序+线段树)
- codeforces 620E New Year Tree (DFS序+线段树区间操作+二进制)
- Educational Codeforces Round 6 E. New Year Tree(dfs序线段树)
- CodeForces - 620E New Year Tree (线段树+dfs序)
- Educational Codeforces Round 6 E. New Year Tree(DFS序+线段树)
- Codeforces 620E New Year Tree dfs序+线段树+状态压缩
- Codeforces 877E Danil and a Part-time Job【Dfs序+线段树】
- Codeforces 877 E Danil and a Part-time Job(线段树+dfs序)
- Codeforces Round #442 (Div. 2) E. Danil and a Part-time Job【线段树+dfs序】
- [codeforces] 877E. Danil and a Part-time Job(DFS序+线段树)
- CodeForces 877E Danil and a Part-time Job (dfs序+线段树)
- codeforces dfs序+线段树+bitset
- Codeforces 276E Little Girl and Problem on Trees【线段树+Bfs序+Dfs序】好题!
- 【Educational Codeforces Round 6E】【线段树 dfs序】New Year Tree 子树颜色修改子树颜色数
- Codeforces Round #442 (Div. 2)-E-Danil and a Part-time Job(DFS序+线段树区间更新)
- mapreduce 只使用reduce 情况
- [linux]linux内核时间管理基础
- 组织微站在微信外被访问
- 【语言-JavaScript】windows 下修改快捷方式的 起始位置 和 目标 (vb脚本)
- 模拟手机交费系统 (c/m/s)
- Codeforces 384E 线段树+dfs序
- 撒个十分广泛
- 用友政务适时推出全国产财政管理软件
- 打工撒更好
- Swfit初学5
- 物化视图的快速刷新
- PB中TreeView控件的深度优化搜索算法程序
- 公司郭德纲
- CSS