可持久化线段树模板题。

#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. 68. 对Extjs中store的多种操作

    转自:https://www.cnblogs.com/exmyth/archive/2013/05/16/3082045.html 先来个声明,看着不错,贴过来的,没都测试过. Store.getCo ...

  2. hadoop-Combiner作用用法

    文章来源http://blog.csdn.net/ipolaris/article/details/8723782 reduce的输入每个key所对应的value将是一大串1,但处理的文本很多时,这一 ...

  3. 【WIP】客户端JavaScript DOM

    创建: 2017/10/12 初步完成: 2017/10/15   更新: 2017/10/14 标题加上[WIP],继续完成     [TODO] 补充暂略的, 搜[略]  DOM树  概要  基本 ...

  4. thinkphp关联操作

    比如:你要求删除用户的时候,同时删除与用户有关的所有信息. 一对一:  有 (HAS_ONE)   属于 (BELONGS_TO)    一对多:  有 (HAS_MANY)  属于 (BELONG_ ...

  5. Akka源码分析-Remote-收发消息UL图

  6. 普通平衡树代码。。。Treap

    应一些人之邀...发一篇代码 #include <iostream> #include <cstdio> #include <cstdlib> #include & ...

  7. fcc html5 css 练习2

    <form action="/submit-cat-photo" >action属性的值指定了表单提交到服务器的地址 <input type="text ...

  8. Server Tomcat v8.0 Server at localhost failed to start 问题解决方法?

    bi编程jsp  servlet 第一个程序: HelloServlet 运行错误 404: 十月 28, 2017 11:25:14 上午 org.apache.tomcat.util.digest ...

  9. Linux学习(一)--基本概念

    一.Linux概述 Linux是一款全球性的免费的开源的操作系统平台,其特点是实现了多任务多用户处理,主要是依赖内核kernel shell,且占用资源少 (最小配置只要4Mb内存就能运行). 百度百 ...

  10. JS——stye属性

    1.样式少的时候使用 this.parentNode.style.backgroundColor="yellow"; 2.style是对象 console.log(box.styl ...