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 ...
随机推荐
- Eclipse+Maven命令创建webapp项目<三>
1.使用maven命令:mvn archetype:create -DgroupId=xxxxx -DartifactId=web-sample -DarchetypeArtifactId=maven ...
- 将php分页类YII绑定框架,就需要改变风格的基础
分页类http://blog.csdn.net/buyingfei8888/article/details/40260127 在内部组件分页类 文件名和一致 组件是在什么地方被载入进来的?在主配置文件 ...
- springmvc+mongodb+maven 项目测试代码
你看我有一篇文章配置,或许还会有.mongodb性能测试结果.一个"快"字 源代码包,请留下邮箱 代码结构图 watermark/2/text/aHR0cDovL2Jsb2cuY3 ...
- 轮播图片 高效图片轮播,两个imageView实现
该轮播框架的优势: 文件少,代码简洁 不依赖任何其他第三方库,耦合度低 同时支持本地图片及网络图片 可修改分页控件位置,显示或隐藏 自定义分页控件的图片,就是这么个性 自带图片缓存,一次加载,永久使用 ...
- oracle表空间查询维护命令大全之三(暂时表空间)史上最全
--UNDO表空间汇总 --查看全部的表空间名字 SELECT NAME FROM V$TABLESPACE; --创建新的UNDO表空间,并设置自己主动扩展參数; CREATE UNDO TABLE ...
- Android 屏幕实现水龙头事件
在android下,事件的发生是在监听器下进行,android系统能够响应按键事件和触摸屏事件.事件说明例如以下: onClick(View v)一个普通的点击button事件 boolean onK ...
- 绕过电信访问Google
最近google被“DNS污染系统”攻击,导致域名无法正常跳转到解析IP,google业务无法访问,也无法使用google搜索,DNS域名污染系统攻击造成google本身故障的假象,针对此问题,一般都 ...
- RH033读书笔记(4)-Lab 5 File Permissions
Lab 5 File Permissions Sequence 1: Determining File Permissions 1. What is the symbolic representati ...
- 设计模式 - Abstract Factory模式(abstract factory pattern) 详细说明
Abstract Factory模式(abstract factory pattern) 详细说明 本文地址: http://blog.csdn.net/caroline_wendy/article/ ...
- hdu1325 Is It A Tree?并检查集合
pid=1325">职务地址 试想一下,在词和话题hdu1272是一样的. 可是hdu1272的博文中我也说了.数据比較水,所以我用非并查集的方法就AC了. 可是这题的数据没那么水,要 ...