HDU-6278 Just h-index (主席树+二分)

放肆的年华 提交于 2020-02-17 15:17:59

题目描述

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

Bobo has published n papers with citations a1,a2,,ana_1,a_2,…,a_n 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,,aria_{l_i},a_{l_{i+1}},…,a_{r_i}.


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 n integers a1,a2,,ana_1,a_2,…,a_n.
The ii-th of last qq lines contains two integers lil_i and rir_i.

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

Constraint

  • 1≤n,qn,q≤105
  • 1≤aia_i≤n
  • 1≤lil_irir_i≤n
  • The sum of nn does not exceed 250,000.
  • The sum of qq does not exceed 250,000.

题解

给定序列a, 在指定区间[L,R][L,R]中找到最大的数字h,使其满足在a[L] ~ a[R]范围内大于等于h的数字不少于h个。
需要涉及区间内有序的问题,可以想到用主席树来做。

二分枚举h,用主席树查找在区间内大于等于h的数字个数是否大于等于h.

(代码借鉴学长lcy大佬,lcytql)





#include<bits/stdc++.h>
using namespace std;
const long long maxn=1e6+10;

struct node{
    int sum;
    int l,r;
}tree[maxn*30];

void reset(int x){
    //这个函数真的 女少 啊,把0节点直接代替初始空线段树所有的节点
    tree[x].sum = 0;
    tree[x].l = tree[x].r = 0;
}

int root[maxn], tot;
int n,q;

int build(int l, int r, int old, int x){
    tot++;
    int temp = tot;
    tree[tot] = tree[old];
    tree[tot].sum++;
    if(l==r) return temp;
    int mid = (l+r)>>1;
    if(x<=mid)
        tree[temp].l=build(l, mid, tree[old].l,x);
    else
        tree[temp].r=build(mid+1, r, tree[old].r, x);
    return temp;
}

int query(int l, int r, int lrt, int rrt, int x){
    if(l==r)
        return tree[rrt].sum - tree[lrt].sum;
    int mid=(l+r)>>1;
    if(x>mid)
        return serch(mid+1, r, tree[lrt].r, tree[rrt].r, x);
    else{
        int rr = tree[rrt].r, lr = tree[lrt].r;
        return tree[rr].sum - tree[lr].sum + serch(l, mid, tree[lrt].l, tree[rrt].l, x);
    } //如果h在区间mid右边,则大于h的区间仍然在右子树上,搜索右子树。
  //否则整个右子树的值都是大于h的一部分,并且在h到mid间也可能存在大于h的数,继续搜索左子树。
}

int solve(int x, int y){
    int l, r, mid, ans;
    ans = 1;
    l = 1;
    r = n;
    while(l<=r){
        mid = (l+r)>>1;
        if(query(1, n, root[x-1], root[y], mid) >= mid){
            ans = mid;
            l = mid + 1;
        }else{
            r = mid - 1;
        }
    }
    return ans;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    //freopen("test.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    
    while(cin>>n>>q){
        tot=0;
        reset(0);
        for(int i=1;i<=n;i++){
            int x;
            cin>>x;
            root[i]=build(1,n,root[i-1],x);
        }
        for(int i=1;i<=q;i++){
            int l,r;
            cin >> l >> r;
            cout<<solve(l,r)<<endl;
        }
    }
    
    return 0;
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!