题目链接:https://cn.vjudge.net/contest/284294#problem/A

题目大意:主席树查询区间第k小。

具体思路:主席树入门。

AC代码:

 #include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
# define ll long long
const int maxn = 1e5+;
struct node
{
int sum;
int l,r;
} tree[maxn*];
int sto[maxn],root[maxn];
vector<int>q;
int tot;
void init()
{
tot=;
root[]=;
}
int get_id(int t)
{
return lower_bound(q.begin(),q.end(),t)-q.begin()+;
}
int add(int pre)
{
int t=++tot;
tree[t].sum=tree[pre].sum+;
tree[t].l=tree[pre].l;
tree[t].r=tree[pre].r;
return t;
}
void update(int &o,int pre,int l,int r,int pos)
{
o=add(pre);
if(l==r)return ;
int m=(l+r)>>;
if(pos<=m)update(tree[o].l,tree[o].l,l,m,pos);
else update(tree[o].r,tree[o].r,m+,r,pos);
}
int query(int st,int ed,int l,int r,int k){
if(l==r)return r;
int sum=tree[tree[ed].l].sum-tree[tree[st].l].sum;
int m=(l+r)>>;
if(k<=sum)return query(tree[st].l,tree[ed].l,l,m,k);
else return query(tree[st].r,tree[ed].r,m+,r,k-sum);
}
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
init();
for(int i=; i<=n; i++)
{
scanf("%d",&sto[i]);
q.push_back(sto[i]);
}
sort(q.begin(),q.end());
q.erase(unique(q.begin(),q.end()),q.end());
int num=q.size();
for(int i=; i<=n; i++)
{
update(root[i],root[i-],,num,get_id(sto[i]));
}
int t1,t2,k;
while(m--){
scanf("%d %d %d",&t1,&t2,&k);
printf("%d\n",q[query(root[t1-],root[t2],,num,k)-]);
}
}
return ;
}

 

A - 低阶入门膜法 - K-th Number (主席树查询区间第k小)的更多相关文章

  1. B - 低阶入门膜法 - D-query (查询区间内有多少不同的数)

    题目链接:https://cn.vjudge.net/contest/284294#problem/B 题目大意:查询区间内有多少个不相同的数. 具体思路:主席树的做法,主席树的基础做法是查询区间第k ...

  2. HDU 5919 - Sequence II (2016CCPC长春) 主席树 (区间第K小+区间不同值个数)

    HDU 5919 题意: 动态处理一个序列的区间问题,对于一个给定序列,每次输入区间的左端点和右端点,输出这个区间中:每个数字第一次出现的位子留下, 输出这些位子中最中间的那个,就是(len+1)/2 ...

  3. poj 2104 主席树(区间第k大)

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

  4. 主席树--动态区间第k小

    主席树--动态区间第\(k\)小 模板题在这里洛谷2617. 先对几个问题做一个总结: 阅读本文需要有主席树的基础,也就是通过区间kth的模板题. 静态整体kth: sort一下找第k小,时间复杂度\ ...

  5. poj2104&&poj2761 (主席树&&划分树)主席树静态区间第k大模板

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

  6. [hdu2665]Kth number(划分树求区间第k大)

    解题关键:划分树模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cs ...

  7. Super Mario HDU - 4417 (主席树询问区间比k小的个数)

    Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory ...

  8. [poj 2104]主席树+静态区间第k大

    题目链接:http://poj.org/problem?id=2104 主席树入门题目,主席树其实就是可持久化权值线段树,rt[i]维护了前i个数中第i大(小)的数出现次数的信息,通过查询两棵树的差即 ...

  9. HDU3727--Jewel (主席树 静态区间第k大)

    Jewel Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

随机推荐

  1. 搬进Github

    学习参考 萌码 一.Github简介和基本操作 Github 上操作基本上围绕一个个项目展开.项目就是一个文件夹,在github中成为“仓库”(repository),里面放着所有的项目文件,可以是代 ...

  2. 自学Linux Shell18.1-sed编辑器基础特性

    点击返回 自学Linux命令行与Shell脚本之路 18.1-sed编辑器基础特性 linux世界中最广泛使用的两个命令行编辑器: sed gawk 1. sed概念 sed是stream edito ...

  3. 自学Zabbix3.12.4-动作Action-Operation配置

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 3.12.4 自学Zabbix3.12.4-动作Action-Operation配置 1. 概述 ...

  4. 洛谷 P3225 [HNOI2012]矿场搭建 解题报告

    P3225 [HNOI2012]矿场搭建 题目描述 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤 ...

  5. luogu4269 Snow Boots G (并查集)

    对于某个靴子,如果0代表某个格能走,1代表不能走,那么只要连续的1的个数的最大值>=靴子的步长,那这个靴子就不能用. 那么只要对靴子和格子都按深度排个序,然后从大到小来扫一遍(靴子越来越浅,能走 ...

  6. 快速幂&快速乘法

    尽管快速幂与快速乘法好像扯不上什么关系,但是东西不是很多,就一起整理到这里吧 快速幂思想就是将ax看作x个a相乘,用now记录当前答案,然后将指数每次除以2,然后将当前答案平方,如果x的2进制最后一位 ...

  7. Codeforces Round #510 (Div. 2)(C)

    传送门:Problem C https://www.cnblogs.com/violet-acmer/p/9682082.html 题意: 给你n个数,定义有两种操作 ① 1 i j : (i != ...

  8. idea去掉无效引用

    Mac版的idea,打开Preference->Editor->General,打勾红色框框. 然后使用快捷键 contrl + option + o 只能去掉当前文件没有引用的包,并不能 ...

  9. instance of

    instanceof是Java的一个二元操作符(运算符),也是Java的保留关键字.它的作用是判断其左边对象是否为其右边类的实例,返回的是boolean类型的数据.用它来判断某个对象是否是某个Clas ...

  10. Linux命令之grep

    grep命令 用处:文本搜索工具 用法:grep + ‘查找关键字的名字’ + 文件名 示例: 还在profile里面查找then 干净利落强大,有关then的信息显示