Codeforces 383C . Propagating tree【树阵,dfs】
标题效果:
做法:
比方说有例如以下一棵树:
(第一行为新数组下标。第二行为新数组存的节点序号)
遍历完之后,再对根节点的每一个儿子做一遍同样的操作就可以。(详细能够看代码)
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#define N 200020
using namespace std;
struct ee//存储须要改动哪些区间的结构体
{
int x1,y1,x2,y2;
}e[N];
vector<int> g[N];
int n,m,a[N],d[N],c[N],bef[N],index=1;
void dfs1(int x,int fa,int deep)//处理d[]数组
{
d[x]=deep;
for(int i=0;i<g[x].size();i++)
if(g[x][i]!=fa) dfs1(g[x][i],x,1-deep);
}
void dfs2(int x,int fa,int deep)//得到新数组
{
if(d[x]==deep) bef[x]=index++;//aft[index++]=x;//bef[i] 当中,i是原节点的编号,bef[i]是i在新数组中的下标
for(int i=0;i<g[x].size();i++)
if(g[x][i]!=fa) dfs2(g[x][i],x,deep);
}
void dfs3(int x,int fa)//预处理每一个点的属性
{
for(int i=0;i<g[x].size();i++)
if(g[x][i]!=fa) dfs3(g[x][i],x);
int ma1=bef[x],mi2=N,ma2=0;
for(int i=0;i<g[x].size();i++)
{
if(g[x][i]==fa) continue;
int cur=g[x][i];
mi2=min(mi2,e[cur].x1);
ma2=max(ma2,e[cur].y1);
ma1=max(ma1,e[cur].y2);
}
e[x].x1=bef[x],e[x].y1=ma1,e[x].x2=mi2,e[x].y2=ma2;//[x1,y1]为须要加值操作的区间,[x2,y2]为须要减值操作的区间,能够由儿子确定
}
int getnum(int x)//以下便是树状数组的区间改动,点查询函数咯~
{
int rnt=0;
for(int i=x;i<=n;i+=(i&(-i)))
{
rnt+=c[i];
}
return rnt;
}
void add(int i,int a)
{
while(i>=1)
{
c[i]+=a;
i-=(i&(-i));
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",a+i);
for(int i=0;i<n-1;i++)
{
int a,b;
scanf("%d%d",&a,&b);
g[a].push_back(b),g[b].push_back(a);
}
dfs1(1,0,1);//计算d[]数组
dfs2(1,0,1);//对根节点进行处理
for(int i=0;i<g[1].size();i++)
dfs2(g[1][i],1,0);//对根节点的每一个儿子进行处理
dfs3(1,0);//预处理 while(m--)
{
int ty;
scanf("%d",&ty);
if(ty==1)
{
int x,y;
scanf("%d%d",&x,&y);
int l1=e[x].x1,r1=e[x].y1,l2=e[x].x2,r2=e[x].y2;
add(r1,y),add(l1-1,-y);
if(r2!=0) add(r2,-y),add(l2-1,y);//假设不是根节点再进行减操作
}
else
{
int x;
scanf("%d",&x);
cout<<getnum(bef[x])+a[x]<<endl;
}
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
Codeforces 383C . Propagating tree【树阵,dfs】的更多相关文章
- CodeForces 383C Propagating tree
Propagating tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...
- codeforces 383C Propagating tree 线段树
http://codeforces.com/problemset/problem/383/C 题目就是说, 给一棵树,将一个节点的值+val, 那么它的子节点都会-val, 子节点的子节点+val. ...
- Codeforces 383C Propagating tree, 线段树, 黑白染色思想
按深度染色,奇深度的点存反权值. #include <bits/stdc++.h> using namespace std; vector <]; ],a[],s[],vis[],i ...
- CodeForces 384E Propagating tree (线段树+dfs)
题意:题意很简单么,给定n个点,m个询问的无向树(1为根),每个点的权值,有两种操作, 第一种:1 x v,表示把 x 结点加上v,然后把 x 的的子结点加上 -v,再把 x 的子结点的子结点加上 - ...
- Codeforces Round #381 (Div. 2)D. Alyona and a tree(树+二分+dfs)
D. Alyona and a tree Problem Description: Alyona has a tree with n vertices. The root of the tree is ...
- CodeForces 343D water tree(树链剖分)
Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...
- 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. 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 ...
随机推荐
- HDU 1856 More is better(并查集+离散化)
题目地址:HDU 1856 水题.因为标号范围太大,而数据数仅仅有10w,所以要先进行离散化.然后就是裸的并查集了. 代码例如以下: #include <iostream> #includ ...
- c++ primer 函数传值1
不看c++ primer 永远不知道自己基础有多差 函数的參数传值一般有两种方式:值传递,引用传递. 值传递有以下两种形式: void func( int a ) { // } void func1 ...
- IntelliJ IDEA 问题总结之中的一个 —— jar包、assets、maven、git
因为工作须要,这几天開始弃用eclipse,换idea.用了几天,idea确实有些地方比較方便.可是麻烦也是不少.并且网上相应的资料并没有eclipse那么多,非常多都是自己琢磨解决的,所以想弄个帖子 ...
- Git打补丁常见问题
Git打补丁常见问题 往往觉得得到某个功能的补丁就觉得这个功能我就已经成功拥有了,可是在最后一步的打补丁的工作也是须要相当慎重的,甚至有可能还要比你获取这个补丁花费的时间还要多.看到好多同行遇到这个问 ...
- 在Windows基础上(硬盘)安装Linux操作系统(CentOS/RedHat)
注:该方法安装CentOS ,RedHat均没有问题,其它Linux操作系统,没有尝试过. 0.创建一个fat32的盘.我分了8G给这个盘,盘符为F.F盘以后的内存所有删除,作为未分配的内存.这个留用 ...
- MVC5+EF6 入门完整教程 总目录
本系列文章会从一个主干开始,逐渐深入,初步规划30篇.初级10篇,中级10篇,综合项目实战10篇 初级10篇 MVC5+EF6 入门完整教程10:多对多关联表更新&使用原生SQL@201505 ...
- ASP.NET自定义控件组件开发 第四章 组合控件开发CompositeControl 后篇 --事件冒泡
原文:ASP.NET自定义控件组件开发 第四章 组合控件开发CompositeControl 后篇 --事件冒泡 CompositeControl 后篇 --事件冒泡 系列文章链接: ASP.NET ...
- Linux课程_系统配置和日常维护
1.设置命令输入提示格公式:"username:当前文件夹$" 2.设置命令输入提示行格式为:"当前系统时间-用户#"(提示:Shell将通过反引號" ...
- SqlServer中存储过程中将Exec的执行结果赋值给变量输出
原文 SqlServer中存储过程中将Exec的执行结果赋值给变量输出 背景: 遇到这样一种情况:动态向存储过程中传入表名和表的某些属性(这里用到的是主键ID),然后利用这两个变量查出一条数据的某些字 ...
- 【Java基础】System.arraycopy()的使用详解
由于在Java中System.arraycopy()方法在一维数组和二维数组中的表现不同,所以做了一个测试 public static void main(String[] args) { int[] ...