hdu 2665 Kth number 主席树
Kth number
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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]
10 1
1 4 2 3 5 6 7 8 9 0
1 3 2
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=;
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);
}
};
Chairmantree tree;
int a[N],b[N];
int getpos(int x,int cnt)
{
int pos=lower_bound(b+,b+cnt,x)-b;
return pos;
}
int main()
{
int T;
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]);
}
//for(int i=1;i<num;i++)
// printf("%d ",tree.rt[i]);
//printf("\n");
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 2665 Kth number 主席树的更多相关文章
- HDU - 2665 Kth number 主席树/可持久化权值线段树
题意 给一个数列,一些询问,问$[l,r]$中第$K$大的元素是哪一个 题解: 写法很多,主席树是最常用的一种之一 除此之外有:划分树,莫队分块,平衡树等 主席树的定义其实挺模糊, 一般认为就是可持久 ...
- hdu 2665 Kth number(划分树模板)
http://acm.hdu.edu.cn/showproblem.php?pid=2665 [ poj 2104 2761 ] 改变一下输入就可以过 http://poj.org/problem? ...
- HDU 2665 Kth number(划分树)
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- hdu 2665 Kth number
划分树 /* HDU 2665 Kth number 划分树 */ #include<stdio.h> #include<iostream> #include<strin ...
- 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )
在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...
- poj2104 k-th number 主席树入门讲解
poj2104 k-th number 主席树入门讲解 定义:主席树是一种可持久化的线段树 又叫函数式线段树 刚开始学是不是觉得很蒙逼啊 其实我也是 主席树说简单了 就是 保留你每一步操作完成之后 ...
- poj 2104 K-th Number 主席树+超级详细解释
poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是 ...
- POJ 2104&HDU 2665 Kth number(主席树入门+离散化)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 50247 Accepted: 17101 Ca ...
- HDU 2665 Kth number(主席树静态区间第K大)题解
题意:问你区间第k大是谁 思路:主席树就是可持久化线段树,他是由多个历史版本的权值线段树(不是普通线段树)组成的. 具体可以看q学姐的B站视频 代码: #include<cmath> #i ...
随机推荐
- Codeforces 603E Pastoral Oddities
传送门:http://codeforces.com/problemset/problem/603/E [题目大意] 给出$n$个点,$m$个操作,每个操作加入一条$(u, v)$长度为$l$的边. 对 ...
- [SQL]批量 更改字符集脚本,批量查询约束,批量查询索引
How to change collation of all database objects in SQL Server. Have you encountered a problem where ...
- android异常:Can not perform this action after onSaveInstanc
extends:http://zhiweiofli.iteye.com/blog/1539467 本人某个android项目开发阶段一直运行良好,直到上线前夕,在某款跑着android 4.03系统的 ...
- CH0601 Genius ACM【倍增】【归并排序】
0601 Genius ACM 0x00「基本算法」例题 描述 给定一个整数 M,对于任意一个整数集合 S,定义“校验值”如下: 从集合 S 中取出 M 对数(即 2∗M 个数,不能重复使用集合中的数 ...
- html锚点的作用和js选项卡锚点跳转的使用
location是javascript里边管理地址栏的内置对象,比如location.href就管理页面的url,用location.href=url就可以直接将页面重定向url.而location. ...
- 第1章 1.9计算机网络概述--OSI参考模型和网络安全
1.物理层安全: 风险:无关人员接入网络. 防范:将用不到的口的网线拔掉,或者用命令将用不到的网线接口shutdown(关闭). 2.数据链路层安全: ADSL账号密码 无线网账号密码 交换机绑定MA ...
- C++模拟Http/Https访问web站点
一.概述 1.Http与Https的区别与联系 在OSI参考模型中Http与Https均属于应用层协议.Http即Hypertext Transfer Protocol,超文本传输协议:而Https为 ...
- Mybatis 创建Configuration对象
Mybatis 创建Configuration对象是在项目启动时就创建了. 具体创建流程为: https://blog.csdn.net/wolfcode_cn/article/details/80 ...
- jsp内置对象与servlet的关系
Servlet与JSP九大内置对象的关系 JSP对象 怎样获得 out->response.getWriter request ->Service方法中的req参数 response ...
- ruby on rails validates
validates :conclusion, :presence => true, :inclusion => { :in => [0,1] } validates :email, ...