题目连接: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. javascript函数作用域实践

    在es6之前,JavaScript是没有块级作用域的,只有函数作用域,也就是说是一个function里面定义的变量外面取不到的:而if for是条件判断的语句,不是函数,由于缺少块级作用域,所以条件中 ...

  2. Java. Warning – Build path specifies execution environment J2SE-1.5

    Build path specifies execution environment J2SE-1.5. There are no JREs installed in the workspace th ...

  3. Android中的eventBus传值

    第一步:在build.gradle中添加依赖dependencies { compile 'org.greenrobot:eventbus:3.0.0'} 第二步:创建一个 Event类: 注意:en ...

  4. geekNews 学习总结

    1:下移隐藏,上移出现 //android.support.v4.widget.NestedScrollView nsvScroller; //FrameLayout llDetailBottom; ...

  5. OGG中断后,重新同步操作

    模拟一下goldengata中断后,重新同步操作: 1.关掉源端抽取进程 GGSCI (20081122-2105) 15> info all Program Status Group Lag ...

  6. NPY and girls

    NPY and girls 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5145 莫队算法 注意到没有修改区间的操作,使用莫队算法:将整个区间分成若干 ...

  7. Velocity China 2016 Web 性能与运维大会:构建快速、可扩展的弹性网站

    Velocity China 2016 Web 性能与运维大会是一场关于构建快速.可扩展的弹性网站所需要的Web性能.运维及开发运维的训练.大会将于2016年12月1日在北京拉开帷幕,此次大会被众多业 ...

  8. jQuery判断滚动条是上滚还是下滚,且是否到达底部或顶部

    jQuery判断滚动条是上滚还是下滚,且是否到达底部或顶部:http://www.haorooms.com/post/jquery_scroll_upanddown //滚动条滚动加载更多内容 //判 ...

  9. 禁止使用test类的就是禁止使用本来的$this对象.可以调用父类的对象

    public function __construct() { parent::__construct( ); parent::__construct( ); if(!APP_DEBUG ) die( ...

  10. Android onConfigurationChanged的作用

    API原文说明: android:configChangesLists configuration changes that the activity will handle itself. When ...