hdu3887 Counting Offspring
Counting Offspring
问你对于每个节点,它的子树上标号比它小的点有多少个
- /*
- 子树的问题,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的更多相关文章
- HDU3887 Counting Offspring [2017年6月计划 树上问题03]
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- Counting Offspring(hdu3887)
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu 3887 Counting Offspring dfs序+树状数组
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 3887 Counting Offspring(DFS序+树状数组)
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 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 ...
- HDU 3887:Counting Offspring(DFS序+树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...
- 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 ...
- 杭电 3887 Counting Offspring
根据上篇翻译的文章以及很多个帖子,都讲述了树状数组最基本的功能就是tree[i]保存的是位置i左边小于等于a[i]的数的个数. 这样也就可以解释代码中为什么有f[i]=getsum(sd[i-1])- ...
- HDU 3887 Counting Offspring (树状数组+人工模拟栈)
对这棵树DFS遍历一遍,同一节点入栈和出栈之间访问的节点就是这个节点的子树. 因此节点入栈时求一次 小于 i 的节点个数 和,出栈时求一次 小于 i 的节点个数 和,两次之差就是答案. PS.这题直接 ...
随机推荐
- python MLP 神经网络使用 MinMaxScaler 没有 StandardScaler效果好
MLP 64,2 preprocessing.MinMaxScaler().fit(X) test confusion_matrix:[[ ...
- JSP的一个增删改查例子和总结
总结的几点: 1.在jsp中注意<%! %>声明代码块中的变量只会在项目开始的时候第一次运行jsp的时候执行一遍,有点类似于java类中的static代码块,所以如果是会改变的值不应该声明 ...
- BEC listen and translation exercise 35
高中听力: At five o'clock, we have afternoon tea, but we don't have it in the kitchen. Father's Day is t ...
- list dict set comprehension 列表推导式 (字典推导式,集合推导式)
从一个list生成新的list [ word.upper() for word in 'hellO worlD!' ] 简单的语法,如果不用list comprehension, 则要用更长的代码. ...
- OpenCV-Python sift/surf特征匹配与显示
import cv2 import numpy as np def drawMatchesKnn_cv2(img1_gray,kp1,img2_gray,kp2,goodMatch): h1, w1 ...
- 【二叉查找树】04根据升序数组构造二叉查找树【Convert Sorted Array to Binary Search Tree】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个升序的数组,把他转换成一个 ...
- Mybatis generator配置文件及说明
项目采用sring mvc + mybatis 组合,这里简单介绍下mybatis的应用: 我的IDE是STS(Spring + Tool + Suite), 安装Mybatis Generator插 ...
- redis的五种数据类型及应用场景
前言 redis是用键值对的形式来保存数据,键类型只能是String,但是值类型可以有String.List.Hash.Set.Sorted Set五种,来满足不同场景的特定需求. 本博客中的示例不是 ...
- 安装DCOS,关于docker异常引发的调查
入门DCOS,刚开始安装,碰到了一个异常: Bind for 0.0.0.0:9000 failed: port is already allocated. 调试这个问题花费了好长时间,因为无法通过n ...
- python构造一个http请求
我们经常会用python来进行抓包,模拟登陆等等, 势必要构造http请求包. http的request通常有4个方法get,post,put,delete,分别对应于查询,更新,添加,删除.我们经常 ...