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 1201
18岁生日 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- libyuv编
下载代码(墙): git clone http://git.chromium.org/external/libyuv.git 用 make 编译(linux.mingw.cygwin): mak ...
- java.lang.RuntimeException: Method called after release()
主要引起是因為在 camera.stopPreview(); camera.release(); 前沒有將setPreviewCallback 設置為null, 解決情況: public void ...
- 3D MAX脚本教程1
本文转载自http://jiurong995294.blog.163.com/blog/static/195133243201192531546490/ 方便以后须要时候使用 为什么要学习3D MAX ...
- Mahout推荐算法ItemBased
Mahout推荐的ItemBased 一. 算法原理 (一) 基本的 下面的例子,参见图评分矩阵:表现user,归类为item. 图(1) 该算法的原理: 1. 计算Item之间的相似度. ...
- ORACLE单字符函数的函数
1. ASCII(C) 说明:返回C的首字符在ASCII码中相应的十进制 举例: SQL>SELECT ASCII('A') A,ASCII('a') B,ASCII( ...
- Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
之前一直用mybatis+mybatis-spring-1.1.1,系统升级mybatis使用后 mybatis-spring-1.2.2, 再其他配置均为改动的情况下执行出错: Property ' ...
- js 加载初始化日期
//初始化加载时间 $(function(){ var now = new Date(); var year = now.getFullYear(); //年 ...
- RH033读书笔记(9)-Lab 10 Understanding the Configuration Tools
Lab 10 Understanding the Configuration Tools Sequence 1: Configuring the Network with system-config- ...
- EL与JSTL注意事项汇总
EL使用表达式(5一个 问题) JSTL使用标签(5问题) 什么是EL.它可以用做? EL全名Expression Language在JSP使用页面 格公式${表达式} 样例${requestScop ...