HDU 2655 主席树
Kth number
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10240 Accepted Submission(s): 3153
For each test case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the sequence and the number of the quaere.
The second line contains n integers, describe the sequence.
Each of following m lines contains three integers s, t, k.
[s, t] indicates the interval and k indicates the kth big number in interval [s, t]
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
#define N 100005
using namespace std;
struct chairmantree
{
int rt[*N],ls[*N],rs[*N],sum[*N];
int tot;
void init()
{
tot=;
}
void build(int l,int r,int &pos)
{
pos=++tot;
sum[pos]=;
if(l==r) return ;
int mid=(l+r)>>;
build(l,mid,ls[pos]);
build(mid+,r,rs[pos]);
}
void update(int p,int c,int pre,int l,int r,int &pos)
{
pos=++tot;
ls[pos]=ls[pre];
rs[pos]=rs[pre];
sum[pos]=sum[pre]+c;
if(l==r) return ;
int mid=(l+r)>>;
if(p<=mid)
update(p,c,ls[pre],l,mid,ls[pos]);
else
update(p,c,rs[pre],mid+,r,rs[pos]);
}
int query(int L,int R,int l,int r,int k)
{
if(l==r) return l;
int mid=(l+r)>>;
int num=sum[ls[R]]-sum[ls[L]];
if(k<=num) return query(ls[L],ls[R],l,mid,k);
else return query(rs[L],rs[R],mid+,r,k-num);
}
} tree;
int t;
int a[N],b[N];
int getpos(int x,int cnt)
{
int pos=lower_bound(b+,b+cnt,x)-b;
return pos;
} int main()
{
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b++n);
int num=unique(b+,b++n)-b;
tree.init();
tree.build(,num-,tree.rt[]);
for(int i=;i<=n;i++)
{
int p=getpos(a[i],num);
tree.update(p,,tree.rt[i-],,num-,tree.rt[i]);
}
while(m--)
{
int l,r,k;
scanf("%d %d %d",&l,&r,&k);
printf("%d\n",b[tree.query(tree.rt[l-],tree.rt[r],,num-,k)]);
}
}
return ;
}
HDU 2655 主席树的更多相关文章
- hdu 5919 主席树(区间不同数的个数 + 区间第k大)
Sequence II Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
- Super Mario HDU 4417 主席树区间查询
Super Mario HDU 4417 主席树区间查询 题意 给你n个数(编号从0开始),然后查询区间内小于k的数的个数. 解题思路 这个可以使用主席树来处理,因为这个很类似查询区间内的第k小的问题 ...
- HDU 2852 主席树
KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 6278 主席树(区间第k大)+二分
Just h-index Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)To ...
- HDU 2665(主席树,无修改第k小)
Kth number Time Limit: 15000/5000 MS (Java/Others) ...
- HDU 3333 & 主席树
题意: balabala SOL: 这题用主席树怎么做呢...貌似一模一样...一个一个建n棵的线段树.先把上一棵树复制下来,当a[i]出现过,就把这棵树里的那个位置去掉------一模一样的思维.. ...
- HDU 4251 --- 主席树(划分树是正解)
题意:查询区间中位数 思路:模板题,相当于区间第K大的数,主席树可以水过,但划分树是正解.但还没搞明白划分树,先上模板 #include <iostream> #include <c ...
- hdu 5140 主席树
这题说的是每个员工有工资 水平 在公司待的年限这几个属性,有大量的查询 查的是在一定的水平和工作年限的工人总工资是多少 这个思路是比较简单的我们按照他们的水平排序,排完后,使用主席树不断地往里面插,然 ...
- HDU - 4866 主席树 二分
题意:在x轴\([1,X]\)内的上空分布有n个占据空间\([L_i,R_i]\),高度\(D_i\)的线段,射中线段的得分为其高度,每次询问从x轴的\(x\)往上空射的最近k个线段的总得分,具体得分 ...
随机推荐
- javaee开发工具及环境配置过程
在配置javaee开发环境的过程中遇到过很多问题,在此系统的整理一下我之前的配置过程 注:配置过程学习自<JSP&Servlet学习笔记(第二版)>详细过程可以阅读此书.在文章的最 ...
- ThreadLocal 线程的私有内存
话说在<操作系统原理>这门课里面,我们学到了很多概念:进程.线程.锁.PV操作.读写者问题等等,大家还记得么?(估计有些概念早已忘记了吧,哈哈哈~) 其中关于进程.线程和锁的东西是我们平时 ...
- ArrayList与LinkedList的普通for循环遍历
对于大部分Java程序员朋友们来说,可能平时使用得最多的List就是ArrayList,对于ArrayList的遍历,一般用如下写法: public static void main(String[] ...
- 【未完】训练赛20190304:KMP+树状数组+线段树+优先队列
头炸了啊,只做出L题,前两天刚看的Shawn zhou的博客学习的,幸亏看了啊,否则就爆零了,发现题目都是经典题,线段树,KMP,我都没看过,最近又在复习考研,真后悔大一大二没好好学习啊,得抽时间好好 ...
- Python变量常量及注释
一.变量命名规则1.有字母.数字.下划线搭配组合而成2.不能以数字开头,更不能全为数字3.不能用Python的关键字4.不要太长5.名字要有意义6.不要用中文7.区分大小写8.采用驼峰体命名(多个单词 ...
- 2018java开发一些面经
算法系列:https://www.cnblogs.com/yanmk/p/9232908.html 2018Java开发面经(持续更新) 不要给自己挖坑!!!不要给自己挖坑!!!不要给自己挖坑!!!如 ...
- 一:HDFS 用户指导
1.hdfs的牛逼特性 Hadoop, including HDFS, is well suited for distributed storage and distributed processin ...
- su和sudo的使用
用于用户身份切换 一.su 命令形式 代表内容 su 切换为root,以non-login shell的方式 su - 切换为root,以login shell的方式 su -l 账号 切换为“账号” ...
- File Searching
Description Have you ever used file searching tools provided by an operating system? For example, in ...
- C语言中动态内存的分配(malloc,realloc)
动态内存分配:根据需要随时开辟,随时释放的内存分配方式.分配时机和释放时机完全由程序员决定,由于没有数据声明,这部分空间没有名字.无法像使用变量或数组那样通过变量名或数组名引用其中的数据,只能通过指针 ...