题目链接:

http://codeforces.com/contest/740/problem/D

D. Alyona and a tree

time limit per test2 seconds
memory limit per test256 megabytes
#### 问题描述
> Alyona has a tree with n vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive integer, in the vertex i she wrote ai. Moreover, the girl wrote a positive integer to every edge of the tree (possibly, different integers on different edges).
>
> Let's define dist(v, u) as the sum of the integers written on the edges of the simple path from v to u.
>
> The vertex v controls the vertex u (v ≠ u) if and only if u is in the subtree of v and dist(v, u) ≤ au.
>
> Alyona wants to settle in some vertex. In order to do this, she wants to know for each vertex v what is the number of vertices u such that v controls u.
#### 输入
> The first line contains single integer n (1 ≤ n ≤ 2·105).
>
> The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the integers written in the vertices.
>
> The next (n - 1) lines contain two integers each. The i-th of these lines contains integers pi and wi (1 ≤ pi ≤ n, 1 ≤ wi ≤ 109) — the parent of the (i + 1)-th vertex in the tree and the number written on the edge between pi and (i + 1).
>
> It is guaranteed that the given graph is a tree.
#### 输出
> Print n integers — the i-th of these numbers should be equal to the number of vertices that the i-th vertex controls
####样例输入
> 5
> 2 5 1 4 6
> 1 7
> 1 1
> 3 5
> 3 6
####样例输出
> 1 0 1 0 0

题意

给你一颗点权为a[i],的带边权的有根树(树根为1),对于节点v和它的子节点u之间,我们称v控制了u当且仅当dis(v,u)<=a[u]的时候,现在让你求每个结点能控制的子节点的个数。

题解

二分+前缀和。

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=2e5+10;
int n;
int arr[maxn]; VPII G[maxn];
LL dep[maxn];
///ans维护的是前缀和,这里的前缀指的是从叶子到根的方向
int ans[maxn]; ///path维护当前的一条路径
vector<pair<LL,int> > path;
void dfs(int u){
///自己肯定能够的到自己
ans[u]++;
///二分找第一个dis(ancestor,u)>arr[u]既dep[u]-dep[ancestor]>arr[u]既dep[u]-arr[u]>dep[ancesotor];
int p=lower_bound(all(path),mkp(dep[u]-arr[u],-1))-path.begin()-1;
if(p>=0) ans[path[p].Y]--; path.pb(mkp(dep[u],u));
for(int i=0;i<G[u].sz();i++){
int v=G[u][i].X;
dep[v]=dep[u]+G[u][i].Y;
dfs(v);
ans[u]+=ans[v];
}
path.pop_back();
} int main(){
clr(ans,0);
scf("%d",&n);
for(int i=1;i<=n;i++) scf("%d",&arr[i]);
for(int v=2;v<=n;v++){
int u,w;
scf("%d%d",&u,&w);
G[u].pb(mkp(v,w));
} dep[1]=0;
dfs(1); for(int i=1;i<=n;i++){
prf("%d",ans[i]-1);
if(i==n) prf("\n");
else prf(" ");
} return 0;
} //end----------------------------------------------------------------------

