日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Just $h$-index HDU - 6278(主席树找区间大于等于k的个数)

發(fā)布時間:2023/12/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Just $h$-index HDU - 6278(主席树找区间大于等于k的个数) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

The hh-index of an author is the largest hh where he has at least hh papers with citations not less than hh.

Bobo has published nn papers with citations a1,a2,…,ana1,a2,…,an respectively.
One day, he raises qq questions. The ii-th question is described by two integers lili and riri, asking the hh-index of Bobo if has only published papers with citations ali,ali+1,…,ariali,ali+1,…,ari.
Input
The input consists of several test cases and is terminated by end-of-file.

The first line of each test case contains two integers nn and qq.
The second line contains nn integers a1,a2,…,ana1,a2,…,an.
The ii-th of last qq lines contains two integers lili and riri.
Output
For each question, print an integer which denotes the answer.

Constraint

  • 1≤n,q≤1051≤n,q≤105
  • 1≤ai≤n1≤ai≤n
  • 1≤li≤ri≤n1≤li≤ri≤n
  • The sum of nn does not exceed 250,000250,000.
  • The sum of qq does not exceed 250,000250,000.
    Sample Input
    5 3
    1 5 3 2 1
    1 3
    2 4
    1 5
    5 1
    1 2 3 4 5
    1 5
    Sample Output
    2
    2
    2
    3
    題意:一組長度為n的序列,m次查詢。每次查詢給出兩個數(shù)字l和r。詢問的是找出最大的數(shù)字k,使得l到r之間的數(shù)字至少有k個數(shù)字大于等于k。
    思路很簡單,二分答案,主席樹上求區(qū)間大于等于k的數(shù)字個數(shù)。
    很多題解說的是求區(qū)間第k大,其實沒有那么麻煩,直接離散化之后二分答案k,求大于等于k的個數(shù)就行。根據(jù)個數(shù)縮短區(qū)間。
    代碼如下:
#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=1e5+100; struct node{int l;int r;int sum; }p[maxx*40]; int a[maxx],b[maxx],root[maxx]; int n,m,tot; /*--------------主席樹--------------*/ inline int build(int l,int r) {int cur=++tot;p[cur].sum=0;if(l==r) return cur;int mid=l+r>>1;p[cur].l=build(l,mid);p[cur].r=build(mid+1,r);return cur; } inline int update(int rot,int pos,int l,int r) {int cur=++tot;p[cur]=p[rot];p[cur].sum++;if(l==r) return cur;int mid=l+r>>1;if(pos<=mid) p[cur].l=update(p[rot].l,pos,l,mid);else p[cur].r=update(p[rot].r,pos,mid+1,r);return cur; } inline int query(int lrot,int rrot,int l,int r,int pos) {if(l==r) return p[rrot].sum-p[lrot].sum;int mid=l+r>>1;int sum=0;if(pos<=mid) sum=query(p[lrot].l,p[rrot].l,l,mid,pos)+p[p[rrot].r].sum-p[p[lrot].r].sum;else sum=query(p[lrot].r,p[rrot].r,mid+1,r,pos);return sum; } int main() {int l,r;while(~scanf("%d%d",&n,&m)){tot=0;for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i];sort(b+1,b+1+n);int len=unique(b+1,b+1+n)-b-1;//離散化root[0]=build(1,len);for(int i=1;i<=n;i++) root[i]=update(root[i-1],lower_bound(b+1,b+1+len,a[i])-b,1,n);while(m--){scanf("%d%d",&l,&r);int L=1,R=n,mid,ans;while(L<=R){mid=L+R>>1;int zz=query(root[l-1],root[r],1,n,lower_bound(b+1,b+1+len,mid)-b);if(zz>=mid){ans=mid;L=mid+1;}else R=mid-1;}printf("%d\n",ans);}}return 0; }

努力加油a啊,(o)/~

總結(jié)

以上是生活随笔為你收集整理的Just $h$-index HDU - 6278(主席树找区间大于等于k的个数)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。