Just h-index+莫队+二分+树状数组


Just h-index

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 232    Accepted Submission(s): 111


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

Bobo has published n papers with citations a1,a2,,an respectively.
One day, he raises q questions. The i-th question is described by two integers li and ri, asking the h-index of Bobo if has *only* published papers with citations ali,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 n and q.
The second line contains n integers a1,a2,,an.
The i-th of last q lines contains two integers li and ri.
 

Output
For each question, print an integer which denotes the answer.

## Constraint

* 1n,q105
* 1ain
* 1lirin
* The sum of n does not exceed 250,000.
* The sum of q does not exceed 250,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
 

Source
 

Recommend
liuyiding
#define happy

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;


#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)

#define all(a) (a).begin(),(a).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define f first
#define s second

ll rd(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

const int MAXN=1e5+10;

int tree[MAXN];
int blocksize;
int block[MAXN];
int ans[MAXN];
int a[MAXN],N,Q;

inline int lowbit(int x){
    return x&-x;
}


struct query{
    int l,r,id;
}q[MAXN];

bool cmp(query a,query b){
    if(block[a.l]==block[b.l])
        return a.r<b.r;
    return block[a.l]<block[b.l];
}
int tot;

void add(int x,int C){
    for(int i=x;i<=MAXN;i+=lowbit(i)){
        tree[i]+=C;
    }
}

int sum(int x){
    int ans=0;
    for(int i=x;i;i-=lowbit(i))
        ans+=tree[i];
    return ans;
}

bool judge(int x){
    if(tot-sum(x-1)>=x)
        return true;
    return false;
}


void solve(){
    int l=1,r=1;
    tot=1;
    add(a[1],1);
    rep(i,0,Q-1){
        while(q[i].r>r){r++;add(a[r],1);tot++;}
        while(q[i].r<r){add(a[r],-1);r--;tot--;}
        while(q[i].l<l){l--;add(a[l],1);tot++;}
        while(q[i].l>l){add(a[l],-1);l++;tot--;}
        int ll=1,rr=N;
        int m;
        while(ll<=rr){
            m=(ll+rr)/2;
            if(judge(m)){
                ll=m+1;
            }else rr=m-1;
        }
        ans[q[i].id]=rr;
    }
}


int main(){
#ifdef happy
    freopen("in.txt","r",stdin);
#endif
    while(~scanf("%d%d",&N,&Q)){
        memset(tree,0,sizeof(tree));
        blocksize=sqrt(N);
        rep(i,1,N){
            a[i]=rd();
            block[i]=i/blocksize+1;
        }
        rep(i,0,Q-1){
            q[i].l=rd();
            q[i].r=rd();
            q[i].id=i;
        }
        sort(q,q+Q,cmp);
        solve();
        rep(i,0,Q-1)
        printf("%d\n",ans[i]);
    }
}

 

猜你喜欢

转载自blog.csdn.net/ujn20161222/article/details/80540071