hdu6278-Just h-index

Just h-index

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


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

题意:如果区间内有h个大于等于h的paper,则是h-index,现在问你区间内最大的h是多少

题解:赛场上很自然的想到区间第k大问题,然后发现是主席树模板题十分开心,结果没带板子想死。。。

二分枚举k,如果区间第k大的值大于等于k,则更新答案!

AC代码

#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#define lson l, m
#define rson m + 1, r

using namespace std;

const int maxn = 1e5 + 10;
int L[maxn << 5], R[maxn << 5], sum[maxn << 5];
int tot, a[maxn], T[maxn], ha[maxn];

int bulid(int l, int r){
	int rt = (++tot);
	if(l < r){
		int m = (l + r) >> 1;
		L[rt] = bulid(lson);
		R[rt] = bulid(rson);
	}
	return rt;
}

int update(int pre, int l, int r, int x){
	int rt = (++tot);
	L[rt] = L[pre], R[rt] = R[pre], sum[rt] = sum[pre] + 1;
	if(l < r){
		int m = (l + r) >> 1;
		if(x <= m)
			L[rt] = update(L[pre], lson, x);
		else
			R[rt] = update(R[pre], rson, x);	
	}
	return rt;
}

int query(int u, int v, int l, int r, int k){
	if(l >= r)
		return l;
	int m = (l + r) >> 1, num = sum[L[v]] - sum[L[u]];
	if(num >= k)
		return query(L[u], L[v], lson, k);
	else
		return query(R[u], R[v], rson, k - num);
}

int main(){
//	freopen("in.txt", "r", stdin);
	int n, m;
	while(scanf("%d %d", &n, &m) != EOF){
		tot = 0;
		for(int i = 1; i <= n; i++){
			scanf("%d", &a[i]);
			ha[i] = a[i];
		}
		sort(ha + 1, ha + 1 + n);
		int d = unique(ha + 1, ha + 1 + n) - ha - 1;
		T[0] = bulid(1, d);
		for(int i = 1; i <= n; i++){
			int x = lower_bound(ha + 1, ha + 1 + d, a[i]) - ha;
			T[i] = update(T[i - 1], 1, d, x);
		}
		while(m--){
			int x1, y1, l, r, ans = 1;
			scanf("%d %d", &x1, &y1);
			l = 1, r = y1 - x1 + 1;
			while(l <= r){
				int m = (l + r) >> 1;
//				if(x1 == 1 && y1 == 5)
//					printf("%d %d\n", ha[query(T[x1 - 1], T[y1], 1, d, y1 - x1 + 2 - m)], m);
				if(ha[query(T[x1 - 1], T[y1], 1, d, y1 - x1 + 2 - m)] >= m){
					l = m + 1;
					ans = max(ans, m);
				}
				else
					r = m - 1;
			}
			printf("%d\n", ans);
		}
	} 
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37064135/article/details/80390630
今日推荐