A - 低阶入门膜法 - K-th Number (主席树查询区间第k小)
题目链接: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小)的更多相关文章
- B - 低阶入门膜法 - D-query (查询区间内有多少不同的数)
题目链接:https://cn.vjudge.net/contest/284294#problem/B 题目大意:查询区间内有多少个不相同的数. 具体思路:主席树的做法,主席树的基础做法是查询区间第k ...
- HDU 5919 - Sequence II (2016CCPC长春) 主席树 (区间第K小+区间不同值个数)
HDU 5919 题意: 动态处理一个序列的区间问题,对于一个给定序列,每次输入区间的左端点和右端点,输出这个区间中:每个数字第一次出现的位子留下, 输出这些位子中最中间的那个,就是(len+1)/2 ...
- poj 2104 主席树(区间第k大)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 44940 Accepted: 14946 Ca ...
- 主席树--动态区间第k小
主席树--动态区间第\(k\)小 模板题在这里洛谷2617. 先对几个问题做一个总结: 阅读本文需要有主席树的基础,也就是通过区间kth的模板题. 静态整体kth: sort一下找第k小,时间复杂度\ ...
- poj2104&&poj2761 (主席树&&划分树)主席树静态区间第k大模板
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 43315 Accepted: 14296 Ca ...
- [hdu2665]Kth number(划分树求区间第k大)
解题关键:划分树模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cs ...
- Super Mario HDU - 4417 (主席树询问区间比k小的个数)
Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory ...
- [poj 2104]主席树+静态区间第k大
题目链接:http://poj.org/problem?id=2104 主席树入门题目,主席树其实就是可持久化权值线段树,rt[i]维护了前i个数中第i大(小)的数出现次数的信息,通过查询两棵树的差即 ...
- HDU3727--Jewel (主席树 静态区间第k大)
Jewel Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
随机推荐
- 快乐的Lambda表达式(一)
转载:http://www.cnblogs.com/jesse2013/p/happylambda.html 原文出处: Florian Rappl 译文出处:Jesse Liu 自从Lambda ...
- Twitter数据挖掘:如何使用Python分析大数据 (3)
让我们来拉取Twitter账号@NyTimes的最近20条微博. 我们可以创建变量来存放待拉取的微博数量(即count),以及待拉取的用户(即name).然后用这两个参数调用user_timeline ...
- 【BZOJ2245】[SDOI2011]工作安排(费用流)
[BZOJ2245][SDOI2011]工作安排(费用流) 题面 BZOJ 洛谷 题解 裸的费用流吧. 不需要拆点,只需要连边就好了,保证了\(W_j<W_{j+1}\). #include&l ...
- 【转】如何基于linux进程通信设计方案
前言 linux下的进程通信手段基本上是从Unix平台上的进程通信手段继承而来的.而对Unix发展做出重大贡献的两大主力AT&T的贝尔实验室及BSD(加州大学伯克利分校的伯克利软件发布中心)在 ...
- elasticsearch 安装、配置
elasticsearch:基于java开发,基于RESTful web 接口,提供分布式多用户能力的全文搜索引擎. elasticsearch 安装: 1. java SE Development ...
- 逆向---03.mov、test等汇编指令、EAX、关键Call、OD调试技巧
上一篇:逆向---02.je & jmp & jnz .OD调试 基础知识:(栗子中也会说的)栗子:链接: https://pan.baidu.com/s/1qZbbTvQ 密码: i ...
- Java NIO -- 通道 Channel
通道(Channel):由 java.nio.channels 包定义的.Channel 表示 IO 源与目标打开的连接.Channel 类似于传统的“流”.只不过 Channel本身不能直接访问数据 ...
- win+R快捷启动程序
win10: Win+R cmd:命令行程序 notepad:记事本 winword:word文档 calc:记事本 mspaint:画图 wordpad:写字板
- java代码示例(6-4)
创建ChangePassword.java /** * 需求分析:修改用户密码 * @author chenyanlong * 日期:2017/10/15 */ package com.hp.test ...
- Spark进阶之路-Standalone模式搭建
Spark进阶之路-Standalone模式搭建 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Spark的集群的准备环境 1>.master节点信息(s101) 2&g ...