bzoj 4756: [Usaco2017 Jan]Promotion Counting
来源:互联网 发布:免费开源电商系统源码 编辑:程序博客网 时间:2024/05/22 04:44
→题目链接←
【想说的话】
没有写过树状数组...
这算是我 树状数组初体验 ?
【题解】
先按能力值从大到小排序,然后找到每个牛的名次
之后一边dfs,一边用树状数组统计
【代码】
#include<bits/stdc++.h>#define MAXN 100010using namespace std;inline int rd(){int x=0,y=1;char c=getchar();while(c<'0' || c>'9'){if(c=='-')y=-y;c=getchar();}while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return x*y;}struct node{int val,num;friend bool operator < (node a,node b){return a.val>b.val;}};int n,a[MAXN],sum[MAXN],ans[MAXN];node cow[MAXN];vector<int>v[MAXN];inline int lowbit(int x){return x&(-x);}inline void add(int x,int k){for(int i=x; i<=n; i+=lowbit(i))sum[i]+=k;}inline int ask(int x){int ret=0;for(int i=x; i>0; i-=lowbit(i))ret+=sum[i];return ret;}void dfs(int x){ans[x]-=ask(a[x]);for(int i=0; i<v[x].size(); i++)dfs(v[x][i]);ans[x]+=ask(a[x]);add(a[x],1);}int main(){n=rd();for(int i=1; i<=n; i++)cow[i].val=rd(),cow[i].num=i;for(int i=2; i<=n; i++)v[rd()].push_back(i);sort(cow+1,cow+n+1);for(int i=1; i<=n; i++)a[cow[i].num]=i;dfs(1);for(int i=1; i<=n; i++)printf("%d\n",ans[i]);return 0;}
阅读全文
0 0
- bzoj 4756: [Usaco2017 Jan]Promotion Counting
- 4756: [Usaco2017 Jan]Promotion Counting
- bzoj 4756: [Usaco2017 Jan]Promotion Counting (线段树合并)
- BZOJ 4756 [Usaco2017 Jan]Promotion Counting 线段树合并
- BZOJ 4756 [Usaco2017 Jan]Promotion Counting dfs序+主席树
- BZOJ[4756][Usaco2017 Jan]Promotion Counting 线段树合并
- BZOJ4756: [Usaco2017 Jan]Promotion Counting
- 【bzoj4756】[Usaco2017 Jan]Promotion Counting
- 【bzoj4756】[Usaco2017 Jan]Promotion Counting
- bzoj 4756: [Usaco2017 Jan]Promotion Counting dsu on tree+树状数组
- 4756: [Usaco2017 Jan]Promotion Counting 线段树合并
- bzoj4756[Usaco2017 Jan]Promotion Counting 树上主席树
- USACO2016 Jan Bronze 1:Promotion Counting
- USACO2016 Jan Bronze 4:Promotion Counting
- [DP] BZOJ 4758 [Usaco2017 Jan]Subsequence Reversal
- BZOJ 4756 Promotion Counting(线段树合并 || dfs)
- 【bzoj 4756】Promotion Counting(线段树合并)
- BZOJ 4758 [Usaco2017 Jan] 区间dp->最长不下降+翻转子序列
- 从《深入分析Java Web开发技术内幕》学到的东西
- 直方图
- 安卓最常用的框架,快速开发
- 299. Bulls and Cows
- Hive编程(十一)【其他文件格式和压缩方法】
- bzoj 4756: [Usaco2017 Jan]Promotion Counting
- 股票大盘常用指标
- RF-常用关键字-log
- js中typeof的用法
- GIT使用小结
- java的学习流程建议
- axure原型设计之抽屉列表
- 【 CDN 最佳实践】CDN 加速 OSS 常见问题及处理思路
- Android在原生集成react native