又是不带修改的区间第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. Ajax 技术二

    一.Ajax与XML案例 例:使用Ajax+XML读取数据表中的分类信息并放入下拉选框中 demo01.php 运行结果: 二.Ajax中的JSON 在Javascript中,可以通过两种方式(XML ...

  2. POJ 2837 Til the Cows Come Home

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45515   Accepted: 15434 Description Bes ...

  3. c++ string 拼接 int错误

    程序中用到字符串和int合成字符串,受java习惯的影响,直接进行了字符串与int的+操作,结果不正确.查了一下才明白问题所在,记录一下string str=”abc”+1;输出为:bc,因为”abc ...

  4. mysql数据库创建database(实例),和用户,并授权

    前言:mysql创建用户的方法分成三种:INSERT USER表的方法.CREATE USER的方法.GRANT的方法. 一.账号名称的构成方式 账号的组成方式:用户名+主机(所以可以出现重复的用户名 ...

  5. Ubuntu14.04 安装 PHP cURL

    今天遇到 Fatal error: Call to undefined function curl_init() in /xxx/xxxx/www/application/library/Ku/Htt ...

  6. 【全面解析DeepZoom 之三】建立DeepZoom应用

    文章出处:http://www.cnblogs.com/zhouyinhui/archive/2008/04/14/1153371.html (周银辉) 与导出整图不一样,你不能这样使用: <M ...

  7. Odoo 库存管理-库存移动(Stock Move)新玩法

    库存移动(Stock Move)新玩法 Odoo的库存移动不仅仅是存货在两个“存货地点”之间的移动的基本概念了,他们可以被“串联”在一起,可以用来生成或改变其对应的拣货单 (Picking).链式库存 ...

  8. 编写php拓展实例--slime项目(用户登录会话类)

      最近公司换了yaf框架,突然对用c实现php拓展感兴趣了,如果一个功能已经很稳定很成熟而且用的地方很多,那么我们就可以尝试用拓展实现(不一定每种情况都可以写成拓展),写成拓展后就不用每次用都包含一 ...

  9. c指针提高

    今天看了两章C语言,于是乎编段程序复习下.  还是不清楚这些神奇的东西的到底要干嘛用...  敲完后,显得这段代码高大上  但是,想实现这个程序,需要这么写的复杂吗?==||   #include & ...

  10. MATLAB plot画线的颜色设定

    plot中画线的颜色通常是八种: 标记符    颜色r          红g          绿b          蓝c          蓝绿m          紫红y          黄 ...