树上倍增+前缀和

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=2e5+10;
const int maxm=22;
int n;
int arr[maxn];
VPII G[maxn]; ///anc[i][j]表示i节点的2^j的祖先
int anc[maxn][maxm];
int ans[maxn];
LL dep[maxn];
void dfs(int u,int f){
ans[u]++;
anc[u][0]=f;
for(int i=1;i<maxm;i++){
anc[u][i]=anc[anc[u][i-1]][i-1];
} ///树上倍增
int pos=u;
for(int i=maxm-1;i>=0;i--){
int tmp=anc[pos][i];
if(dep[u]-dep[tmp]<=arr[u]){
pos=tmp;
}
} pos=anc[pos][0];
ans[pos]--; for(int i=0;i<G[u].sz();i++){
int v=G[u][i].X;
dep[v]=dep[u]+G[u][i].Y;
dfs(v,u);
ans[u]+=ans[v];
}
} int main(){
clr(ans,0);
scf("%d",&n);
for(int i=1;i<=n;i++) scf("%d",&arr[i]);
for(int v=2;v<=n;v++){
int u,w;
scf("%d%d",&u,&w);
G[u].pb(mkp(v,w));
} dep[1]=0;
dfs(1,0); for(int i=1;i<=n;i++){
prf("%d",ans[i]-1);
if(i==n) prf("\n");
else prf(" ");
} return 0;
} //end-----------------------------------------------------------------------

Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想的更多相关文章

  1. 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 ...

  2. Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和

    B. Alyona and a tree 题目连接: http://codeforces.com/contest/739/problem/B Description Alyona has a tree ...

  3. Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs序+树状数组

    D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  4. Codeforces Round #358 (Div. 2) C. Alyona and the Tree 水题

    C. Alyona and the Tree 题目连接: http://www.codeforces.com/contest/682/problem/C Description Alyona deci ...

  5. Codeforces Round #358 (Div. 2) C. Alyona and the Tree dfs

    C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input standa ...

  6. Codeforces Round #358 (Div. 2)——C. Alyona and the Tree(树的DFS+逆向思维)

    C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input standa ...

  7. Codeforces Round #358 (Div. 2) C. Alyona and the Tree

    C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input standa ...

  8. Codeforces Round #381 (Div. 1) A. Alyona and mex 构造

    A. Alyona and mex 题目连接: http://codeforces.com/contest/739/problem/A Description Alyona's mother want ...

  9. Codeforces Round #381 (Div. 2)C. Alyona and mex(思维)

    C. Alyona and mex Problem Description: Alyona's mother wants to present an array of n non-negative i ...

随机推荐

  1. Virtual Box上安装CentOS7

    问题1:安装完没有桌面系统(Gnome或KDE)解答:安装的时候,软件选择为最小安装",更改该选择 问题2:开机提示Initial setup of CentOS Linux 7 (core ...

  2. NOIP2008 普及组T4 立体图 解题报告-S.B.S.(施工未完成)

    题目描述 小渊是个聪明的孩子,他经常会给周围的小朋友们将写自己认为有趣的内容.最近,他准备给小朋友们讲解立体图,请你帮他画出立体图. 小渊有一块面积为m*n的矩形区域,上面有m*n个边长为1的格子,每 ...

  3. HDU 1850 Being a Good Boy in Spring Festival

    此题先考虑第一种,5 7 9的情况,先手如果想赢,则必定要把异或值变为0,因为随便取,所以此处的异或指的是对堆中的石子数进行异或,而非异或其SG函数. 首先7^9=14,因为要异或为0,则5要变成14 ...

  4. 数据结构Java实现07----队列:顺序队列&顺序循环队列、链式队列、顺序优先队列

    一.队列的概念: 队列(简称作队,Queue)也是一种特殊的线性表,队列的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置插入和删除,而队列只允许在其一端进行插入操作在其 ...

  5. uGUI练习(四) Light UI

    练习目标 在我之前的文章 Unity 2D Sprite Lighting ,讲到在2D Sprite中可以使用灯光,非常高兴的是,在Unity的新UI系统中我们也可以使用灯光 步骤 1.创建一个Pa ...

  6. GitHub入门之二 参与一个项目编写

    接上文:大多数时候我们也需要把别人的代码进行整合和修改,而不是简单的修改,这时就需要对一个项目进行修改. 注意,本系列文章主要说明在github网站上的操作,更多高级操作请使用git控制台 一.for ...

  7. pycharm简单使用

    http://blog.csdn.net/chenggong2dm/article/details/9365437

  8. HBuilder打包ios应用

    1先安装itunes在电脑上 2,在HBuilder的工具栏上的"工具"选项卡上安装ios插件

  9. dos系统下mysql常用命令

    show table status;//查看所有表状态,通过这个命令可以得知表的创建时间和最后更新时间,以及该表是基表还是视图以及是什么表引擎等信息. show table status from d ...

  10. velocity .vm

    关于.vm 后缀的文件,是velocity的文件.velocity是基于java的一种页面模板引擎,支持#if #else #foreach等写法的前台文件.$link.contextPath是该引擎 ...