题意:题意很简单么,给定n个点,m个询问的无向树(1为根),每个点的权值,有两种操作,

第一种:1 x v,表示把 x 结点加上v,然后把 x 的的子结点加上 -v,再把 x 的子结点的子结点加上 -(-v),依次。。。

第二种:2 x, 表示查询 x 结点的权值。

析:因为这是一棵树,很难维护,所以可以考虑先用 dfs 记录每个结点的开始和结束的时间戳,而位于开始和结束时间戳内的就是就是它的子孙结点,

这样就能维护两棵线段树,一棵是奇数层的, 一棵是偶数层的,每次执行 1 操作就把相应的结点的开始和结束作为一个区间进行更新,然后再执行

相反层的 -v 进行更新。当查询的时候,就直接输出相应层的输出再加原来权值即可。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 200000 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} int dfsnum[maxn], in[maxn];
int a[maxn], out[maxn], dep[maxn];
int sum[2][maxn<<2], addv[2][maxn<<2];
vector<int> G[maxn];
int cnt; void dfs(int u, int fa, int d){
dep[u] = d;
in[u] = ++cnt; for(int i = 0; i < G[u].size(); ++i){
int v = G[u][i];
if(v == fa) continue;
dfs(v, u, d+1);
}
out[u] = cnt;
} void push_down(int *sum, int rt, int *addv){
if(addv[rt] == 0) return ;
int l = rt<<1, r = rt<<1|1;
sum[l] += addv[rt];
sum[r] += addv[rt];
addv[l] += addv[rt];
addv[r] += addv[rt];
addv[rt] = 0;
} void update(int *sum, int *addv, int L, int R, int val, int l, int r, int rt){
if(L <= l && r <= R){
addv[rt] += val;
sum[rt] += val;
return ;
}
push_down(sum, rt, addv);
int m = l + r >> 1;
if(L <= m) update(sum, addv, L, R, val, lson);
if(R > m) update(sum, addv, L, R, val, rson);
} int query(int *sum, int *addv, int M, int l, int r, int rt){
if(l == r) return sum[rt];
push_down(sum, rt, addv);
int m = l + r >> 1;
if(M <= m) return query(sum, addv, M, lson);
return query(sum, addv, M, rson);
} int main(){
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; ++i) scanf("%d", a+i);
for(int i = 1; i < n; ++i){
int u, v;
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1, -1, 1);
while(m--){
int x, c, op;
scanf("%d %d", &op, &x);
int t = dep[x]&1;
if(op == 1){
scanf("%d", &c);
update(sum[t], addv[t], in[x], out[x], c, 1, n, 1);
if(in[x] < out[x])
update(sum[t^1], addv[t^1], in[x]+1, out[x], -c, 1, n, 1);
}
else printf("%d\n", query(sum[t], addv[t], in[x], 1, n, 1) + a[x]);
}
return 0;
}

  

CodeForces 384E Propagating tree (线段树+dfs)的更多相关文章

  1. codeforces 383C Propagating tree 线段树

    http://codeforces.com/problemset/problem/383/C 题目就是说,  给一棵树,将一个节点的值+val, 那么它的子节点都会-val, 子节点的子节点+val. ...

  2. Codeforces 383C Propagating tree, 线段树, 黑白染色思想

    按深度染色,奇深度的点存反权值. #include <bits/stdc++.h> using namespace std; vector <]; ],a[],s[],vis[],i ...

  3. Codeforces 383C . Propagating tree【树阵,dfs】

    标题效果: 有一棵树,有两种操作模式对本树:1:表示为(1 x val),在NOx加在节点上val,然后x每个节点加上儿子- val.给每个儿子一个儿子在一起-(- val),加到没有儿子为止.2:表 ...

  4. CF620E New Year Tree 线段树 dfs序

    luogu链接 题目大意: 有一个节点有颜色的树 操作1.修改子树的颜色 操作2.查询子树颜色的种类 注意,颜色种类小于60种 只有子树的操作,dfs序当然是最好的选择 dfs序列是什么,懒得讲了,自 ...

  5. CF620E New Year Tree 线段树+dfs序+bitset

    线段树维护 dfs 序是显然的. 暴力建 60 个线段树太慢,于是用 bitset 优化就好了 ~ code: #include <bits/stdc++.h> #define M 63 ...

  6. S - Query on a tree HDU - 3804 线段树+dfs序

    S - Query on a tree HDU - 3804   离散化+权值线段树 题目大意:给你一棵树,让你求这棵树上询问的点到根节点直接最大小于等于val的长度. 这个题目和之前写的那个给你一棵 ...

  7. HDU 5692 线段树+dfs序

    Snacks Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  8. Tsinsen A1505. 树(张闻涛) 倍增LCA,可持久化线段树,DFS序

    题目:http://www.tsinsen.com/A1505 A1505. 树(张闻涛) 时间限制:1.0s   内存限制:512.0MB    总提交次数:196   AC次数:65   平均分: ...

  9. 【XSY2534】【BZOJ4817】树点涂色 LCT 倍增 线段树 dfs序

    题目大意 ​ Bob有一棵\(n\)个点的有根树,其中\(1\)号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜 ...

随机推荐

  1. UVALive - 4270 Discrete Square Roots (扩展欧几里得)

    给出一组正整数$x,n,r$,使得$r^2\equiv x(mod\: n)$,求出所有满足该等式的$r$. 假设有另一个解$r'$满足条件,则有$r^2-r'^2=kn$ 因式分解,得$(r+r') ...

  2. Centos7 安装 MongoDB4.0

    目录 安装包下载 MongoDB安装 启动数据库 补充 小结 诚邀访问我的个人博客:我在马路边 更好的阅读体验点击查看原文:Centos7安装MongoDB4.0 原创博客,转载请注明出处 @ 由于项 ...

  3. [转]【鹅厂网事】全局精确流量调度新思路-HttpDNS服务详解

    小编:对于互联网,域名是访问的第一跳,而这一跳很多时候会“失足”,导致访问错误内容,失败连接等,让我们在互联网上畅游的爽快瞬间消失,而对于这关键的第一跳,鹅厂也在持续深入研究和思考对策,今天小编就邀请 ...

  4. Operating System-进程/线程内部通信-信号量、PV操作的实现和应用(解决哲学家进餐和生产者消费者问题)

    本文主要内容: 信号量的实现 利用信号量解决哲学家用餐问题 利用信号量解决生产者消费者问题 一.信号量的实现 1.1 信号量结构 typedef struct { int value; struct ...

  5. 最终还是选择了markdownpad2

    markdownpad2使用 最终 哈哈,最后还是选择了markdownpad2,经过探索才知道这个玩意多么好用. 点击,下载. 碰到的问题 1.win10出现HTML无法渲染得对话框 结果是,官网有 ...

  6. laravel vendor目录的安装

  7. binlog之一:binary log初探

    MySQL Binary Log也就是常说的bin-log,是mysql执行改动产生的二进制日志文件,其主要作用有两个: Replication(主从数据库):在master端开启binary log ...

  8. xargs 命令使用小记

    ls -1|xargs -t -i mv {} noncredit{}  注意: ls -1 是123的1,不是lmn的l

  9. 第 十五 课 Go 语言范围(Range)

    Go 语言中 range 关键字用于 for 循环中迭代数组(array).切片(slice).通道(channel)或集合(map)的元素 package main import "fmt ...

  10. spring--AOP--日志---demo1---bai

    AOP日志DEMO1: 实体类: package com.etc.entity; import org.aspectj.lang.annotation.Pointcut; public class U ...