POJ2104 K-th Number[主席树]【学习笔记】
| Time Limit: 20000MS | Memory Limit: 65536K | |
| Total Submissions: 51440 | Accepted: 17594 | |
| Case Time Limit: 2000MS | ||
Description
That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?"
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.
Input
The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given.
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).
Output
Sample Input
7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3
Sample Output
5
6
3
Hint
Source
document:
1.http://blog.csdn.net/metalseed/article/details/8045038
2.http://www.cnblogs.com/oyking/p/3230296.html
1.两种建树写法,貌似用引用比较快
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e5+;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,m,mp[N],l,r,k;
struct data{
int v,id;
bool operator <(const data &r)const{return v<r.v;}
}a[N];
struct node{
int lc,rc,size;
}t[N*];
int cnt=,root[N];
void insert(int num,int &x,int l,int r){//printf("ins %d %d %d\n",l,r,x);
cnt++;
t[cnt]=t[x];x=cnt;
++t[x].size;
if(l==r) return;
int mid=(l+r)>>;
if(num<=mid) insert(num,t[x].lc,l,mid);
else insert(num,t[x].rc,mid+,r);
}
int ins(int num,int pre,int l,int r){
int x=++cnt;
t[x]=t[pre]; ++t[x].size;
if(l==r) return x;
int mid=(l+r)>>;
if(num<=mid) t[x].lc=ins(num,t[x].lc,l,mid);
else t[x].rc=ins(num,t[x].rc,mid+,r);
return x;
}
int query(int i,int j,int l,int r,int k){
if(l==r) return l;
int ls=t[t[j].lc].size-t[t[i].lc].size;
int mid=(l+r)>>;
if(k<=ls) return query(t[i].lc,t[j].lc,l,mid,k);
else return query(t[i].rc,t[j].rc,mid+,r,k-ls);
} int main(){
n=read();m=read();
for(int i=;i<=n;i++) a[i].v=read(),a[i].id=i;
sort(a+,a++n);
for(int i=;i<=n;i++) mp[a[i].id]=i; for(int i=;i<=n;i++){
//root[i]=ins(mp[i],root[i-1],1,n);
root[i]=root[i-];
insert(mp[i],root[i],,n);
}
while(m--){
l=read();r=read();k=read();
printf("%d\n",a[query(root[l-],root[r],,n,k)].v); }
}
[2017-03-02]
在扔上一点课件上的东西,虽然感觉跟前面的总结有点重复,但还是有点用吧
函数式编程:不修改,只新增(保留所有的历史版本)。
@ 考虑一次单点修改对整棵树的信息的影响;只有一条链上的
信息真正改变了。
@ 函数式线段树:对于线段树所有的单点修改操作,不真正的
修改,而是通过新增节点的方式来构建。
由于除了链上的信息是不变的,所以把直接指向它们就可以
了。
@ 所以整棵线段树是动态的,要用动态的节点来实现。
@ 只需要记录根节点,就能访问“第x 次修改后的线段树”
了。
新模板:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lc(x) t[x].lc
#define rc(x) t[x].rc
typedef long long ll;
const int N=1e5+;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,m,mp[N],l,r,k;
struct Fode{
int v,id;
bool operator <(const data &r)const{return v<r.v;}
}a[N];
struct node{
int lc,rc,size;
}t[N*];
int sz,root[N];
void fIns(int &x,int l,int r,int p){
t[++sz]=t[x];x=sz;
t[x].size++;
if(l==r) return;
int mid=(l+r)>>;
if(p<=mid) fIns(t[x].lc,l,mid,p);
else fIns(t[x].rc,mid+,r,p);
}
int fQue(int x,int y,int l,int r,int k){
if(l==r) return l;
int lsize=t[lc(y)].size-t[lc(x)].size;
int mid=(l+r)>>;
if(k<=lsize) return fQue(lc(x),lc(y),l,mid,k);
else return fQue(rc(x),rc(y),mid+,r,k-lsize);
} int main(){
freopen("in","r",stdin);
n=read();m=read();
for(int i=;i<=n;i++) a[i].v=read(),a[i].id=i;
sort(a+,a++n);
for(int i=;i<=n;i++) mp[a[i].id]=i; for(int i=;i<=n;i++) root[i]=root[i-],fIns(root[i],,n,mp[i]);
while(m--){
l=read();r=read();k=read();
printf("%d\n",a[fQue(root[l-],root[r],,n,k)].v);
}
}
POJ2104 K-th Number[主席树]【学习笔记】的更多相关文章
- 主席树学习笔记(静态区间第k大)
题目背景 这是个非常经典的主席树入门题——静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输出 ...
- 主席树学习笔记-hdu-2665
主席树就是对每个历史版本都建了一颗线段树,这样我们在统计一些问题的时候,对于一个区间[L,R]的询问,就可以利用前缀和的思想找到第L-1和第R颗历史版本的线段树来处理查找.由于这样空间需求就增大了,注 ...
- 【poj2104】K-th Number 主席树
题目描述 You are working for Macrohard company in data structures department. After failing your previou ...
- poj2104 k-th number 主席树入门讲解
poj2104 k-th number 主席树入门讲解 定义:主席树是一种可持久化的线段树 又叫函数式线段树 刚开始学是不是觉得很蒙逼啊 其实我也是 主席树说简单了 就是 保留你每一步操作完成之后 ...
- zkw线段树学习笔记
zkw线段树学习笔记 今天模拟赛线段树被卡常了,由于我自带常数 \(buff\),所以学了下zkw线段树. 平常的线段树无论是修改还是查询,都是从根开始递归找到区间的,而zkw线段树直接从叶子结点开始 ...
- poj 2104 K-th Number 主席树+超级详细解释
poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是 ...
- 仙人掌&圆方树学习笔记
仙人掌&圆方树学习笔记 1.仙人掌 圆方树用来干啥? --处理仙人掌的问题. 仙人掌是啥? (图片来自于\(BZOJ1023\)) --也就是任意一条边只会出现在一个环里面. 当然,如果你的图 ...
- 线段树学习笔记(基础&进阶)(一) | P3372 【模板】线段树 1 题解
什么是线段树 线段树是一棵二叉树,每个结点存储需维护的信息,一般用于处理区间最值.区间和等问题. 线段树的用处 对编号连续的一些点进行修改或者统计操作,修改和统计的复杂度都是 O(log n). 基础 ...
- [POJ2104] K – th Number (可持久化线段树 主席树)
题目背景 这是个非常经典的主席树入门题--静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个正整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输 ...
随机推荐
- 基于CkEditor实现.net在线开发之路(4)快速布局,工具箱,模板载入,tab选项卡简单说明与使用
上一章给常用的from表单控件属性页面,进行了简单说明和介绍,但是由于是在网页中做界面设计,操作肯定没有桌面应用程序方便,便捷,为了更方便的布局与设计,今天我主要说一下快速布局,工具箱,tab选项卡, ...
- java 中包的概念,常量,静态与继承
一.包的概念:创建,使用. 1.新建包: 最上面一行,之前不能再有其它代码了. package 包名; 注意:包名一般都有命名的规范.例如:com.itnba.maya.zy(从大到小). 2.使用包 ...
- linux下安装mysql
下载Mysql包 因为mysql比较大,我们不能像安装nginx和php那样,通过下载源码,编译成二进制安装.mysql安装比php和nginx稍微麻烦一点. 这里mysql我们直接下载编译好的二进制 ...
- spring入门(七)【springMVC返回json串】
现在多数的应用为了提高交互性多使用异步刷新,即在不刷新整个页面的情况下,只刷新局部,局部刷新用得最多就是ajax,ajax和后台进行交互的数据格式使用的最多的是JSON,这里简单描述,在springm ...
- spring整合hibernate
spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...
- node-inspector调试工具应用
一.起因 想调试某些开源的nodejs项目,然后就选择了node-inspector插件. 他的优点: 1.可以借用chrome或firefox浏览器进行调试,与前端调试融合. 2.配置简单 二.必备 ...
- 推荐8个实现 SVG 动画的 JavaScript 库
SVG 是一种分辨率无关的图形(矢量图形).这意味着它在任何类型的屏幕都不会遭受任何质量损失.除此之外,你可以让 SVG 灵活现一些动画效果.这篇文章就给大家推荐8个实现 SVG 动画的 JavaSc ...
- 解决sea.js引用jQuery提示$ is not a function的问题
在使用sea.js的如下写法引用jQuery文件时, //main.jsdefine(function(require,exports,module){ var $ = require('jquery ...
- Linux安全基础:shell及一些基础命令
1.什么是shell?Shell是用户和Linux操作系统之间的接口.Linux中有多种shell,其中缺省使用的是Bash. 2.shell的分类(1)bash bash shell 是 Bourn ...
- Google C++单元测试框架GoogleTest---TestFixture使用
一.测试夹具(Test Fixtures):对多个测试使用相同的数据配置 如果你发现自己写了两个或更多的测试来操作类似的数据,你可以使用测试夹具.它允许您为几个不同的测试重复使用相同的对象配置. 要创 ...