hdu3887 Counting Offspring】的更多相关文章

Counting Offspring HDU - 3887 问你对于每个节点,它的子树上标号比它小的点有多少个 /* 子树的问题,dfs序可以很轻松的解决,因为点在它的子树上,所以在线段树中,必定在它的两个时间戳的区间之间,所以我们只需要从小到大考虑,它的区间里有多少个点已经放了,然后再把它放进去.很容易的解决了 每行的最后一个数后面不要出输出多余的空格,否则会PE */ #include<iostream> #include<cstdio> #include<cstring…
Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2809    Accepted Submission(s): 981 Problem Description You are given a tree, it’s root is p, and the node is numbered from 1…
Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2759    Accepted Submission(s): 956 Problem Description You are given a tree, it's root is p, and the node is numbered from 1…
Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description You are given a tree, it’s root is p, and the node is numbered from 1 to n. Now define f(i) as the number of nodes whose numbe…
Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3054    Accepted Submission(s): 1031 Problem Description You are given a tree, it’s root is p, and the node is numbered from 1…
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1的边不一定的树,然后给出问题:询问区间和 或者 节点值更新. HDU 3887: 题意:和POJ 3321的题意差不多,只不过对每个节点询问不包含该节点的区间和 思路:今天才学了下才知道有DFS序这种东西,加上树状数组处理一下区间和 和 节点更新. DFS序大概就是我们在DFS遍历一棵树的时候,在进…
http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样的,DFS序+树状数组,一开始没想到,用了DFS序+排序,结果超时了.在in和out之间的时间戳是该节点子树的范围,从后往前扫,再删掉大的,这样可以满足值小于该节点的条件. #include <cstdio> #include <cstring> #include <cmath&…
这几个题练习DFS序的一些应用. 问题引入: 给定一颗n(n <= 10^5)个节点的有根树,每个节点标有权值,现有如下两种操作: 1.C x y     以节点x的权值修改为y. 2.Q x       求出以节点x为根的子树权值和. 最直观的做法, 枚举一个子树内所有节点的权值加和.但这种做法的每一次讯问的时间复杂度是O(n)的,很明显无法满足题目的需要,我们需要更优的解法. 我们考虑DFS序的另外一种形式,当访问到一个节点时记下当前的时间戳,我们设它为L[x],  结束访问一个节点时当前的…
根据上篇翻译的文章以及很多个帖子,都讲述了树状数组最基本的功能就是tree[i]保存的是位置i左边小于等于a[i]的数的个数. 这样也就可以解释代码中为什么有f[i]=getsum(sd[i-1])-getsum(st[i]))/2.因为getsum保存的就是左边比i小的数,注意因为序列是通过dfs求出的,因而每个节点都有进入和退出过程,也就是每个节点都出现了2次,比如说对于数4来说,有4个节点,假设3为顶点,边的关系是3-2-1,3-2-4,那么dfs扫描出的序列就是3,2,1,1,4,4,2…
对这棵树DFS遍历一遍,同一节点入栈和出栈之间访问的节点就是这个节点的子树. 因此节点入栈时求一次 小于 i 的节点个数 和,出栈时求一次 小于 i 的节点个数 和,两次之差就是答案. PS.这题直接DFS会爆栈,可以重新设置栈的大小 #pragma comment(linker,"/STACK:100000000,100000000") 也可以人工模拟栈,代码如下. #include <cstdio> #include <cstring> #include &…
http://acm.hdu.edu.cn/showproblem.php?pid=3887 [题意] 给定一棵树,给定这棵树的根 对于每个结点,统计子树中编号比他小的结点个数 编号从小到大一次输出 [思路] 从小到大处理每个结点,即统计当前结点的结果后,把当前结点插入到树状数组中 [AC] #include<bits/stdc++.h> using namespace std; typedef long long ll; ; *maxn; int n,rt; struct edge { in…
翻译 题目描述 给你一棵树,和它的树根 $P$,并且节点从 $1\rightarrow n$ 编号,现在定义 $f(i)$ 为 $i$ 的子树中,节点编号小于 $i$ 的节点的个数. 输入格式 有多组数据 (不超过 10 组),对于每组数据:第一行两个整数 $n,p$ $(n\le 10^5)$ 表示树有 $n$ 个节点,树根是 $p$.接下来的 $n-1$ 行,每行两个整数,代表一条树边.输入以两个零作为结束. 输出格式 对于每组测试数据,输出一行 $n$ 个整数 $f(1),f(2)....…
题意: N个点形成一棵树.给出根结点P还有树结构的信息. 输出每个点的F[i].F[i]:以i为根的所有子结点中编号比i小的数的个数. 0<n<=10^5 思路: 方法一:直接DFS,进入结点x时记录一下比x小的数的个数.出来x时记录一下比x小的数的个数.相减就是F[x].结合树状数组. 方法二:写下DFS序.对DFS序列建线段树.然后从小到大对结点进行插入.用线段树统计. 代码:(方法一) int const N=1e5+5; int n,p; vector<int> G[N];…
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2424 Accepted Submission(s): 838 Problem DescriptionYou are given a tree, it’s root is p, and the node is numbered from 1 to n. N…
题目链接 Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1757    Accepted Submission(s): 582 Problem Description You are given a tree, it’s root is p, and the node is numbered fr…
在上篇,我了解了基数的基本概念,现在进入Linear Counting算法的学习. 理解颇浅,还请大神指点! http://blog.codinglabs.org/articles/algorithms-for-cardinality-estimation-part-ii.html 它的基本处理方法和上篇中用bitmap统计的方法类似,但是最后要用到一个公式: 说明:m为bitmap总位数,u为0的个数,最后的结果为n的一个估计,且为最大似然估计(MLE). 那么问题来了,最大似然估计是什么东东…
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31474   Accepted: 15724 Description Due to recent rains, water has pooled in various places in Farmer John's field, which is repr…
ZOJ3944 People Counting ZOJ3939 The Lucky Week 1.PeopleConting 题意:照片上有很多个人,用矩阵里的字符表示.一个人如下: .O. /|\ (.) 占3*3格子,句号“.”为背景.没有两个人完全重合.有的人被挡住了一部分.问照片上有几个人. 题解: 先弄个常量把3*3人形存起来,然后6个部位依次找,比如现在找头,找到一个头,就把这个人删掉(找这个人的各个部位,如果在该部位位置的不是这个人的身体,就不删),删成句号,疯狂找就行了. 代码:…
#include <stdio.h> #include <malloc.h> #define MAX_STACK 10 ; // define the node of stack typedef struct node { int data; node *next; }*Snode; // define the stack typedef struct Stack{ Snode top; Snode bottom; }*NewStack; void creatStack(NewSt…
1004. Counting Leaves (30)   A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contains one test case. Each case starts with a line containing 0 < N < 100,…
Problem Figure 2. The Hamming distance between these two strings is 7. Mismatched symbols are colored red. Given two strings ss and tt of equal length, the Hamming distance between ss and tt, denoted dH(s,t)dH(s,t), is the number of corresponding sym…
Problem A string is simply an ordered collection of symbols selected from some alphabet and formed into a word; the length of a string is the number of symbols that it contains. An example of a length 21 DNA string (whose alphabet contains the symbol…
// uva 11401 Triangle Counting // // 题目大意: // // 求n范围内,任意选三个不同的数,能组成三角形的个数 // // 解题方法: // // 我们设三角巷的最长的长度是c(x),另外两边为y,z // 则由z + y > x得, x - y < z < x 当y = 1时,无解 // 当y = 2时,一个解,这样到y = x - 1 时 有 x - 2个 // 解,所以一共是0,1,2,3....x - 2,一共(x - 2) * (x - 1…
当我们在使用JSONKit处理数据时,直接将文件拉进项目往往会报这两个错“JSONKit   does not support Objective-C Automatic Reference Counting(ARC)”,“ARC forbids Objective-C objects in struct”,这是由于JSONKit库未更新,不支持ARC机制.我们可以参照如下步骤解决:…
有使用JSonKit的朋友,如果遇到“JSonKit does not support Objective-C Automatic Reference Counting(ARC)”这种情况,可参照如下方法: 点击项目根目录->targets->Build Phases->JSONKit.m->添加“-fno-objc-arc”字段,在运行就OK了.…
Lake Counting Time Limit: 1000MS     Memory Limit: 65536K Total Submissions: 17917     Accepted: 9069 Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <=…
Boring Counting Time Limit: 3000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述     In this problem you are given a number sequence P consisting of N integer and Pi is the ith element in the sequence. Now you task is to answer a list of queries, for each…
Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) Total Submission(s): 2811    Accepted Submission(s): 827 Problem Description In this problem we consider a rooted tree with N vertices. The vertices a…
Counting Squares Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1885    Accepted Submission(s): 946 Problem Description Your input is a series of rectangles, one per line. Each rectangle is sp…
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2610 Boring Counting Time Limit: 3000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述     In this problem you are given a number sequence P consisting of N integer and Pi is the ith ele…