Codeforces Gym F. Performance Review (树状数组 + DFS 序)
来源:互联网 发布:oppo手机怎么优化网络 编辑:程序博客网 时间:2024/06/05 20:38
Problem
给定一个有根树,每个点给定其父节点 fa ,自身的 rank ,以及自身的一个 value 值。任意一个点对其祖先产生贡献,当且仅当该点 rank 小于其祖先的 rank ,贡献度为该点的 value 值。问树上每个点的贡献值。
限制条件
1 ≤ E ≤ 100000
1 ≤ ri ≤ 100000
1 ≤ ti ≤ 100000
解题思路
考虑用树状数组单点更新每个 rank 产生的 value 值,区间查询 [0, rank-1]
的 value 和。
DFS 遍历每个点。结合 DFS 搜索的特性。统计编号为 i 号点的贡献值,可以优先将当前树状数组中区间 [0, rank-1]
的 value 和作为 pre 暂存(此处的 value 和不应对该点 i 产生贡献)。当 DFS 处理完以 i 点的为根的子树后,此时统计 [0, value-1]
的 value 和减掉 pre 即为所有 i 点的子孙节点 rank 比 i 点小的总贡献值。
代码
#include<bits/stdc++.h>using namespace std;const int E = 100000 + 10;int e;long long tree[E], pre[E], ans[E];struct Node { int to, r, t;} p;vector<Node> g[E];int lowbit(int x) { return x & -x; }void add(int r, int t) { for(int i=r;i<E;i+=lowbit(i)) tree[i] += t;}long long get(int r) { long long ret = 0; for(int i=r;i;i-=lowbit(i)) ret += tree[i]; return ret;}void dfs(int rt, int rnk) { pre[rt] = get(rnk-1); for(int i=0;i<g[rt].size();i++) { dfs(g[rt][i].to, g[rt][i].r); add(g[rt][i].r, g[rt][i].t); } ans[rt] = get(rnk-1) - pre[rt];}int main(){ scanf("%d", &e); int rt, rnk; for(int i=1, fa;i<=e;i++) { scanf("%d %d %d", &fa, &p.r, &p.t); if(fa == -1) { rt = i; rnk = p.r; continue; } p.to = i; g[fa].push_back(p); } dfs(rt, rnk); for(int i=1;i<=e;i++) printf("%lld\n", ans[i]);}
阅读全文
0 0
- Codeforces Gym F. Performance Review (树状数组 + DFS 序)
- Codeforces Gym F. Performance Review (树状数组 + DFS 序)
- Gym 101174F Performance Review 线段树合并
- Codeforces Round #300(E,F 树状数组)
- codeforces gym 101142 F(思维)
- codeforces Gym 101102F
- Codeforces-GYM-100726F
- Codeforces GYM 100803F
- Codeforces 570D TREE REQUESTS dfs序+树状数组
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
- codeforces contest 383 problem C(树状数组+dfs序)
- dfs序+树状数组
- Codeforces Round #225 (Div. 1) C-Propagating tree (DFS序+线段树/树状数组)
- CodeForces 396C 树状数组 + DFS
- 第八届福建省大学生程序设计竞赛-重现赛(F Problem 2277 Change)DFS序+树状数组
- Codeforces Gym 100589A Queries on the Tree(树状数组+分块)
- Codeforces Gym 101505D Orchard Division(离散化+树状数组+扫描线+二分)
- [REVIEW] 树状数组模板
- Android中小数处理(DecimalFormat)
- 用springmvc实现类似restful风格接口(接收和返回为xml格式数据)
- git的使用
- c语言课程设计-学生成绩管理系统
- SSH协议原理
- Codeforces Gym F. Performance Review (树状数组 + DFS 序)
- 回文串题目总结
- java 获取客户端用户真实ip
- LeetCode 561 : ArrayPartition I
- 将汉字转为字符串
- 【Swift】数据的存放
- nodeJs随笔
- 检查链表是否为回文
- 秒杀系统优化思路