码迷,mamicode.com
首页 > 其他好文 > 详细

Just h-index

时间:2019-09-02 23:39:56      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:==   chmod   eve   eof   mit   otto   typedef   long   color   

Just h-index

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


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

 

#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
const int maxn = 1e5 + 10;

int n, q, c[maxn];
vector<int> e;
int root[maxn * 40], sum[maxn * 40], tot;
int ls[maxn * 40], rs[maxn * 40];

inline void init() {
    e.clear();
    memset(root, 0, sizeof(root));
    memset(ls, 0, sizeof(ls));
    memset(rs, 0, sizeof(rs));
    tot = 0;
}

inline void build(int &rt, int l, int r) {
    rt = ++tot;
    sum[rt] = 0;
    if (l == r)return;
    int mid = l + r >> 1;
    build(ls[rt], l, mid);
    build(rs[rt], mid + 1, r);
}

inline void update(int l, int r, int pre, int &rt, int pos) {
    rt = ++tot;
    ls[rt] = ls[pre];
    rs[rt] = rs[pre];
    sum[rt] = sum[pre] + 1;
    if (l == r)return;
    int mid = l + r >> 1;
    if (pos <= mid) {
        update(l, mid, ls[pre], ls[rt], pos);
    } else {
        update(mid + 1, r, rs[pre], rs[rt], pos);
    }
}

inline int query(int l, int r, int pre, int &rt, int pos) {
    if (l == r)return l;
    int mid = l + r >> 1;
    int cur = sum[ls[rt]] - sum[ls[pre]];
    if (cur >= pos) {
        return query(l, mid, ls[pre], ls[rt], pos);
    } else {
        return query(mid + 1, r, rs[pre], rs[rt], pos - cur);
    }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.txt", "r", stdin);
#endif
    while (scanf("%d%d", &n, &q) != EOF) {
        init();
        for (register int i = 1; i <= n; ++i) {
            scanf("%d", &c[i]);
            e.emplace_back(c[i]);
        }
        sort(e.begin(), e.end());
        e.erase(unique(e.begin(), e.end()), e.end());
        build(root[0], 1, n);

        for (register int i = 1; i <= n; ++i) {
            c[i] = lower_bound(e.begin(), e.end(), c[i]) - e.begin() + 1;
        }
        for (register int i = 1; i <= n; ++i) {
            update(1, n, root[i - 1], root[i], c[i]);
        }
        //cerr<<"***"<<endl;
        int l, r;
        while (q--) {
            scanf("%d%d", &l, &r);
            int mx = r - l + 1;
            int L = 1, R = mx, res = 0;
            while (L <= R) {
                int mid = L + R >> 1;
                int rk = mx - mid + 1;
                int id = query(1, n, root[l - 1], root[r], rk);
                if (e[id - 1] >= mid) {
                    res = mid;
                    L = mid + 1;
                } else {
                    R = mid - 1;
                }
            }
            printf("%d\n", res);
        }
    }
    return 0;
}

 

Just h-index

标签:==   chmod   eve   eof   mit   otto   typedef   long   color   

原文地址:https://www.cnblogs.com/czy-power/p/11450429.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!