Kth number

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 9213    Accepted Submission(s): 2868

Problem Description
Give you a sequence and ask you the kth big number of a inteval.
 
Input
The first line is the number of the test cases. 

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]
 
Output
For each test case, output m lines. Each line contains the kth big number.
 
Sample Input
1
10 1
1 4 2 3 5 6 7 8 9 0
1 3 2
 
Sample Output
2
这道题目网上的题解大多是划分树解法,其实求区间第K大还有一个方法就是可持久化线段树,
这里由于给的值可能是负数,必须离散化,而且离散化之后的线段树空间可以开的更小一点
防止内存超限
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <map> using namespace std;
const int maxn=1e5;
int rt[maxn+5];
int ls[maxn*18+5];
int rs[maxn*18+5];
int sum[maxn*18+5];
int b[maxn+5];
int a[maxn+5];
int n,m;
int l,r;
int p;
map<int,int> m1,m2;
void update(int &node,int l,int r,int val)
{
if(!node)
{
sum[p]=ls[p]=rs[p]=0;
node=p;
p++;
}
else
{
sum[p]=sum[node];ls[p]=ls[node];
rs[p]=rs[node];node=p;
p++;
}
if(l==r)
{
sum[node]++;
return;
}
sum[node]++;
int mid=(l+r)>>1;
if(val<=mid) update(ls[node],l,mid,val);
else update(rs[node],mid+1,r,val);
}
int query(int node1,int node2,int l,int r,int k)
{
if(sum[node2]-sum[node1]<k) return -1;
if(l==r) return l;
int mid=(l+r)>>1;
int num=sum[ls[node2]]-sum[ls[node1]];
if(num>=k)
return query(ls[node1],ls[node2],l,mid,k);
else
return query(rs[node1],rs[node2],mid+1,r,k-num);
}
int main()
{
int t;
scanf("%d",&t);
int s,e,k;
while(t--)
{
l=1e9;r=0;
m1.clear();
m2.clear();
scanf("%d%d",&n,&m);
p=1;
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
a[i]=b[i];
}
sort(b+1,b+n+1);
for(int i=1;i<=n;i++)
{
m1[b[i]]=i;
m2[i]=b[i];
}
l=1,r=n;
update(rt[1]=0,l,r,m1[a[1]]);
for(int i=2;i<=n;i++)
update(rt[i]=rt[i-1],l,r,m1[a[i]]);
int ans;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&s,&e,&k);
ans=query(rt[s-1],rt[e],l,r,k);
printf("%d\n",m2[ans]);
}
}
return 0;
}


 

HDU 2665 Kth number(可持续化线段树)的更多相关文章

  1. HDU 2665.Kth number-可持久化线段树(无修改区间第K小)模板 (POJ 2104.K-th Number 、洛谷 P3834 【模板】可持久化线段树 1(主席树)只是输入格式不一样,其他几乎都一样的)

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

  2. POJ 2104&HDU 2665 Kth number(主席树入门+离散化)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 50247   Accepted: 17101 Ca ...

  3. hdu 2665 Kth number(划分树)

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

  4. HDU 2665 Kth number(主席树静态区间第K大)题解

    题意:问你区间第k大是谁 思路:主席树就是可持久化线段树,他是由多个历史版本的权值线段树(不是普通线段树)组成的. 具体可以看q学姐的B站视频 代码: #include<cmath> #i ...

  5. 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )

    在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...

  6. hdu 2665 Kth number

    划分树 /* HDU 2665 Kth number 划分树 */ #include<stdio.h> #include<iostream> #include<strin ...

  7. HDU - 2665 Kth number 主席树/可持久化权值线段树

    题意 给一个数列,一些询问,问$[l,r]$中第$K$大的元素是哪一个 题解: 写法很多,主席树是最常用的一种之一 除此之外有:划分树,莫队分块,平衡树等 主席树的定义其实挺模糊, 一般认为就是可持久 ...

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

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

  9. HDU 2665 Kth number(划分树)

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

随机推荐

  1. atitit.Atitit. Gui控件and面板-----服务端控件 java struts的实现最佳实践

    atitit.Atitit.  Gui控件and面板-----服务端控件 java struts的实现最佳实践 1. 服务器控件的类别 1 1.1. 数据控件:该类控件可细分为两种类型:数据源控件和数 ...

  2. android线程控制UI更新(Handler 、post()、postDelayed()、postAtTime)

    依照以下的理解就是handler与ui线程有一定的关联能够由于更新界面仅仅能在主线程中全部更新界面的地方能够在接受消息的handleMessage那里还有更新界面能够在handler.port(new ...

  3. C++常函数

    常函数即在类的成员函数参数列表后放置const的函数,常函数的作用是限制函数体对成员变量的修改,此外,常函数也不能调用非 常函数. #include <iostream> using na ...

  4. nyoj16矩形嵌套(第一道dp关于dag的题目)

    http://acm.nyist.net/JudgeOnline/problem.php?pid=16 题意:有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c, ...

  5. alsamixer + alsactl 控制放音通道

    1 使用alsamixer的gui界面配置放音(控制OUT1,OUT2的音量); 2 退出alsamixer,使用alsactl  store生成配置文件,文件位于/etc/asound.state; ...

  6. http协议之报文详解

    一. 概述 用于HTTP协议交互的信息被称为HTTP报文.请求端(客户端)的http报文叫做请求报文,响应端的叫做响应报文. 报文,是网络中交换和传输的数据单元,即站点一次性要发送的数据块.报文包含了 ...

  7. lnmp服务器的目录信息

    LNMP状态管理命令: LNMP状态管理: /root/lnmp {start|stop|reload|restart|kill|status}Nginx状态管理:/etc/init.d/nginx ...

  8. Task Scheduling

    Introduction In the past, developers have generated a Cron entry for each task they need to schedule ...

  9. eclipse启动无响应,老是加载不了revert resources,或停留在Loading workbench状态

    做开发的同学们或多或少的都会遇到eclipse启动到一定程度时,就进入灰色无响应状态再也不动了.启动画面始终停留在Loading workbench状态.反复重启,状态依旧. 多数情况下,应该是非正常 ...

  10. LRU算法 - LRU Cache

    这个是比较经典的LRU(Least recently used,最近最少使用)算法,算法根据数据的历史访问记录来进行淘汰数据,其核心思想是“如果数据最近被访问过,那么将来被访问的几率也更高”. 一般应 ...