主席树

推荐博客:

https://blog.csdn.net/pengwill97/article/details/80920143

https://www.cnblogs.com/AKMer/p/9956734.html

模板:求区间静态第k大

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=2e5+10; 
 4 struct node{
 5     int l,r,num;
 6 }tree[maxn*100];
 7 int size=0,a[maxn],refl[maxn],root[maxn];
 8 void update(int &x,int y,int l,int r,int dex){
 9     x=++size;
10     tree[x]=tree[y]; tree[x].num++;
11     if(l==r) return ;
12     int mid=(l+r)>>1;
13     if (dex<=mid) update(tree[x].l,tree[y].l,l,mid,dex);
14     else update(tree[x].r,tree[y].r,mid+1,r,dex);
15 }
16 int query(int x,int y,int l,int r,int kk){
17     if (l==r) return l;
18     int mid=(l+r)>>1;
19     int tmp=tree[tree[y].l].num-tree[tree[x].l].num;
20     if (tmp>=kk) return query(tree[x].l,tree[y].l,l,mid,kk);
21     else return query(tree[x].r,tree[y].r,mid+1,r,kk-tmp); 
22 }
23 int main(){
24     int n,m,l,r,k;scanf("%d%d",&n,&m);
25     for (int i=1;i<=n;i++){
26         scanf("%d",&a[i]);
27         refl[i]=a[i];
28     } 
29     sort(refl+1,refl+1+n);
30     int tot=unique(refl+1,refl+1+n)-(refl+1);
31     for (int i=1;i<=n;i++){
32         a[i]=lower_bound(refl+1,refl+1+tot,a[i])-refl;
33         update(root[i],root[i-1],1,tot,a[i]);
34     }
35     while (m--){
36         scanf("%d%d%d",&l,&r,&k);
37         int tmp=query(root[l-1],root[r],1,tot,k);
38         printf("%d\n",refl[tmp]);
39     }
40     return 0;
41 } 

 

posted @ 2019-08-28 14:16  Changer-qyz  阅读(...)  评论(...编辑  收藏