又是不带修改的区间第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. (转载)struts2的驱动模型

    模型驱动即ModelDriver,所谓ModelDriven,意思是直接把实体类当成页面数据的收集对象. 比如,有实体类User如下: public class User { private int ...

  2. Mac支付宝插件风波

    1.前言 首先我喜欢看一些创业的书,很多书里都会有马云的身影,马云也算是对我有一定的影响,从而我对淘宝也产生了一定的好感.但是关于这次插件事情,我对阿里产生了一些排斥的心里作用.我并不想吐槽淘宝,也不 ...

  3. ClickOnce证书签名

    打开Microsoft .NET Framework 的SDK命令提示,按以下步骤操作: 1.创建一个自我签署的X.509证书(.cer)和一个.pvk私钥文件,用到makecert工具,命令如下: ...

  4. ajax 设置Access-Control-Allow-Origin实现跨域访问

    ajax跨域访问是一个老问题了,解决方法很多,比较常用的是JSONP方法,JSONP方法是一种非官方方法,而且这种方法只支持GET方式,不如POST方式安全. 即使使用jquery的jsonp方法,t ...

  5. Windows Server 2008 R2 64bit兼容Chrome浏览器

    近日更换系统Windows Server 2008 R2 64bit系统,发现谷歌浏览器插件无法正常运行,终于找到如下解决方案: 打开桌面谷歌浏览器属性,将target目标 C:\Users\Admi ...

  6. Microsoft Virtual Academy 介绍

    Microsoft Virtual Academy 是微软的虚拟学院,会推出微软各个方面的一些教程 介绍一点有用的链接 http://www.microsoftvirtualacademy.com/e ...

  7. mariadb 10 多源复制(Multi-source replication) 业务使用场景分析,及使用方法

    mariadb 10 多源复制(Multi-source replication) 业务使用场景分析,及使用方法 官方mysql一个slave只能对应一个master,mariadb 10开始支持多源 ...

  8. Mininet VM设置笔记

    Mininet VM是为了加快Mininet安装,而且可以很容易在linux平台上运行. VM运行在Windows,Mac,Linux,通过VMware.VirtualBox,QEMU和KVM. 下载 ...

  9. PIGCMS提示“你的程序为盗版,非法授权,请联系QQ7530782或者8441010”的修复方法

    最近群里又有人发出来微信平台盗版源码这个问题求解决,其实我本人是一直支持正版的,大家有条件的还是购买正好为好,既然有人问我就顺便解决了下,其实很简单,再换个接口就好了,查看了一下是在\PigCms\L ...

  10. iOS 进阶 第四天(0329)

    0329 UIScrollView的常见属性及其解释 常见属性,如下图: 具体解释,如下图: 喜马拉雅设置的例子 代码: 效果