如果这题只传到儿子不继续向下就是裸的dfs序+线段树,继续往下传的还改变正负号,我们可以根据它的层数来确定正负号

  1 #include<bits/stdc++.h>
  2
  3 #define inf 0x3f3f3f3f
  4
  5 #define lson (id<<1)
  6
  7 #define rson ((id<<1)|1)
  8
  9 #define mid ((l+r)>>1)
 10
 11 const int maxn=200000;
 12
 13 using namespace std;
 14
 15 int t;
 16
 17 int p;
 18
 19 int n,m;
 20
 21 int u,v;
 22
 23 int x,val;
 24
 25 int dep[maxn+10];
 26
 27 int a[maxn+10];
 28
 29 int pos[maxn+10];
 30
 31 int q[maxn+10];
 32
 33 int son[maxn+10];
 34
 35 int tree[maxn*4+10];
 36
 37 int sum[maxn*4+10];
 38
 39 int lazy[maxn*4+10];
 40
 41 vector<int> G[maxn+10];
 42
 43 void push_up(int id){
 44    tree[id]=tree[lson]+tree[rson];
 45 }
 46
 47 void push_down(int id,int l,int r){
 48         if(lazy[id]){
 49                 lazy[lson]+=lazy[id];
 50                 lazy[rson]+=lazy[id];
 51                 tree[lson]+=(mid-l+1)*lazy[id];
 52                 tree[rson]+=(r-mid)*lazy[id];
 53                 lazy[id]=0;
 54         }
 55         return ;
 56 }
 57
 58 void build(int id,int l,int r){
 59       if(l==r){
 60         sum[id]=a[pos[l]];
 61         return ;
 62       }
 63       build(lson,l,mid);
 64       build(rson,mid+1,r);
 65       push_up(id);
 66       return ;
 67 }
 68
 69 void update(int id,int l,int r,int x,int y,int val){
 70         if(l==x&&r==y){
 71                 tree[id]+=val*(r-l+1);
 72                 lazy[id]+=val;
 73                 return ;
 74         }
 75         push_down(id,l,r);
 76         if(x>mid){
 77                 update(rson,mid+1,r,x,y,val);
 78         } else if(y<=mid){
 79                 update(lson,l,mid,x,y,val);
 80         } else {
 81                 update(lson,l,mid,x,mid,val);
 82                 update(rson,mid+1,r,mid+1,y,val);
 83         }
 84 }
 85
 86 int fi(int id,int l,int r,int x){
 87         if(l==r){
 88                 int temp=dep[pos[l]]&1;
 89                 if(!temp) temp=-1;
 90                 return sum[id]+lazy[id]*temp;
 91         }
 92         push_down(id,l,r);
 93         if(x<=mid){
 94                 return fi(lson,l,mid,x);
 95         } else {
 96                 return fi(rson,mid+1,r,x);
 97         }
 98 }
 99
100 void dfs(int x,int fa,int d){
101         dep[x]=d;
102         pos[++p]=x;
103         q[x]=p;
104         for(size_t i=0;i<G[x].size();i++){
105                 if(G[x][i]==fa) continue;
106                 dfs(G[x][i],x,d+1);
107                 son[x]++;
108                 son[x]+=son[G[x][i]];
109         }
110 }
111
112 int main()
113 {
114     scanf("%d%d",&n,&m);
115     for(int i=1;i<=n;i++){
116         scanf("%d",&a[i]);
117     }
118     for(int i=1;i<n;i++){
119         scanf("%d%d",&u,&v);
120         G[u].push_back(v);
121         G[v].push_back(u);
122     }
123     dfs(1,0,1);
124     build(1,1,n);
125     for(int i=1;i<=m;i++){
126         scanf("%d",&t);
127         if(t==1){
128         scanf("%d%d",&x,&val);
129         if(dep[x]&1)
130         update(1,1,n,q[x],q[x]+son[x],val);
131         else update(1,1,n,q[x],q[x]+son[x],-val);
132         } else {
133         scanf("%d",&x);
134         int ans=fi(1,1,n,q[x]);
135         printf("%d\n",ans);
136         }
137     }
138     return 0;
139 }

