[poj 2104]主席树+静态区间第k大
题目链接:http://poj.org/problem?id=2104
主席树入门题目,主席树其实就是可持久化权值线段树,rt[i]维护了前i个数中第i大(小)的数出现次数的信息,通过查询两棵树的差即可得到第k大(小)元素。
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std; #define lson(i) node[i].lson
#define rson(i) node[i].rson const int maxn=;
int rt[maxn]; struct Node
{
int lson,rson,val;
}node[maxn*];
int tot; void push_up(int i)
{
node[i].val=node[lson(i)].val+node[rson(i)].val;
} int build(int l,int r)
{
int i=++tot;
if (l==r) node[i].val=;
else
{
int mid=(l+r)/;
lson(i)=build(l,mid);
rson(i)=build(mid+,r);
push_up(i);
}
return i;
} int rebuild(int k,int x,int i,int nowl,int nowr)
{
int th=++tot;
lson(th)=lson(i);
rson(th)=rson(i);
node[th].val=node[i].val;
if (nowl==nowr) node[th].val+=x;
else
{
int mid=(nowl+nowr)/;
if (k<=mid) node[th].lson=rebuild(k,x,lson(i),nowl,mid);
else node[th].rson=rebuild(k,x,rson(i),mid+,nowr);
push_up(th);
}
return th;
} int query(int rt1,int rt2,int k,int nowl,int nowr)
{
if (nowl==nowr) return nowl;
int left=node[lson(rt2)].val-node[lson(rt1)].val;
int mid=(nowl+nowr)/;
if (left>=k) return query(lson(rt1),lson(rt2),k,nowl,mid);
else return query(rson(rt1),rson(rt2),k-left,mid+,nowr);
} int a[maxn];
vector<int> ls; int main()
{
int n,m;
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++) scanf("%d",&a[i]);
ls.clear();
for (int i=;i<=n;i++) ls.push_back(a[i]);
sort(ls.begin(),ls.end());
ls.erase(unique(ls.begin(),ls.end()),ls.end());
rt[]=build(,n);
for (int i=;i<=n;i++)
{
int j=lower_bound(ls.begin(),ls.end(),a[i])-ls.begin();
rt[i]=rebuild(j+,,rt[i-],,n);
}
for (int i=;i<=m;i++)
{
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",ls[query(rt[l-],rt[r],k,,n)-]);
}
return ;
}
[poj 2104]主席树+静态区间第k大的更多相关文章
- poj 2104 主席树(区间第k大)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 44940 Accepted: 14946 Ca ...
- poj2104&&poj2761 (主席树&&划分树)主席树静态区间第k大模板
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 43315 Accepted: 14296 Ca ...
- HDU 2665 Kth number(主席树静态区间第K大)题解
题意:问你区间第k大是谁 思路:主席树就是可持久化线段树,他是由多个历史版本的权值线段树(不是普通线段树)组成的. 具体可以看q学姐的B站视频 代码: #include<cmath> #i ...
- POJ2104-- K-th Number(主席树静态区间第k大)
[转载]一篇还算可以的文章,关于可持久化线段树http://finaltheory.info/?p=249 无修改的区间第K大 我们先考虑简化的问题:我们要询问整个区间内的第K大.这样我们对值域建线段 ...
- HDU3727--Jewel (主席树 静态区间第k大)
Jewel Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- POJ 2104 HDU 2665 主席树 解决区间第K大
两道题都是区间第K大询问,数据规模基本相同. 解决这种问题, 可以采用平方划分(块状表)复杂度也可以接受,但是实际表现比主席树差得多. 这里大致讲一下我对主席树的理解. 首先,如果对于某个区间[L,R ...
- 主席树入门(区间第k大)
主席树入门 时隔5个月,我又来填主席树的坑了,现在才发现学算法真的要懂了之后,再自己调试,慢慢写出来,如果不懂,就只会按照代码敲,是不会有任何提升的,都不如不照着敲. 所以搞算法一定要弄清原理,和代码 ...
- POJ 2104 K-th Number (区间第k大)
题意:给定一个序列A,接下来又m个询问,每个询问输出A[L,R]中的第K大.(保证第k大存在) 思路: 我想拿来练习“可持久化线段树”的,搜到这个比较巧的算法也可以解决这个问题,叫“归并树?.大概的思 ...
- 洛谷.3834.[模板]可持久化线段树(主席树 静态区间第k小)
题目链接 //离散化后范围1~cnt不要错 #include<cstdio> #include<cctype> #include<algorithm> //#def ...
随机推荐
- Python学习:变量
变量(Variables): 是为了存储程序在运算过程中的一些中间结果,为了方便日后调用储存在计算的内存中 官方介绍: Variables are used to storeinformation t ...
- 《深入浅出MFC》– Document-View深入探讨
1.其实Document/View不是什么新东西,Xerox PARC实验室是这种观念的滥觞.它是Smalltalk环境中的关键性部分,在那里它被称为Model-View-Controller(MVC ...
- Python自动化运维——系统进程管理模块
Infi-chu: http://www.cnblogs.com/Infi-chu/ 模块:psutil psutil是一个跨平台库,可以很轻松的为我们实现获取系统运行的进程和资源利用率等信息. 功能 ...
- ffmpeg使用笔记
1.从mp4中提取h264:ffmpeg -i 264.mp4 -codec copy -bsf h264_mp4toannexb -f h264 output.h2642.从mp4中提取hevc:f ...
- go学习笔记-语言指针
语言指针 定义及使用 变量是一种使用方便的占位符,用于引用计算机内存地址.取地址符是 &,放到一个变量前使用就会返回相应变量的内存地址. 一个指针变量指向了一个值的内存地址.类似于变量和常量, ...
- CSS3单选动画
本示例实现了两种单选按钮动画效果,一种是缩放,一种是旋转,以下是html布局以及css样式 html:这里使用了label标签的for属性,以此来绑定radio <div class=" ...
- Datetime与Datetime2的区别
Datetime: 时间格式,对应于数据库中的DateTime类型,对应于.NET里面的System. DateTime类型.DateTime支持日期从1753年1月1日到9999年12 ...
- 序列化---fastjson使用
该文章主要介绍com.alibaba.fastjson的使用. 首先创建maven工程,导入fastjson.挑个热度高的版本就好了. 首先考虑下,我们通常什么时候会使用序列化和反序列化: 1.将ja ...
- zabbix 通过key(键值)获取信息
在agent端进行修改264行,例如: UserParameter=get.os.type,head -1 /etc/issue 保存重启agent 验证 zabbix_get -s IP -k ge ...
- Assetbundle1
AssetBundle运行时加载:来自文件就用CreateFromFile(注意这种方法只能用于standalone程序)这是最快的加载方法也可以来自Memory,用CreateFromMemory( ...