Kth number

                                                Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                                        Total Submission(s): 10682    Accepted Submission(s): 3268

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
说实话  我不知道我该怎么去解释 因为这就是主席树的板子
无区间修改的主席树 不解释
 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
#define ll long long
#define LL unsigned long long
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=0.0000000001;
const int N=+;
struct node{
int left,right;
int val;
}tree[N*];
int cnt;
int a[N],b[N];
int root[N];
int build(int l,int r){
int pos=++cnt;
tree[pos].val=;
if(l==r)return pos;
int mid=(l+r)>>;
tree[pos].left=build(l,mid);
tree[pos].right=build(mid+,r);
return pos;
}
void update(int pre,int &now,int x,int l,int r){
tree[++cnt]=tree[pre];
now=cnt;
tree[now].val++;
if(l==r)return;
int mid=(l+r)>>;
if(x<=mid){
update(tree[pre].left,tree[now].left,x,l,mid);
}
else{
update(tree[pre].right,tree[now].right,x,mid+,r);
} }
int query(int L,int R,int k,int l,int r){
if(l==r) return l;
int mid=(l+r)>>;
int sum=tree[tree[R].left].val-tree[tree[L].left].val;
if(k<=sum){
return query(tree[L].left,tree[R].left,k,l,mid);
}
else{
return query(tree[L].right,tree[R].right,k-sum,mid+,r);
} }
int main()
{
int t;
scanf("%d",&t);
while(t--){
memset(root,,sizeof(root));
memset(a,,sizeof(a));
memset(b,,sizeof(b));
int n,m;
scanf("%d%d",&n,&m);
cnt=;
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b++n);
int tt = unique(b+,b++n)-b-;
root[0]=build(1,tt);
for(int i=;i<=n;i++) {
a[i]=lower_bound(b+,b++tt,a[i])-b;//二分找到a[i]的位置
update(root[i-],root[i],a[i],,tt);//root[i-1]表示上一个版本的线段树
}
for(int i=;i<=m;i++) {
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
int ans=query(root[l-],root[r],k,,tt);
printf("%d\n", b[ans]);
}
}
return ;
}

HDU 2665(主席树,无修改第k小)的更多相关文章

  1. POJ 2104 HDU 2665 主席树 解决区间第K大

    两道题都是区间第K大询问,数据规模基本相同. 解决这种问题, 可以采用平方划分(块状表)复杂度也可以接受,但是实际表现比主席树差得多. 这里大致讲一下我对主席树的理解. 首先,如果对于某个区间[L,R ...

  2. Super Mario HDU - 4417 (主席树询问区间比k小的个数)

    Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory ...

  3. HDU 5919 - Sequence II (2016CCPC长春) 主席树 (区间第K小+区间不同值个数)

    HDU 5919 题意: 动态处理一个序列的区间问题,对于一个给定序列,每次输入区间的左端点和右端点,输出这个区间中:每个数字第一次出现的位子留下, 输出这些位子中最中间的那个,就是(len+1)/2 ...

  4. 主席树--动态区间第k小

    主席树--动态区间第\(k\)小 模板题在这里洛谷2617. 先对几个问题做一个总结: 阅读本文需要有主席树的基础,也就是通过区间kth的模板题. 静态整体kth: sort一下找第k小,时间复杂度\ ...

  5. A - 低阶入门膜法 - K-th Number (主席树查询区间第k小)

    题目链接:https://cn.vjudge.net/contest/284294#problem/A 题目大意:主席树查询区间第k小. 具体思路:主席树入门. AC代码: #include<i ...

  6. SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)

    COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to  ...

  7. BZOJ.2588.Count on a tree(主席树 静态树上第k小)

    题目链接 /* 序列上的主席树 某点是利用前一个点的根建树 同理 树上的主席树 某个节点可以利用其父节点(is unique)的根建树 排名可以利用树上前缀和求得: 对于(u,v),w=LCA(u,v ...

  8. SPOJ 10628 Count on a tree(Tarjan离线 | RMQ-ST在线求LCA+主席树求树上第K小)

    COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to  ...

  9. 主席树(区间第k小的数)

    题目链接: https://www.luogu.org/problem/P3834 首先要离散化,然后主席树模板. 1 #include<cstdio> 2 #include<cst ...

随机推荐

  1. jQuery——插件制作

    1.$.fn.extend:扩展 jQuery 元素集来提供新的方法(通常用来制作插件),使用时是$('选择器').方法 2.$.extend:扩展jQuery对象本身,用来在jQuery命名空间上增 ...

  2. 控制台——对WIN32 API的使用(user32.dll)

    Win32 API概念:即为Microsoft 32位平台的应用程序编程接口(Application Programming Interface).所有在Win32平台上运行的应用程序都可以调用这些函 ...

  3. [Windows Server 2012] 安装SQL Server 2012

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装SQL S ...

  4. 3星|《IBM商业价值报告:区块链》:一些重要行业对区块链的态度和已经发生的区块链的应用

    区块链项目开发指南 (区块链技术丛书) 介绍IBM的专家们调研许多重要行业与组织后总结的各行业对区块链的态度和实际的应用.看起来有点意思,不过有两个缺点: 1:这些实际已经发生的应用基本没看到相关的新 ...

  5. 用 ilasm 反编译、修改.net dll文件

    有些.net dll我们没有源码,如果要修改某些东西,可以用ilasm.exe反编译为il代码,修改后再编译回dll ilasm通常放在以下路径 C:\Windows\Microsoft.NET\Fr ...

  6. bootstrapValidator代码中开启验证和判断验证是否通过

    //开启验证 $('#saveadmin_form').data('bootstrapValidator').validate(); //是否通过校验 if(!$('#saveadmin_form') ...

  7. CentOS7搭建KMS服务器

    使用vlmcsd搭建KMS服务器 1.下载vlmcsd: wget https://github.com/Wind4/vlmcsd/releases/download/svn1111/binaries ...

  8. 终端打印SQL语句

    在 Django 项目的 settings.py 文件中配置: LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handle ...

  9. Delphi 10.3.2最新消息

    官方已经发布消息,招内测人员了! https://www.barnsten.com/default/newsupdates/details?news_id=328 https://docs.googl ...

  10. WEB 移动端 CSS3动画性能 优化

    很多时候,我们在开发移动端的时候要使自己的网页兼容不同的机型,很多时候会采用CSS3动画,但是很多时候在安卓机下,动画明显会出现卡顿,很难看,那么这里我介绍几个CSS 属性进行硬件加速那么就会得到明显 ...