又是不带修改的区间第k大,这次用的是一个不同的方法,划分树,划分树感觉上是模拟了快速排序的过程,依照pivot不断地往下划分,然后每一层多存一个toleft[i]数组,就可以知道在这一层里从0到i里有多少个被划分到了左子树,知道区间有多少个被分到左子树,就可以一路递归下去,不需要像函数式线段数一样,二分再加query,所以每次询问的复杂度也只是O(nlogn),空间复杂度的话就是O(nlogn),具体的介绍很多个链接都有,具体看下面给出的两个链接,它们对我起到非常大的帮助。

http://blog.csdn.net/famousdt/article/details/7064866

http://www.cnblogs.com/pony1993/archive/2012/07/17/2594544.html

写的时候尤其是递归询问区间的时候很容易出现off-by-one error,我在纸上比划了很久才搞清楚的。有点微弱呀- -0下面是代码。800多ms,是函数式线段数的时间的一半吧

#pragma warning(disable:4996)
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#define maxn 100000
using namespace std; int tree[22][maxn + 50];
int toleft[22][maxn + 50];
int as[maxn + 50];
int n, q; void build(int l, int r, int dep)
{
if (l == r) return;
int mid = (l + r) >> 1;
int same = mid - l + 1;
for (int i = l; i <= r; i++){
if (tree[dep][i] < as[mid]){
same--;
}
}
int ls = l;
int rs = mid + 1;
for (int i = l; i <= r; i++){
if (tree[dep][i] < as[mid]) tree[dep + 1][ls++] = tree[dep][i];
else if (tree[dep][i] == as[mid] && same) tree[dep + 1][ls++] = tree[dep][i], same--;
else tree[dep + 1][rs++] = tree[dep][i];
toleft[dep][i] = toleft[dep][l - 1] + ls - l;
}
build(l, mid, dep + 1);
build(mid + 1, r, dep + 1);
} int query(int left, int right, int k, int L, int R, int dep)
{
if (left == right) return tree[dep][left];
int mid = (L + R) >> 1;
int x = toleft[dep][left - 1] - toleft[dep][L - 1];
int y = toleft[dep][right] - toleft[dep][L - 1];
int rx = left - 1 - L + 1 - x;
int ry = right - L + 1 - y;
int cnt = y - x;
if (cnt >= k) return query(L + x, L + y - 1, k, L, mid, dep + 1);
// 注意off-by-one error.
else return query(mid + 1 + rx,mid + 1 + ry - 1, k - cnt, mid + 1, R, dep + 1);
} int main()
{
while (cin >> n >> q)
{
for (int i = 1; i <= n; i++){
scanf("%d", as + i);
tree[0][i] = as[i];
}
sort(as + 1, as + n + 1);
build(1, n, 0);
int li, ri, ki;
for (int i = 0; i < q; i++){
scanf("%d%d%d", &li, &ri, &ki);
printf("%d\n", query(li, ri, ki, 1, n, 0));
}
}
return 0;
}

POJ2104 k-th number 划分树的更多相关文章

  1. poj 2104 K-th Number 划分树,主席树讲解

    K-th Number Input The first line of the input file contains n --- the size of the array, and m --- t ...

  2. 静态区间第k大(划分树)

    POJ 2104为例[经典划分树问题] 思想: 利用快速排序思想, 建树时将区间内的值与区间中值相比,小于则放入左子树,大于则放入右子树,如果相等则放入左子树直到放满区间一半. 查询时,在建树过程中利 ...

  3. [NBUT 1458 Teemo]区间第k大问题,划分树

    裸的区间第k大问题,划分树搞起. #pragma comment(linker, "/STACK:10240000") #include <map> #include ...

  4. [hdu2665]Kth number(划分树求区间第k大)

    解题关键:划分树模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cs ...

  5. hdu 2665 Kth number(划分树模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=2665 [ poj 2104 2761 ]  改变一下输入就可以过 http://poj.org/problem? ...

  6. HDU 2665 Kth number(划分树)

    Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...

  7. HDU-2665-Kth number(划分树)

    Problem Description Give you a sequence and ask you the kth big number of a inteval.   Input The fir ...

  8. poj 2104 K-th Number (划分树入门 或者 主席树入门)

    题意:给n个数,m次询问,每次询问L到R中第k小的数是哪个 算法1:划分树 #include<cstdio> #include<cstring> #include<alg ...

  9. POJ 2104 K-th Number(划分树)

    题目链接 参考HH大神的模版.对其中一些转移,还没想清楚,大体明白上是怎么回事了,划分树就是类似快排,但有点点区别的.多做几个题,慢慢理解. #include <cstdio> #incl ...

随机推荐

  1. 洛谷 P3374 【模板】树状数组 1

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. ...

  2. transport

    #include<iostream> using namespace std; int transport(int a) { ; ) ; else a=a/; d=; ) { a=a*+; ...

  3. template_20_实现智能指针

    一:计数器二:析构和释放三:SmartPoint

  4. cadence16.3破解方法

    今天安装cadence16.3,安装了两遍都失败了,百思不得其解,结果总是出现在license上,如下图所示: 后面当然就只能启动demo版了,于是网上找,参照以前古老的法子,终于解决问题! 首先,开 ...

  5. c# 取得ip地址和网关

    /// <summary> /// 得到本机IP /// </summary> private string GetLocalIP() { //本机IP地址 string st ...

  6. ASP.NET MVC 表单的几种提交方式

    下面是总结一下在ASP.NET MVC中表单的几种提交方式. 1.Ajax提交表单 需要引用 <script type="text/javascript" src=" ...

  7. WPF 绑定三(绑定List中指定的字符串)

    xaml: <Window x:Class="WpfApplication1.Window3" xmlns="http://schemas.microsoft.co ...

  8. 基于CSS3新属性Animation及transform实现类似翻书效果

    注:本实例JS部分均以原生JS编写,不善用原生JS的,可用jQuery等对三方框架改写 先上效果图:(样式有点丑,可以忽略一下下,效果出来了就好,后期加到其他项目中方便更改0.0) 类似翻书效果,原本 ...

  9. Oracle 摘去数据块的面纱

    Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F 00018000h 6 A2 0 0 0c 0 80 3 8b 61 15 0 0 0 3 4 type frmt spa ...

  10. MvvmCross for WPF File Plugin

    本文以MvvmCross为框架基础 最近用了File Plugin插件,一开始也是没用明白,写一下记录下来,也方便需要的人吧 首先这个File Plugin需要先在UI项目里创建一个Bootstrap ...