Just h-index
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.
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.
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
* 1≤n,q≤105
* 1≤ai≤n
* 1≤li≤ri≤n
* The sum of n does not exceed 250,000.
* The sum of q does not exceed 250,000.
## Constraint
* 1≤n,q≤105
* 1≤ai≤n
* 1≤li≤ri≤n
* 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; }