可持久化线段树模板题。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <vector> using namespace std; int n,q,tot,a[];
int root[],Left[],Right[];
int val[]; void Insert(const int l,const int r,const int root_l,int& root_r,const int d)
{
val[root_r=++tot]=val[root_l]+;
if(l==r)return ;
int mid=l+((r-l)>>);
if(d<=mid)
{
Right[root_r]=Right[root_l];
Insert(l,mid,Left[root_l],Left[root_r],d);
}
else
{
Left[root_r]=Left[root_l];
Insert(mid+,r,Right[root_l],Right[root_r],d);
}
return ;
} int Query(const int l,const int r,const int root_l,const int root_r,const int d)
{
if(l==r)return l;
int mid=l+((r-l)>>),temp;
temp=val[Left[root_r]]-val[Left[root_l]];
if(temp>=d)return Query(l,mid,Left[root_l],Left[root_r],d);
return Query(mid+,r,Right[root_l],Right[root_r],d-temp);
} int main()
{
freopen("in","r",stdin);
int i;
vector<int> vec; scanf("%d%d",&n,&q);
for(i=;i<=n;++i)
{
scanf("%d",&a[i]);
vec.push_back(a[i]);
} sort(vec.begin(),vec.end());
vec.erase(unique(vec.begin(),vec.end()),vec.end()); for(i=;i<=n;++i)
{
a[i]=lower_bound(vec.begin(),vec.end(),a[i])-vec.begin()+;
Insert(,vec.size(),root[i-],root[i],a[i]);
} for(i=;i<=q;++i)
{
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",vec[Query(,vec.size(),root[l-],root[r],k)-]);
} return ;
}

[POJ2104] 区间第k大数 [区间第k大数,可持久化线段树模板题]的更多相关文章

  1. HDU 1698 Just a Hook (线段树模板题-区间求和)

    Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...

  2. 区间第K小——可持久化线段树模板

    概念 可持久化线段树又叫主席树,之所以叫主席树是因为这东西是fotile主席创建出来的. 可持久化数据结构思想,就是保留整个操作的历史,即,对一个线段树进行操作之后,保留访问操作前的线段树的能力. 最 ...

  3. 洛谷3372线段树模板题 对区间+k或者查询区间和

    #include<bits/stdc++.h> using namespace std; typedef unsigned int ui; typedef long long ll; ty ...

  4. POJ - 3264 线段树模板题 询问区间最大最小值

    这是线段树的一个模板题,给出一串数字,然后询问区间的最大最小值. 这个其实很好办,只需把线段树的节点给出两个权值,一个是区间的最小值,一个是区间的最大值,初始化为负无穷和正无穷,然后通过不断地输入节点 ...

  5. [poj2104]可持久化线段树入门题(主席树)

    解题关键:离线求区间第k小,主席树的经典裸题: 对主席树的理解:主席树维护的是一段序列中某个数字出现的次数,所以需要预先离散化,最好使用vector的erase和unique函数,很方便:如果求整段序 ...

  6. 51Nod 1175 区间中第K大的数 (可持久化线段树+离散)

    1175 区间中第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题   一个长度为N的整数序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有 ...

  7. POJ- 2104 hdu 2665 (区间第k小 可持久化线段树)

    可持久化线段树 也叫函数式线段树也叫主席树,其主要思想是充分利用历史信息,共用空间 http://blog.sina.com.cn/s/blog_4a0c4e5d0101c8fr.html 这个博客总 ...

  8. 【可持久化线段树】POJ2104 查询区间第k小值

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

  9. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

随机推荐

  1. MSP430:中断简介

    (5).中断应用程序举例(外部中断): void interrupt_initial() { P1DIR&=~BIT7;      //P1.7为输入 P1IE|=0x80;      //P ...

  2. vuejs 中如何优雅的获取 Input 值

    http://www.sunzhongwei.com/how-to-get-input-value-in-vuejs

  3. HIT1917Peaceful Commission(2-SAT)

    Peaceful Commission   Source : POI 2001   Time limit : 10 sec   Memory limit : 32 M Submitted : 2112 ...

  4. 基于Spark Streaming预测股票走势的例子(一)

    最近学习Spark Streaming,不知道是不是我搜索的姿势不对,总找不到具体的.完整的例子,一怒之下就决定自己写一个出来.下面以预测股票走势为例,总结了用Spark Streaming开发的具体 ...

  5. 个人微信二次开发API接口

    通过这个API接口可以做什么? 通过我们提供的API接口您可以开发: 工作手机(如:X创,X码,XX管家等) 微信群讲课软件(如:讲课X师,一起X堂等) 微信社群管理软件(如:小X管家,微X助手等) ...

  6. (转)50道JavaScript基础面试题(附答案)

    https://segmentfault.com/a/1190000015288700 1 介绍JavaScript的基本数据类型 Number.String .Boolean .Null.Undef ...

  7. STL之vector篇

    #include<iostream> #include<cstdio> #include<cstring> #include<vector> #incl ...

  8. JS——this与new

    this: 1.this只出现在函数中 2.谁调用函数,this就指的是谁 3.new People的this指的就是被创建的对象实例 new: 1.开辟内存空间,存储新创建的对象 2.把this设置 ...

  9. dubbo之优雅停机

    优雅停机 Dubbo 是通过 JDK 的 ShutdownHook 来完成优雅停机的,所以如果用户使用 kill -9 PID 等强制关闭指令,是不会执行优雅停机的,只有通过 kill PID 时,才 ...

  10. 如何解决Win10预览版一闪而过的disksnapshot.exe进程?

    Win10之家讯上周微软如约向Insider用户推送了Win10预览版10576更新,本次更新修复了之前版本中存在的一些问题,从日常使用的情况来看,对比之前的预览版系统要更稳定了一些,但是还是存在一些 ...