随笔 - 531  文章 - 0  评论 - 3  阅读 - 10215 

可持久化线段树

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <cstdio>
#include <algorithm>
using namespace std ;
const int maxn=200010;
int a[maxn],b[maxn],blen,n,CNT;
int sum[maxn<<5],lc[maxn<<5],rc[maxn<<5],rt[maxn<<5];
 
 int update(int k,int l,int r,int x){
   int t=++CNT,md=(l+r)>>1;
   lc[t]=lc[k],rc[t]=rc[k],sum[t]=sum[k]+1;
    
   if(l==r) return t;
   if(x<=md) lc[t]=update(lc[t],l,md,x);
    else rc[t]=update(rc[t],md+1,r,x);
    return t;
}
 
int query(int l,int r,int x,int y,int kth){
   int md=(l+r)>>1,t=sum[lc[y]]-sum[lc[x]];
   if(l==r) return l;
   if(kth<=t) return query(l,md,lc[x],lc[y],kth);
   else return query(md+1,r,rc[x],rc[y],kth-t);
}
 
int main(){
   int p,i,x,y,k,m;
   scanf("%d%d",&n,&m);
   for(i=1;i<=n;i++) scanf("%d",a+i),b[i]=a[i];
    
   sort(b+1,b+n+1);
   blen=unique(b+1,b+n+1)-b-1;
    
   for(i=1;i<=n;i++){
       p=lower_bound(b+1,b+blen+1,a[i])-b;
        
       rt[i]=update(rt[i-1],1,blen,p);
    }
   while(m--){
       scanf("%d%d%d",&x,&y,&k);
   printf("%d\n",b[query(1,blen,rt[x-1],rt[y],k)]);
   }
}

 

posted on   towboat  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示