C. Propagating tree
2 seconds
256 megabytes
standard input
standard output
Iahub likes trees very much. Recently he discovered an interesting tree named propagating tree. The tree consists of n nodes numbered from 1 to n, each node i having an initial value ai. The root of the tree is node 1.
This tree has a special property: when a value val is added to a value of node i, the value -val is added to values of all the children of node i. Note that when you add value -val to a child of node i, you also add -(-val) to all children of the child of node i and so on. Look an example explanation to understand better how it works.
This tree supports two types of queries:
- "1 x val" — val is added to the value of node x;
- "2 x" — print the current value of node x.
In order to help Iahub understand the tree better, you must answer m queries of the preceding type.
The first line contains two integers n and m (1 ≤ n, m ≤ 200000). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000). Each of the next n–1 lines contains two integers vi and ui (1 ≤ vi, ui ≤ n), meaning that there is an edge between nodes vi and ui.
Each of the next m lines contains a query in the format described above. It is guaranteed that the following constraints hold for all queries: 1 ≤ x ≤ n, 1 ≤ val ≤ 1000.
For each query of type two (print the value of node x) you must print the answer to the query on a separate line. The queries must be answered in the order given in the input.
5 5
1 2 1 1 2
1 2
1 3
2 4
2 5
1 2 3
1 1 2
2 1
2 2
2 4
3
3
0
The values of the nodes are [1, 2, 1, 1, 2] at the beginning.
Then value 3 is added to node 2. It propagates and value -3 is added to it's sons, node 4 and node 5. Then it cannot propagate any more. So the values of the nodes are [1, 5, 1, - 2, - 1].
Then value 2 is added to node 1. It propagates and value -2 is added to it's sons, node 2 and node 3. From node 2 it propagates again, adding value 2 to it's sons, node 4 and node 5. Node 3 has no sons, so it cannot propagate from there. The values of the nodes are [3, 3, - 1, 0, 1].
You can see all the definitions about the tree at the following link: http://en.wikipedia.org/wiki/Tree_(graph_theory)
思路:dfs序+线段树;
首先dfs序映射一下,然后转换成然后线段树维护,新然后开两个数组,一个作为正一个作为负。
复杂度n×log(n);
1 #include<stdio.h>
2 #include<algorithm>
3 #include<queue>
4 #include<stdlib.h>
5 #include<iostream>
6 #include<string.h>
7 #include<set>
8 #include<map>
9 #include<vector>
10 using namespace std;
11 typedef long long LL;
12 int ans[200005];
13 int id[200005];
14 int a[200005];
15 typedef vector<int> Ve;
16 vector<Ve>vec(200005);
17 bool flag[200005];
18 int cn = 0;
19 int l[200005];
20 int r[200005];
21 void dfs(int n,int p);
22 int tree1[200005*4];
23 int tree2[4*200005];
24 void update(int x,int n,int c);
25 int ask(int x);
26 int jiou[200005];
27 void update(int l,int r,int k,int nn,int mm,int co,int p)
28 {
29 if(l > mm||r < nn)
30 {
31 return ;
32 }
33 else if(l <= nn&& r >= mm)
34 {
35 if(p%2)tree1[k]+=co;
36 else tree2[k]+=co;
37 return ;
38 }
39 else
40 {
41 update(l,r,2*k+1,nn,(nn+mm)/2,co,p);
42 update(l,r,2*k+2,(nn+mm)/2+1,mm,co,p);
43 }
44 }
45 int ask1(int l,int r,int k,int nn,int mm)
46 {
47 if(l > mm || r < nn)
48 return 0;
49 else if(l <= nn&&r >= mm)
50 {
51 return tree1[k];
52 }
53 else
54 {
55 tree1[2*k+1] += tree1[k];
56 tree1[2*k+2] += tree1[k];
57 tree1[k] = 0;
58 int nx = ask1(l,r,2*k+1,nn,(nn+mm)/2);
59 int ny = ask1(l,r,2*k+2,(nn+mm)/2+1,mm);
60 return nx + ny;
61 }
62 }
63 int ask2(int l,int r,int k,int nn,int mm)
64 {
65 if(l > mm || r < nn)
66 return 0;
67 else if(l <= nn&&r >= mm)
68 {
69 return tree2[k];
70 }
71 else
72 {
73 tree2[2*k+1] += tree2[k];
74 tree2[2*k+2] += tree2[k];
75 tree2[k] = 0;
76 int nx = ask2(l,r,2*k+1,nn,(nn+mm)/2);
77 int ny = ask2(l,r,2*k+2,(nn+mm)/2+1,mm);
78 return nx + ny;
79 }
80 }
81 int main(void)
82 {
83 int n,m;
84 scanf("%d %d",&n,&m);
85 for(int i = 1; i <= n; i++)
86 {
87 scanf("%d",&a[i]);
88 }
89 for(int i = 1; i < n; i++)
90 {
91 int x,y;
92 scanf("%d %d",&x,&y);
93 vec[x].push_back(y);
94 vec[y].push_back(x);
95 }
96 dfs(1,1);
97 for(int i = 1; i <= n; i++)
98 id[ans[i]] = i;
99 while(m--)
100 {
101 int val;
102 int co,ic;
103 scanf("%d %d",&val,&ic);
104 if(val == 1)
105 {
106 scanf("%d",&co);
107 update(l[ic],r[ic],0,1,cn,co,jiou[ic]);
108 }
109 else
110 {
111 int xx = ask1(id[ic],id[ic],0,1,cn);
112 int yy = ask2(id[ic],id[ic],0,1,cn);
113 //printf("%d\n",xx);
114 if(jiou[ic]%2)
115 {
116 printf("%d\n",xx-yy+a[ic]);
117 }
118 else printf("%d\n",yy-xx+a[ic]);
119 }
120 }
121 return 0;
122 }
123 void dfs(int n,int p)
124 {
125 flag[n] = true;
126 ans[++cn] = n;
127 l[n] = cn;
128 jiou[n] = p;
129 for(int i = 0; i < vec[n].size(); i++)
130 {
131 int x = vec[n][i];
132 if(!flag[x])
133 dfs(x,p+1);
134 }
135 r[n] = cn;
136 }
C. Propagating tree的更多相关文章
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #225 (Div. 2) E. Propagating tree dfs序+-线段树
题目链接:点击传送 E. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- AC日记——Propagating tree Codeforces 383c
C. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CodeForces 383C Propagating tree
Propagating tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- 题解 CF383C 【Propagating tree】
这道题明明没有省选难度啊,为什么就成紫题了QAQ 另:在CF上A了但是洛谷Remote Judge玄学爆零. 思路是DFS序+线段树. 首先这道题直观上可以对于每一次修改用DFS暴力O(n),然后对于 ...
- codeforces 383C Propagating tree 线段树
http://codeforces.com/problemset/problem/383/C 题目就是说, 给一棵树,将一个节点的值+val, 那么它的子节点都会-val, 子节点的子节点+val. ...
- Codeforces 383C . Propagating tree【树阵,dfs】
标题效果: 有一棵树,有两种操作模式对本树:1:表示为(1 x val),在NOx加在节点上val,然后x每个节点加上儿子- val.给每个儿子一个儿子在一起-(- val),加到没有儿子为止.2:表 ...
- CodeForces 384E Propagating tree (线段树+dfs)
题意:题意很简单么,给定n个点,m个询问的无向树(1为根),每个点的权值,有两种操作, 第一种:1 x v,表示把 x 结点加上v,然后把 x 的的子结点加上 -v,再把 x 的子结点的子结点加上 - ...
随机推荐
- Mysql-多个left join 计算逻辑
单个left join: (1)一对一:结果表的行数=左表行数 (2)一对多:结果表的行数>左表行数 多个left join: (0)多个left join由上到下,依次生成查询表,原理同单个l ...
- Telink BLE MESH PWM波的小结
本本针对Telink BLE MESH SDK 灯控的使用进行说明. 1.调整灯光的频率 默认情况下 SDK PWM波的频率是 600HZ的,有时我们需要将它调整频率,例如调整为4K,只需要更改参数 ...
- HDC2021技术分论坛:异构组网如何解决共享资源冲突?
作者:lijie,HarmonyOS软总线领域专家 相信大家对HarmonyOS的"超级终端"比较熟悉了.那么,您知道超级终端场景下的多种设备在不同环境下是如何组成一个网络的吗?这 ...
- java输入代码
import java.util.Scanner; public class Demo59 { public static void main(String[] args) { / ...
- Hadoop入门 概念
Hadoop是分布式系统基础架构,通常指Hadoop生态圈 主要解决 1.海量数据的存储 2.海量数据的分析计算 优势 高可靠性:Hadoop底层维护多个数据副本,即使Hadoop某个计算元素或存储出 ...
- HDFS01 概述
HDFS 概述 目录 HDFS 概述 HDFS的产生背景和定义 HDFS产生背景 HDFS定义 优缺点 优点 缺点 组成 NameNode DataNode Secondary NameNode(2n ...
- 扩展kmp 学习笔记
学习了一下这个较为冷门的知识,由于从日报开始看起,还是比较绕的-- 首先定义 \(Z\) 函数表示后缀 \(i\) 与整个串的 \(lcp\) 长度 一个比较好的理解于实现方式是类似于 \(manac ...
- Linux服务器---xopps
XOOPS XOOPS是一款用php制作的开源网站管理系统,可用于构建各种网络站点. 1.下载XOOPS软件(https://xoops.org/) 2.将XOOPS软件中的htdocs文件夹拷贝到a ...
- Vue 标签中的ref属性和refs
ref: ref 被用来给元素或子组件注册引用信息.引用信息将会注册在父组件的 $refs 对象上.如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素:如果用在子组件上,引用就指向组件. ...
- 关系型数据库和非关系型数据库区别、oracle与mysql的区别
一.关系型数据库 关系型数据库,是指采用了关系模型来组织数据的数据库. 关系模型是在1970年由IBM的研究员E.F.Codd博士首先提出的,在之后的几十年中,关系模型的概念得到了充分的发展并逐 ...