当前位置:网站首页>主席树复习
主席树复习
2022-07-13 16:28:00 【Oops_Corsini】
主席树:可持久化权值线段树
模板(查询区间第k大,单点修改):luogu【模板】可持久化线段树2
利用了前缀和的思想,动态开点
利用已有的且不变动的部分减小空间复杂度
#include<bits/stdc++.h>
#define ll long long
#define maxn 200010
using namespace std;
int n,m;
ll a[maxn];//原数组
ll b[maxn];//离散化数组
int tot;
struct node{
int l,r,sum;
}e[maxn*50];
int cnt,rot[maxn];//根节点数组
void insert(int l,int r,int pre,int &now,int k){
e[++cnt]=e[pre];
now=cnt;
e[now].sum++;
if(l==r)return ;
int mid=(l+r)>>1;
if(k<=mid)insert(l,mid,e[pre].l,e[now].l,k);
else insert(mid+1,r,e[pre].r,e[now].r,k);
}
int query(int l,int r,int L,int R,int k){
if(l==r){
return l;
}
int mid=(l+r)>>1;
int res=e[e[R].l].sum-e[e[L].l].sum;
if(k<=res)return query(l,mid,e[L].l,e[R].l,k);
else return query(mid+1,r,e[L].r,e[R].r,k-res);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
b[i]=a[i];
}
sort(b+1,b+n+1);
tot=unique(b+1,b+n+1)-b-1;
for(int i=1;i<=n;i++){
int k=lower_bound(b+1,b+tot+1,a[i])-b;
insert(1,n,rot[i-1],rot[i],k);
}
for(int L,R,k,i=1;i<=m;i++){
scanf("%d%d%d",&L,&R,&k);
cout<<b[query(1,n,rot[L-1],rot[R],k)]<<endl;
}
return 0;
}
边栏推荐
- Optimisation SQL très pratique
- UE4引擎自定义ScreenPass和MRT输出
- 1748. 唯一元素的和-快速排序,加条件遍历
- 面试题 08.04. 幂集
- Prometheus operator overview
- Power Bi countries / regions SVG coloring map download and use
- Vcenter6 vSphere failed to log in fault handling record
- Foreign key constraint
- [paper notes] deep reinforcement learning for robotic pushing and picking in cluttered environment
- Power Bi SVG colored map: from the world to the whole country, provinces, cities, districts and counties, towns, streets and villages, building space operation skills
猜你喜欢

Foreign key constraint

图像、视频、3D 数据一把抓,不挑食的 AI 模型 Omnivore

ES6:箭头函数用法

A gem enthusiast's strategy of building a station from scratch
Advanced customization of uni app

Analysis of die cutting process of adhesive tape

微信小程序3-小程序生命周期和组件

案例推荐|千亿级、大规模:腾讯超大 Apache Pulsar 集群性能调优实践

VMware vCenter server appliance (VCSA) 6.0 installation process

Applet container technology improves the efficiency of hybrid app development
随机推荐
gcc __attribute__ 关键字举例之 visibility
Excuse me, how much does it cost to open an account for flush stock speculation? Is it safe to open an account?
Debezium series: event deserialization. failure. handling. Mode setting binlog file parsing error debezium processing method
acw 倒水问题(爆搜出所有方案)
Cf1257f make them similar (meet in the middle, simulated annealing)
案例推荐|千亿级、大规模:腾讯超大 Apache Pulsar 集群性能调优实践
GCC rust is approved to be included in the mainline code base, or will meet you in GCC 13
Wechat applet 5 real machine test
【环境配置】PPYOLOE训练自己的数据集(自用)
What made Tron rank among the top three in the public chain after Terra fell?
微信小程序2-WXSS,WXS
新一代云原生消息队列 (二)
Qucs初步使用指南(不是multism)
[200 opencv routines] 228 Extendlbp improved operator of feature description
嵌入式系统 期末复习提纲
[paper notes] temperature priority push and grasp method of dense objects based on deep reinforcement learning
PreScan快速入门到精通第十五讲之道路元素
Microbiome - amplicon 16S analysis and visualization (2022.7 class starts this week)
“双一流”中国地质大学(北京)雄安校区,2025年投用
理想L9搭载旗舰级安全配置,让全家人出行更安全、更便捷