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 ...
随机推荐
- poj2115-C Looooops -线性同余方程
线性同余方程的模板题.和青蛙的约会一样. #include <cstdio> #include <cstring> #define LL long long using nam ...
- Trailing Zeroes (III) LightOJ - 1138(二分)
You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in d ...
- 04 Zabbix核心概念回顾
04 Zabbix核心概念回顾 1. 监控四大核心功能: 数据采集----数据储存----数据展示和数据分析-----告警 1.1. 数据采集方式: SNMP:被监控设备上面必须启用SNMP a ...
- shoi2017小结
某省选 胡雨菲让我做的,她自己已经AK了... 在loj(自由oj?)上面搜索shoi2017即可. 洛谷上也有,搜六省联考就行 第一题:大水题枚举 P3745 看题目就很水:(其实是因为胡雨菲给我讲 ...
- 【洛谷P4735】最大异或和
题目大意:给定一个长度为 N 的序列,支持两个操作:在序列末尾添加一个新的数字,查询序列区间 \([l,r]\) 内使得 \(a_p\oplus a_{q+1}\oplus ... a_N\oplus ...
- calloc()的使用
百度百科 calloc是一个ISO C函数 函数名: calloc 函数原型:void *calloc(size_t n, size_t size): 功 能: 在内存的动态存储区中分配n个长度为si ...
- Solr7.1---Getting Start
目前最新版的Solr是7.1.0 有个我还不清楚的地方是,为何5.5.X或者6.6.X版本还在更新,给我的感觉好像每一个大版本都有自己的维护团队.不管了. 首先-系统要求 JDK1.8+ 解压Solr ...
- hdu 2586(裸LCA)
传送门 题意: 某村庄有n个小屋,n-1条道路连接着n个小屋(无环),求村庄A到村庄B的距离,要求是经过任一村庄不超过一次. 题解: 求出 lca = LCA(u,v) , 然后答案便是dist[u] ...
- echarts2 饼图处理标签文字过长使之达到指定字数换行的目的
在使用echarts2的过程中,有时会遇到标签文字过长导致显示不完整的问题.例如: 这时候就需要用到 标签里的formatter 回调函数来处理这种情况了. 方式一: formatter : ' { ...
- checkbox用图片替换原始样式,并实现同样的功能
1.结构: <div class="box1"> <input/> <div class="box2"> <img / ...