View Code

转载于:https://www.cnblogs.com/GeniusYang/p/6049990.html

Codeforces 384E-线段树+dfs序相关推荐

  1. bzoj3252攻略(线段树+dfs序)或者(树链剖分+dfs)

    3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1341 Solved: 642 [Submit][Status][Discuss] ...

  2. BZOJ_3252_攻略_线段树+dfs序

    BZOJ_3252_攻略_线段树+dfs序 Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏.今天他得到了一款新游戏< ...

  3. CodeForces - 620E New Year Tree(线段树+dfs序+状态压缩)

    题目链接:点击查看 题目大意:给出一棵无向树,每个节点都有一种颜色,接下来时m次操作: 1 x y:将x及其子树染成y的颜色 2 x:查询x及其子树上共有多少种不同的颜色 题目分析:看完这个题的第一反 ...

  4. Codeforces 877 E Danil and a Part-time Job(线段树+dfs序)

    题目地址 题意:给你一棵树,1为根节点,每个节点都有应该状态0或者1,你有两种操作,pow的操作是把该节点以及他的所有子树的每个节点进行自身异或的操作,get就是查询该节点以及他的所有子树的每个节点有 ...

  5. HDU5692(线段树+dfs序)

    Snacks Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  6. bzoj3252攻略(线段树+dfs序)

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 562  Solved: 238 [Submit][Status][Discuss] ...

  7. HDU - 3974 Assign the task (线段树 + dfs序)

    HDU - 3974 题意:有个公司有一些关系,每个人(除了boss)都有且仅有一个上司,这就是一棵树的关系,然后会有一些操作,C i,询问第i个人现在的任务,T x y,把y任务给x, 给x相当于给 ...

  8. 苹果树(线段树+Dfs序)

    1228 苹果树  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 在卡卡的房子外面,有一棵苹果树.每年的春天,树上总会结 ...

  9. Change FZU - 2277(线段树+dfs序)

    There is a rooted tree with n nodes, number from 1-n. Root's number is 1.Each node has a value ai. I ...

最新文章

  1. 安卓网页广告拦截_拦截烦人的网页广告,增加上网体验
  2. 我们需要什么样的数据架构?
  3. response.setContentType()方法浅析
  4. php引用地址,关于php的引用
  5. Mac OS X Yosemite安装盘U盘制作
  6. 被面试官虐过之后,他轻蔑的问我:你还说你了解单例模式吗?
  7. 【upstream】Nginx配置upstream实现负载均衡
  8. 成功解决TypeError: a bytes-like object is required, not ‘str‘
  9. flacs 安装教程_文章详细信息
  10. 【排序算法】堆排序——常规方法
  11. 是不让页面重新渲染元素~·
  12. python lxml 模块_Python lxml模块安装教程
  13. 详细推导波特图增益和相位曲线
  14. win10删除自定义的背景图片历史记录
  15. 千月双端影视APP源码
  16. 最新+电脑象棋测试软件,中国象棋2017电脑版
  17. 神经网络浅讲:从神经元到深度学习
  18. rabbitMq入门
  19. pytest的mark功能
  20. tup,list,dict,set的创建

热门文章

  1. dao层如何调用对象_以k8s集群管理为例,大牛教你如何设计优秀项目架构
  2. pion webrtc 示例代码解析 一
  3. 关于Mytatis动态拼接in语句并且按照指定顺序排序的问题
  4. php7扩展开发教程,Laravel 7 扩展开发教程
  5. 添加lua_非关系型数据库Redis之Lua脚本
  6. 【Elasticsearch】Elasticsearch:Searchable snapshot - 可搜索的快照
  7. 【Elasticsearch】 es Bootstrap Checks Failed
  8. 【Spring】Spring 依赖注入之手动注入
  9. 【Spring】Spring 自定义scope
  10. 【Flink】FlinkException The file LOG does not exist on the TaskExecutor