线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间对应线段树中的一个叶结点。 使用线段树可以快速的查找某一个节点在若干条线段中出现的次数,时间复杂度为O(logN)。而未优化的空间复杂度为2N,实际应用时一般还要开4N的数组以免越界,因此有时需要离散化让空间压缩。
这个问题是约瑟夫问题的一个变种,可以用线段树解决。
This problem is a variant of the Josephus problem which can be solved with segment tree.
大意:排队买票问题,后面来的总是把原来的人替换掉,并且使得原来的人往后移,让你顺序输出他们代表的编号,用线段树来记录空格数。
For each test cases, output a single line of space-separated integers which are the values of people in the order they stand in the queue.
应用推荐