题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2665

题意:给你一个区间,让你找这个区间第K大的数

题解:主席树模版题,也可以用划分树

 #include<cstdio>
#include<vector>
#include<algorithm>
#define FFC(i,a,b) for(int i=a;i<=b;++i)
using namespace std; const int maxn=1e5+;
int root[maxn],a[maxn],cnt;
vector<int>v;
struct node{int l,r,sum;}T[maxn*];
int getid(int x){return lower_bound(v.begin(),v.end(),x)-v.begin()+;} void update(int l,int r,int &x,int y,int pos){
T[++cnt]=T[y],T[cnt].sum++,x=cnt;
if(l==r)return;
int mid=(l+r)>>;
if(mid>=pos)update(l,mid,T[x].l,T[y].l,pos);
else update(mid+,r,T[x].r,T[y].r,pos);
} int query(int l,int r,int x,int y,int k){
if(l==r)return l;
int mid=(l+r)>>;
int sum=T[T[y].l].sum-T[T[x].l].sum;
if(sum>=k)return query(l,mid,T[x].l,T[y].l,k);
else return query(mid+,r,T[x].r,T[y].r,k-sum);
}
int main(){
int t,m,n,x,y,k;
scanf("%d",&t);
while(t--){
v.clear();cnt=;
scanf("%d%d",&n,&m);
FFC(i,,n)scanf("%d",&a[i]),v.push_back(a[i]);
sort(v.begin(),v.end()),v.erase(unique(v.begin(),v.end()),v.end());
FFC(i,,n)update(,n,root[i],root[i-],getid(a[i]));
FFC(i,,m)scanf("%d%d%d",&x,&y,&k),printf("%d\n",v[query(,n,root[x-],root[y],k)-]);
}
return ;
}

hdu_2665_Kth number(主席树)的更多相关文章

  1. poj2104 k-th number 主席树入门讲解

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

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

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

  3. POJ2104 K-th Number[主席树]【学习笔记】

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

  4. [poj2104] K-th Number (主席树)

    主席树 Description You are working for Macrohard company in data structures department. After failing y ...

  5. poj 2104 K-th Number(主席树 视频)

    K-th Number 题意: 给你一些数,让你求一个区间内,第k大的数是多少. 题解: 主席树第一题,看的qsc视频写的,戳戳戳 学到了unique函数,他的作用是:把相邻的重复的放到后面,返回值是 ...

  6. 主席树:POJ2104 K-th Number (主席树模板题)

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

  7. 【POJ2104】【HDU2665】K-th Number 主席树

    [POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...

  8. SPOJ MKTHNUM & POJ 2104 - K-th Number - [主席树模板题]

    题目链接:http://poj.org/problem?id=2104 Description You are working for Macrohard company in data struct ...

  9. Online Judge 2014 K-th Number -主席树

    You are working for Macrohard company in data structures department. After failing your previous tas ...

随机推荐

  1. css display属性介绍

    none此元素不会被显示. block此元素将显示为块级元素,此元素前后会带有换行符. inline默认.此元素会被显示为内联元素,元素前后没有换行符. inline-block行内块元素.(CSS2 ...

  2. <button>与<input type="button"> 的区别

    <button> button按钮点击会刷新整个页面 <input type="button">  不会刷新整个页面 本文为本人用来记录自己做的一些东西,如 ...

  3. BuildingAndRunningUAFServerUsingMaven

    https://github.com/eBay/UAF/wiki/BuildingAndRunningUAFServerUsingMaven(CLIonly) 实现uaf的demo,使用ebay的方案 ...

  4. 《JavaScript高级程序设计》读书笔记 ---Function 类型

    说起来ECMAScript 中什么最有意思,我想那莫过于函数了——而有意思的根源,则在于函数实际上是对象.每个函数都是Function 类型的实例,而且都与其他引用类型一样具有属性和方法.由于函数是对 ...

  5. 安装Redis无错流程

    1.参考文章<安装3.0.3版本配置文章参考>http://www.iyunv.com/thread-89612-1-1.html 2.安装tcl组件包(安装Redis需要tcl支持) 下 ...

  6. 关于c语言变量的内存分布测试程序

    #include <stdio.h> #include <stdlib.h> ; // 常全局变量 ; // 初始化全局变量 int uninit_global; // 未初始 ...

  7. hdu1022

    #include <stdio.h>#include <string.h> int main(void){ int n,i,j,k,l; char o1[10],o2[10], ...

  8. socket编程——一个简单的例子

    从一个简单的使用TCP例子开始socket编程,其基本步骤如下: server                                                  client ++++ ...

  9. 为UIView添加分类直接改或获取控件的frame值

    #import <UIKit/UIKit.h> @interface UIView (SJBExtends) @property (nonatomic,assign)CGFloat x; ...

  10. android基础(一)

    wrap_conten:包裹实际文本内容 match_parent:当前控件铺满父类容器:2.3api之后添加的一个属性值 fill_parent:包裹实际文本内容,在2.3api之前的一个属性值 a ...