Counting Offspring

HDU - 3887

问你对于每个节点,它的子树上标号比它小的点有多少个

/*
子树的问题,dfs序可以很轻松的解决,因为点在它的子树上,所以在线段树中,必定在它的两个时间戳的区间之间,所以我们只需要从小到大考虑,它的区间里有多少个点已经放了,然后再把它放进去。很容易的解决了
每行的最后一个数后面不要出输出多余的空格,否则会PE
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 100010
using namespace std;
int n,p,num,head[maxn],xu[maxn],sz[maxn],opx,opl,opr;
struct node{int to,pre;}e[maxn*];
struct Node{int l,r,v;}tr[maxn<<];
void Insert(int from,int to){
e[++num].to=to;
e[num].pre=head[from];
head[from]=num;
}
void build(int l,int r,int k){
tr[k].l=l;tr[k].r=r;tr[k].v=;
if(l==r)return;
int mid=(l+r)>>;
build(l,mid,k<<);
build(mid+,r,k<<|);
}
int id;
void dfs(int now,int father){
xu[now]=++id;
sz[now]=;
for(int i=head[now];i;i=e[i].pre){
int to=e[i].to;
if(to==father)continue;
dfs(to,now);
sz[now]+=sz[to];
}
}
int query(int l,int r,int k){
if(l>=opl&&r<=opr){return tr[k].v;}
int mid=(l+r)>>;
int res=;
if(opl<=mid)res+=query(l,mid,k<<);
if(opr>mid)res+=query(mid+,r,k<<|);
return res;
}
void change(int l,int r,int k){
if(l==r){tr[k].v++;return;}
int mid=(l+r)>>;
if(opx<=mid)change(l,mid,k<<);
else change(mid+,r,k<<|);
tr[k].v=tr[k<<].v+tr[k<<|].v;
}
int main(){
freopen("Cola.txt","r",stdin);
while(){
scanf("%d%d",&n,&p);
if(n==&&p==)return ;
memset(head,,sizeof(head));
memset(e,,sizeof(e));num=;
id=;build(,n,);
int x,y;
for(int i=;i<n;i++){
scanf("%d%d",&x,&y);
Insert(x,y);Insert(y,x);
}
dfs(p,);
for(int i=;i<=n;i++){
opl=xu[i],opr=xu[i]+sz[i]-;
if(i!=n)printf("%d ",query(,n,));
else printf("%d",query(,n,));
opx=xu[i];
change(,n,);
}
puts("");
}
}

hdu3887 Counting Offspring的更多相关文章

  1. HDU3887 Counting Offspring [2017年6月计划 树上问题03]

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  2. Counting Offspring(hdu3887)

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  3. hdu 3887 Counting Offspring dfs序+树状数组

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  4. HDU 3887 Counting Offspring(DFS序+树状数组)

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  5. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  6. HDU 3887:Counting Offspring(DFS序+树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...

  7. Hdu 3887 Counting Offspring \ Poj 3321 Apple Tree \BZOJ 1103 [POI2007]大都市meg

    这几个题练习DFS序的一些应用. 问题引入: 给定一颗n(n <= 10^5)个节点的有根树,每个节点标有权值,现有如下两种操作: 1.C x y     以节点x的权值修改为y. 2.Q x ...

  8. 杭电 3887 Counting Offspring

    根据上篇翻译的文章以及很多个帖子,都讲述了树状数组最基本的功能就是tree[i]保存的是位置i左边小于等于a[i]的数的个数. 这样也就可以解释代码中为什么有f[i]=getsum(sd[i-1])- ...

  9. HDU 3887 Counting Offspring (树状数组+人工模拟栈)

    对这棵树DFS遍历一遍,同一节点入栈和出栈之间访问的节点就是这个节点的子树. 因此节点入栈时求一次 小于 i 的节点个数 和,出栈时求一次 小于 i 的节点个数 和,两次之差就是答案. PS.这题直接 ...

随机推荐

  1. elasticsearch的store属性跟_source字段——如果你的文档长度很长,存储了_source,从_source中获取field的代价很大,你可以显式的将某些field的store属性设置为yes,否则设置为no

    转自:http://kangrui.iteye.com/blog/2262860 众所周知_source字段存储的是索引的原始内容,那store属性的设置是为何呢?es为什么要把store的默认取值设 ...

  2. python生成excel格式座位表

    脚本分两个文件: 1.生成二维随机列表:GenerateLocaltion.py 2.将列表导入excel文件:CreateExcel.py 先上GenerateLocaltion.py: impor ...

  3. hdu-5583 Kingdom of Black and White(数学,贪心,暴力)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5583 Kingdom of Black and White Time Limit: 2000/1000 ...

  4. Mybatis generator配置文件及说明

    项目采用sring mvc + mybatis 组合,这里简单介绍下mybatis的应用: 我的IDE是STS(Spring + Tool + Suite), 安装Mybatis Generator插 ...

  5. ACM学习历程—CodeForces 590A Median Smoothing(分类讨论 && 数学)

    题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给一个串,头和尾每次变换保持不变. 中间的a[i]变成a[i-1],a[i],a[i+ ...

  6. centos 6.7 (UDEV,、etc/hosts)安装 RAC 11.2.0.4 报错处理

    环境说明: ​db 11.2.0.4        os: centos 6.7    UDEV管理ASM      没有配置DNS服务器,采用/etc/hosts​报错: ​1.    CVU(Cl ...

  7. docker异常处理

    Error running DeleteDevice dm_task_run failed重启docker即可 Error starting daemon: error initializing gr ...

  8. zabbix发送邮件

    1.zabbix服务器上已安装postfix邮件服务,如果没安装用yum安装sendmail也可以(简单) 2.vim /etc/mail.rc 在此配置中加上用户名及密码等,即可用这个账号发送邮件 ...

  9. python构造一个http请求

    我们经常会用python来进行抓包,模拟登陆等等, 势必要构造http请求包. http的request通常有4个方法get,post,put,delete,分别对应于查询,更新,添加,删除.我们经常 ...

  10. EXPLAIN 命令

    MySQL EXPLAIN 命令详解 MySQL的EXPLAIN命令用于SQL语句的查询执行计划(QEP).这条命令的输出结果能够让我们了解MySQL 优化器是如何执行SQL 语句的.这条命令并没有提 ...