1、主席树是以前缀和形式建立的可持久化线段树。
2、基于动态开结点的存储形式。
3、每次插入一个值时,最多新开O(logn)个结点。
4、空间复杂度O(nlogn)。
5、单次操作时间复杂度O(iogn)。
6、可以查询区间的值域信息。
7、相对于线段树套平衡树的优势:代码简单、速度快。
8、劣势:离散数据结构,难以修改(可以采取对询问分块等方式弥补)。
9、主席树的插入建立运用了可持久化和部分重建的思想。
【例题1】
题目背景
这是个非常经典的主席树入门题——静态区间第K小
数据已经过加强,请使用主席树。同时请注意常数优化
题目描述
如题,给定N个整数构成的序列,将对于指定的闭区间查询其区间内的第K小值。
输入格式
第一行包含两个正整数N、M,分别表示序列的长度和查询的个数。
第二行包含N个整数,表示这个序列各项的数字。
接下来M行每行包含三个整数l,r,kl,r,k , 表示查询区间[l,r][l,r]内的第k小值。
输出格式
输出包含k行,每行1个整数,依次表示每一次查询的结果
输入输出样例
输入 #1
5 5
25957 6405 15770 26287 26465
2 2 1
3 4 1
4 5 1
1 2 2
4 4 1
输出 #1
6405
15770
26287
25957
26287
说明/提示
数据范围:
对于20%的数据满足:1≤N,M≤101≤N,M≤10
对于50%的数据满足:1≤N,M≤1031≤N,M≤103
对于80%的数据满足:1≤N,M≤1051≤N,M≤105
对于100%的数据满足:1≤N,M≤2⋅1051≤N,M≤2⋅105
对于数列中的所有数aiai,均满足−109≤ai≤109−109≤ai≤109
样例数据说明:
N=5,数列长度为5,数列从第一项开始依次为[25957,6405,15770,26287,26465][25957,6405,15770,26287,26465]
第一次查询为[2,2][2,2]区间内的第一小值,即为6405
第二次查询为[3,4][3,4]区间内的第一小值,即为15770
第三次查询为[4,5][4,5]区间内的第一小值,即为26287
第四次查询为[1,2][1,2]区间内的第二小值,即为25957
第五次查询为[4,4][4,4]区间内的第一小值,即为26287
【代码】
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5+5;
int n,m,a[maxn];
vector<int> v;
inline int getid(int x)
{
return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
}
typedef struct Node
{
int l,r,sum;
}no;
no hjt[maxn*40];
int cnt,root[maxn];
void insert(int l,int r,int pre,int &now,int p)//插入
{
hjt[++cnt]=hjt[pre];
now=cnt;
hjt[now].sum++;
if(l==r) return;
int m=(l+r)>>1;
if(p<=m) insert(l,m,hjt[pre].l,hjt[now].l,p);
else insert(m+1,r,hjt[pre].r,hjt[now].r,p);
}
int query(int l,int r,int L,int R,int k)//查询
{
if(l==r) return l;
int m=(l+r)>>1;
int t=hjt[hjt[R].l].sum-hjt[hjt[L].l].sum;
if(k<=t) return query(l,m,hjt[L].l,hjt[R].l,k);
else return query(m+1,r,hjt[L].r,hjt[R].r,k-t);
}
signed main()
{
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
v.push_back(a[i]);
}
sort(v.begin(),v.end());
v.erase(std::unique(v.begin(),v.end()),v.end());
for(int i=1;i<=n;i++)
{
insert(1,n,root[i-1],root[i],getid(a[i]));
}
while(m--)
{
int l,r,k;
scanf("%lld%lld%lld",&l,&r,&k);
int id=query(1,n,root[l-1],root[r],k)-1;
int ans=v[id];
printf("%lld\n",ans);
}
return 0;
}
【说明】
1、lower_bound的使用:
定义于头文件#inxlude<algorithm>
Forwardlt lower_bound(Forwardlt first,Forwardlt last,const T& value);
返回范围指向[first,last)中首个不小于(即大于或等于)value的元素的迭代器,或若找不到这种元素则返回last。
用法:int t=lower_bound(a+l,a+r,k)-a;(a是数组)
这个函数经常用于在一个有序序列中插入一个元素,是进行插入操作后的序列依旧是有序的。
2、unique()函数:
返回参数数组中所有不同的值,并按照从小到大排序。
该算法删除相邻的重复元素,然后重新排列输入范围内的元素,并且返回一个迭代器(容器的长度没变,只是元素顺序改变了),表示无重复的值范围得结束。
在STL中unique函数是一个去重函数, unique的功能是去除相邻的重复元素(只保留一个),其实它并不真正把重复的元素删除,是把重复的元素移到后面去了,然后依然保存到了原数组中,然后 返回去重后最后一个元素的地址,因为unique去除的是相邻的重复元素,所以一般用之前都会要排一下序。
【例题2】
Keen On Everything But Triangle
N sticks are arranged in a row, and their lengths are a1,a2,...,aNa1,a2,...,aN.
There are QQ querys. For ii-th of them, you can only use sticks between lili-th to riri-th. Please output the maximum circumference of all the triangles that you can make with these sticks, or print −1−1 denoting no triangles you can make.
Input
There are multiple test cases.
Each case starts with a line containing two positive integers N,Q(N,Q≤105)N,Q(N,Q≤105).
The second line contains NN integers, the ii-th integer ai(1≤ai≤109)ai(1≤ai≤109) of them showing the length of the ii-th stick.
Then follow QQ lines. ii-th of them contains two integers li,ri(1≤li≤ri≤N)li,ri(1≤li≤ri≤N), meaning that you can only use sticks between lili-th to riri-th.
It is guaranteed that the sum of NNs and the sum of QQs in all test cases are both no larger than 4×1054×105.
Output
For each test case, output QQ lines, each containing an integer denoting the maximum circumference.
Sample Input
5 3
2 5 6 5 2
1 3
2 4
2 5
Sample Output
13
16
16
【代码】
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#define int long long
using namespace std;
const int maxn=2e5+5;
int n,m,a[maxn];
vector<int> v;
inline int getid(int x)
{
return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
}
typedef struct Node
{
int l,r,sum;
}no;
no hjt[maxn*40];
int cnt,root[maxn];
void insert(int l,int r,int pre,int &now,int p)
{
hjt[++cnt]=hjt[pre];
now=cnt;
hjt[now].sum++;
if(l==r) return ;
int m=(l+r)>>1;
if(p<=m) return insert(l,m,hjt[pre].l,hjt[now].l,p);
return insert(m+1,r,hjt[pre].r,hjt[now].r,p);
}
int query(int l,int r,int L,int R,int k)
{
if(l==r) return l;
int m=(l+r)>>1;
int t=hjt[hjt[R].l].sum-hjt[hjt[L].l].sum;
if(k<=t) return query(l,m,hjt[L].l,hjt[R].l,k);
return query(m+1,r,hjt[L].r,hjt[R].r,k-t);
}
signed main()
{
while(cin>>n>>m)//多组输入
{
v.clear();
for(int i=1;i<=n;i++)
{
cin>>a[i];
v.push_back(a[i]);
}
sort(v.begin(),v.end());
v.erase(std::unique(v.begin(),v.end()),v.end());
for(int i=1;i<=n;i++)
{
insert(1,n,root[i-1],root[i],getid(a[i]));
}
while(m--)
{
int l,r,f=0;
cin>>l>>r;
int len=r-l+1;
for(int i=1;i<=r-l-1;i++)
{
int first=query(1,n,root[l-1],root[r],len-i+1)-1;//最长的边
int second=query(1,n,root[l-1],root[r],len-i)-1;//第二长的边
int tired=query(1,n,root[l-1],root[r],len-i-1)-1;//最短的边
int e1=v[first],e2=v[second],e3=v[tired];
if(e2+e3>e1)//第二、第三长的边的和大于最长的边成立时,该三角形周长最大
{
cout<<e1+e2+e3<<"\n";
f=1;
break;//找到合适的三角形就停止循环
}
}
if(f==0)
cout<<"-1"<<"\n";
}
}
return 0;
}