可持久化线段树模板题。

#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. 能够完成qq信息提醒的代码

    下面这个网址就帮咱们实现了QQ提醒的功能,别被它的外面吓坏,虽然是长的有点恶心,但是它可是一段“神奇代码”. http://qzs.qq.com/snsapp/app/bee/widget/open. ...

  2. [Swift通天遁地]六、智能布局-(8)布局框架的使用:多分辨率适配和横竖屏布局

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  3. RHEL6.5 设置yum,IP地址,解压缩

    系统运维 www.osyunwei.com 温馨提醒:qihang01原创内容©版权所有,转载请注明出处及原文链接 服务器相关设置如下: 操作系统:RHEL 6.5 64位 IP地址:192.168. ...

  4. 无法定位程序输入点_except_handler4_common于动态链接库msvcrt.dll

    这是由于sp3加载的驱动造成的:只需要将C:\WINDOWS\system32\dwmapi.dll重新命名一下即可以解决. 可以调试程序当系统加载到“c:\Program Files\China M ...

  5. 6.11---上传图片遇到的bug,字节流输入流输出流----图解----图片必须是post

    !!!这里要注意不能是目录必须是指定的文件名+目录,不然就存照片到指定的目录不成功 ----------------------------------------完整controller-servi ...

  6. Android 微信SDK图片分享(checkArgs fail, thumbData is invalid)

    微信官网给的Demo中.图片的分享例子他是这么描述的: String url = "http://pic2.nipic.com/20090506/1478953_125254084_2.jp ...

  7. 解决Sql Server 日志满了,设置收缩

    解决Sql Server 日志满了,设置收缩: --查看文件占用空间 . '文件大小(MB)',* from sysfiles; ALTER DATABASE SpyData SET RECOVERY ...

  8. html5——DOM扩展

    元素获取 1.document.getElementsByClassName ('class') 通过类名获取元素,以类数组形式存在. 2.document.querySelector(‘div’) ...

  9. R语言开发环境的搭建

    1.R语言的下载 https://mirrors.tuna.tsinghua.edu.cn/CRAN/ 2.R语言的安装 安装完后,打开R Console 输入 pie(c(0.9, 0.2, 0.3 ...

  10. vs for Mac中的启用Entity Framework Core .NET命令行工具

    在vs for Mac的工具菜单中已没有了Package Manager Console. 我们可以通过以下方法使用Entity Framework Core .NET命令行工具: 1.添加Nuget ...