题意:

查询区间第\(k\)大

分析:

之前是用主席树做的,现在学一下划分树。

学习链接

划分树的空间复杂度为\(O(nlogn)\),这点比主席树更优。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;const int maxn = 100000 + 10;
const int maxd = 20;int n, m;
int sorted[maxn];
int T[maxd][maxn], num[maxd][maxn];void build(int d, int L, int R) {int M = (L + R) / 2;int lsame = M - L + 1;for(int i = L; i <= R; i++) if(T[d][i] < sorted[M]) lsame--;int lpos = L, rpos = M+1;for(int i = L; i <= R; i++) {if(i == L) num[d][i] = 0;else num[d][i] = num[d][i - 1];if(T[d][i] < sorted[M] || (T[d][i] == sorted[M] && lsame)) {T[d+1][lpos++] = T[d][i];num[d][i]++;if(T[d][i] == sorted[M]) lsame--;} else {T[d+1][rpos++] = T[d][i];}}if(L < M) build(d + 1, L, M);if(M+1 < R) build(d + 1, M+1, R);
}int query(int d, int L, int R, int qL, int qR, int k) {if(L == R) return T[d][L];int M = (L + R) / 2;int cntl;if(L == qL) cntl = 0;else cntl = num[d][qL - 1];int cntr = num[d][qR];int cnt = cntr - cntl;if(cnt >= k) return query(d + 1, L, M, L + cntl, L + cntr - 1, k);else {cntl = qL - L - cntl;cntr = qR - L + 1 - cntr;return query(d + 1, M+1, R, M+1+cntl, M+cntr, k - cnt);}
}int main()
{scanf("%d%d", &n, &m);for(int i = 1; i <= n; i++) {scanf("%d", sorted + i);T[0][i] = sorted[i];}sort(sorted + 1, sorted + 1 + n);build(0, 1, n);while(m--) {int l, r, k; scanf("%d%d%d", &l, &r, &k);printf("%d\n", query(0, 1, n, l, r, k));}return 0;
}

转载于:https://www.cnblogs.com/AOQNRMGYXLMV/p/5346124.html

POJ 2104 K-th Number 划分树相关推荐

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

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

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

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

  3. hdu 2665 Kth number 划分树

    求区间第k大元素的值, 看代码的注释. #include<cstring> #include<cstdio> #include<iostream> #include ...

  4. 【转】POJ-2104(K-th Number 划分树)

    [题目描述]有n个数字排成一列,有m个询问,格式为:left right k 即问在区间[left,right]第k大的数据为多少?建图: 建树的过程比较简单,对于区间[l,r],首先通过对原数组的排 ...

  5. POJ 2104 K-th Number(区间第k大数)(平方切割,归并树,划分树)

    题目链接: http://poj.org/problem? id=2104 解题思路: 由于查询的个数m非常大.朴素的求法无法在规定时间内求解. 因此应该选用合理的方式维护数据来做到高效地查询. 假设 ...

  6. 【划分树】 POJ 2104 HDU 2665 K-th Number 裸题

    了解了.... #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ma ...

  7. POJ 2104 划分树

    划分树:查询区间第K大 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #includ ...

  8. 划分树基础 —— HDU 2665 Kth number

    对应 HDU 题目 :点击打开链接 Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  9. 【转】POJ 2104 K-th Number(2)

    转自: http://huangwei.host7.meyu.net/?p=24 题意:给定序列,求出[s,t]区间内第k小的数字. 解法:线段树+归并排序+二分枚举 Whu回来后,开始好好看了下其余 ...

最新文章

  1. 帝国重新生成html,帝国CMS实现提交评论后自动重新生成内容页
  2. cpc卡内计费信息异常包括_抖音信息流落地页直达广告怎么做?
  3. 【Android 性能优化】应用启动优化 ( 启动优化项目 | 界面启动时间 | 启动优化项目 | 方法追踪 MethodTracing )
  4. 基于异或,取反和循环移位实现一个简单的加密解密函数
  5. Nginx(三):负载均衡策略 与 Nginx静态服务器
  6. 浮动在IE6,7下的一些问题
  7. .Net 开源项目资源大全
  8. bzoj 3631: [JLOI2014]松鼠的新家(LCA+树上差分)
  9. 【轨迹预测】基于matlab卡尔曼滤波运动轨迹预测【含Matlab源码 590期】
  10. php后缀加swp,注意发现交换文件 filename.swp
  11. OpenLayers 官网例子的中文详解 1
  12. ABBYY FineReader 12 破解版(附注册码)
  13. 计算机的桌面图标设置在哪里,电脑小图标怎么设置在哪里
  14. 微信小程序 - 点击事件传递参数(简单详细)
  15. 恶补地理知识--四大洋,七大洲
  16. 揪心!河南极端暴雨突袭,多地受灾严重!加油,河南
  17. Docker - 编译安装nginx镜像
  18. 主元素、主元素II、主元素III
  19. Excel 2010 VBA 入门 139 创建工作簿级的自定义选项卡
  20. 什么才算好的监控系统?

热门文章

  1. c语言修仙角色,C语言修仙手游-C语言修仙手游安卓版V7.4.0预约_第一手游网
  2. 弹窗页面PHP代码不执行,PHP代码没有被执行,而是代码显示在页面上
  3. 飞桨模型保存_重磅发布开源框架、生物计算平台螺旋桨,百度飞桨交了年终成绩单...
  4. Python处理多行文本问题--一个简单方法读取多行fasta文件
  5. RecyclerView的基本用法
  6. Linux下Tomcat的启动、关闭、杀死进程
  7. Android之系统自带的文字外观设置
  8. [译] PHP7 数组:HashTable
  9. Prim算法和Kruskal算法求最小生成树
  10. Python爬虫入门三之Urllib库的基本使用