hdu2665-Kth number
Kth number
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4585 Accepted Submission(s): 1461
For each test case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the sequence and the number of the quaere.
The second line contains n integers, describe the sequence.
Each of following m lines contains three integers s, t, k.
[s, t] indicates the interval and k indicates the kth big number in interval [s, t]
1
10 1
1 4 2 3 5 6 7 8 9 0
1 3 2
2
pid=2660" target="_blank">2660
2662 2667 2663 2661一開始用归并树 超时了, 仅仅好去学划分树= = 妈蛋,看了好久才理解。
划分树建树的原理和归并树有点类似。仅仅只是划分树採用的是高速排序的方式,先把原数列排序,建树的时候以中间大的数为基数。将小的扔左边,大的扔右边,有一些小细节,如有多个数与基数同样的时候,须要一个暂时变量记录一下,用seg[dep][i] 记录i到左边区间有 多少个数是小于基数的,查询的时候,推断区间 还有就
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 100000+10; struct node{
int lson,rson;
int mid(){
return (lson+rson)>>1;
}
}tree[maxn*4]; int seg[25][maxn];
int lftnum[25][maxn];
int num[maxn];
int n,m,sta,ed; void build(int L,int R,int rt,int dep){
tree[rt].lson = L;
tree[rt].rson = R;
if(L==R) return;
int mid = tree[rt].mid(),key = num[mid],scnt = mid-L+1;//左边中间值的个数
for(int i = L; i <= R ; i++){
if(seg[dep][i] < num[mid]){
scnt--;
}
}
int lp = L,rp = mid+1;
for(int i = L; i <= R; i++){
if(i==L){
lftnum[dep][i] = 0;
}else{
lftnum[dep][i] = lftnum[dep][i-1];
}
if(seg[dep][i] < key){
lftnum[dep][i]++;
seg[dep+1][lp++] = seg[dep][i];
}
else if(seg[dep][i] > key){
seg[dep+1][rp++] = seg[dep][i];
}
else{
if(scnt>0){
scnt--;
lftnum[dep][i]++;
seg[dep+1][lp++] = seg[dep][i];
}else{
seg[dep+1][rp++] = seg[dep][i];
}
}
}
build(L,mid,rt<<1,dep+1);
build(mid+1,R,rt<<1|1,dep+1);
}
int query(int L,int R,int rt,int dep,int k){
if(tree[rt].lson==tree[rt].rson){
return seg[dep][L];
}
int cnt,act;
if(L==tree[rt].lson){
cnt = lftnum[dep][R];
act = 0;
}else{
cnt = lftnum[dep][R] - lftnum[dep][L-1];
act = lftnum[dep][L-1];
}
int mid = tree[rt].mid();
if(cnt >= k){
L = tree[rt].lson + act;
R = tree[rt].lson + act + cnt-1;
return query(L,R,rt<<1,dep+1,k);
}else{
int a = L-tree[rt].lson-act;
int b = R-L-cnt+1;
L = mid + a + 1;
R = mid + a + b;
return query(L,R,rt<<1|1,dep+1,k-cnt);
} }
int main(){
int ncase;
cin >> ncase;
while(ncase--){
cin >> n >> m;
for(int i = 1; i <= n; i++){
scanf("%d",&num[i]);
seg[0][i] = num[i];
}
sort(num+1,num+n+1);
build(1,n,1,0);
while(m--){
int k;
scanf("%d%d%d",&sta,&ed,&k);
printf("%d\n",query(sta,ed,1,0,k));
}
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
hdu2665-Kth number的更多相关文章
- HDU2665 Kth number 【合并树】
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- [hdu2665]Kth number(划分树求区间第k大)
解题关键:划分树模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cs ...
- HDU2665 kth number 线段树做法
题意:求区间第k小 思路: 线段树 每个节点上保存 当前区间已经排序好的序列 (归并一下就好了嘛 复杂度 O(l)的) 这样建树的时空复杂度都是 O(nlogn)的 对于 每次询问 二分一个答案 在树 ...
- 【POJ2104】【HDU2665】K-th Number 主席树
[POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...
- POJ2104 K-th Number(主席树)
题目 Source http://poj.org/problem?id=2104 Description You are working for Macrohard company in data s ...
- POJ2104 K-th Number[主席树]【学习笔记】
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 51440 Accepted: 17594 Ca ...
- poj[2104]K-th Number
Description You are working for Macrohard company in data structures department. After failing your ...
- [划分树] POJ 2104 K-th Number
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 51732 Accepted: 17722 Ca ...
- poj 2104 K-th Number(可持久线段树)
K-th Number 持久化:http://www.cnblogs.com/tedzhao/archive/2008/11/12/1332112.html 结构:http://www.docin.c ...
- [POJ2104]K-th Number
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 34048 Accepted: 10810 Ca ...
随机推荐
- 使用windows-SQLyog连接linux-mysql
嘿嘿,最近又清闲了一点,重新安装了mysql去学习. -----博客园-邦邦酱好 系统环境: 1. 主机为windows系统,安装了SQLyog. 2. 主机上还安装了虚拟机,系统为c ...
- android系统reboot
这里所说的reboot指的是软件重启,并非断电重启.我们知道android系统的几个功能,比如:回复出厂设置.OTA升级等都需要重启系统,而且重启后要进入recovery模式,有的手机还带有重启进入f ...
- cocos2D-x 3.5 引擎解析之--引用计数(Ref),自己主动释放池(PoolManager),自己主动释放池管理器( AutoreleasePool)
#include <CCRef.h> Ref is used for reference count manangement. If a classinherits from Ref. C ...
- a标签的背景图在ie8下不显示的问题
突然发现临下班时候问题就多, 马上下班了被头头告知线上已经上线很久的活动现在有个兼容性问题, a标签的背景图在ie8下会有不显示的情况. 我自己找了台ie8的机器实验了一下, 发现一切正常, 但是在另 ...
- 意外地解决了一个WPF布局问题
原文:意外地解决了一个WPF布局问题 今天做了一个小测试,意外地将之前的一个困扰解决了,原问题见<WPF疑难杂症会诊>中的“怎么才能禁止内容撑大容器?” 以前我是在外侧嵌套Canvas容器 ...
- 二叉树的建立与遍历(山东理工OJ)
题目描写叙述 已知一个按先序序列输入的字符序列,如abc,,de,g,,f,,,(当中逗号表示空节点).请建立二叉树并按中序和后序方式遍历二叉树,最后求出叶子节点个数和二叉树深度. 输入 输入一个长度 ...
- 九度 题目1044:Pre-Post
转载请注明本文链接http://blog.csdn.net/yangnanhai93/article/details/40658571 题目链接:pid=1044">http://ac ...
- Oracle Data Provider for .NET 的使用经验
原文:Oracle Data Provider for .NET 的使用经验 Oracle Data Provider for .NET 是Oracle提供的基于Ado.net接口的一个开发包. ...
- 数学思想方法-分布式计算-linux/unix技术基础(5)
shell命令行参数 -bash-4.2$ cat test1.sh#!/bin/shecho "$0 "echo "$1 "echo "$2 ...
- 用数据说话,外贸产品选择(中篇)-google趋势分析法
在上篇文章<用数据说话,贸B2C产品选择(上篇)-热门搜索法>中我们能搜索出来几种产品了,那我们就拿上次搜索出来的热门产品来做一个趋势分析.我们经过几个站点挑出了几种热卖产品Wedding ...