Kth number

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

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
 
Source
思路:主席树板子题;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=;
struct Chairmantree
{
int rt[N*],ls[N*],rs[N*],sum[N*];
int tot;
void init()
{
tot=;
}
void build(int l,int r,int &pos)
{
pos=++tot;
sum[pos]=;
if(l==r)return;
int mid=(l+r)>>;
build(l,mid,ls[pos]);
build(mid+,r,rs[pos]);
}
void update(int p,int c,int pre,int l,int r,int &pos)
{
pos=++tot;
ls[pos]=ls[pre];
rs[pos]=rs[pre];
sum[pos]=sum[pre]+c;
if(l==r)return;
int mid=(l+r)>>;
if(p<=mid)
update(p,c,ls[pre],l,mid,ls[pos]);
else
update(p,c,rs[pre],mid+,r,rs[pos]);
}
int query(int L,int R,int l,int r,int k)
{
if(l==r)return l;
int mid=(l+r)>>;
int num=sum[ls[R]]-sum[ls[L]];
if(k<=num)return query(ls[L],ls[R],l,mid,k);
else return query(rs[L],rs[R],mid+,r,k-num);
}
};
Chairmantree tree;
int a[N],b[N];
int getpos(int x,int cnt)
{
int pos=lower_bound(b+,b+cnt,x)-b;
return pos;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b++n);
int num=unique(b+,b++n)-b;
tree.init();
tree.build(,num-,tree.rt[]);
for(int i=;i<=n;i++)
{
int p=getpos(a[i],num);
tree.update(p,,tree.rt[i-],,num-,tree.rt[i]);
}
//for(int i=1;i<num;i++)
// printf("%d ",tree.rt[i]);
//printf("\n");
while(m--)
{
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",b[tree.query(tree.rt[l-],tree.rt[r],,num-,k)]);
}
}
return ;
}

hdu 2665 Kth number 主席树的更多相关文章

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

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

  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(划分树)

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

  4. hdu 2665 Kth number

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

  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. poj2104 k-th number 主席树入门讲解

    poj2104 k-th number 主席树入门讲解 定义:主席树是一种可持久化的线段树 又叫函数式线段树   刚开始学是不是觉得很蒙逼啊 其实我也是 主席树说简单了 就是 保留你每一步操作完成之后 ...

  7. poj 2104 K-th Number 主席树+超级详细解释

    poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是 ...

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

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

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

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

随机推荐

  1. Mysql group_concat函数列转行,与行转列

    例一: SELECT num from user 1.使用group_concat函数得到列转行 select group_concat(num) from user 2.使用SUBSTRING_IN ...

  2. 自动化测试---PageObjects快速入门(一)

    PageObject快速入门 介绍: Page Object是selenium的一种设计模式, 是在web自动化测试中将一个页面设备成一个class或基础库来实际的方法这种模式的好处有以下几点:1.让 ...

  3. jvm的内存模型

    转自:https://www.cnblogs.com/dingyingsi/p/3760447.html 我们知道,计算机CPU和内存的交互是最频繁的,内存是我们的高速缓存区,用户磁盘和CPU的交互, ...

  4. 网络下载功能实现(downloader ) ---- HTML5+

    模块:downloader Downloader模块管理网络文件下载任务,用于从服务器下载各种文件,并支持跨域访问操作.通过plus.downloader获取下载管理对象.Downloader下载使用 ...

  5. 第k最短路A*启发式搜索

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 21549   Accepted: 5862 ...

  6. open live writer 安装 markdown 插件

    我自己用的是 OpenLiveWriter ,所以本篇只讲 OLW 的,WindowsLiveWriter 戳 MarkdownInWindowsLiveWriter,OpenLiveWriter 戳 ...

  7. 公民身份号码校验码算法(C#版)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. HRBUST - 1153 意外 HRBUST - 1153 (数论)

    意外 Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 326(87 users) Total Accepted: 97(63 users ...

  9. c# 自定义控件之小小进度条

    先看效果图: 非常简洁的一个进度条. 完整项目源码下载:http://files.cnblogs.com/files/tuzhiyuan/%E8%BF%9B%E5%BA%A6%E6%9D%A1%E6% ...

  10. C#基础整理(二)

    1.变量类型int.double.string.char.bool.decimal变量使用规则:先声明,再赋值,最后使用 2.命名规范:Camel:第一个单词首字母小写,其他单词首字母大写,其余字母小 ...