hdu 1806 Frequent values(二分+线段树)

Frequent values

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1050 Accepted Submission(s): 375


Problem Description
You are given a sequence of n integers a 1 , a 2 , ... , a n in non-decreasing order. In addition to that, you are given several queries consisting of indices i and j (1 ≤ i ≤ j ≤ n). For each query, determine the most frequent value among the integers a i , ... , a j .


Input
The input consists of several test cases. Each test case starts with a line containing two integers n and q (1 ≤ n, q ≤ 100000). The next line contains n integers a 1 , ... , a n(-100000 ≤ a i ≤ 100000, for each i ∈ {1, ..., n}) separated by spaces. You can assume that for each i ∈ {1, ..., n-1}: a i ≤ a i+1. The following q lines contain one query each, consisting of two integers i and j (1 ≤ i ≤ j ≤ n), which indicate the boundary indices for the query.

The last test case is followed by a line containing a single 0.


Output
For each query, print one line with one integer: The number of occurrences of the most frequent value within the given range.

Sample Input
  
  
10 3 -1 -1 1 1 1 1 3 10 10 10 2 3 1 10 5 10 0

Sample Output
  
  
1 4 3
样例:
其余你应该知道怎么做了!
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
using namespace std;

const int maxn = 100010;
int v[maxn] , sum[maxn];
struct tree{
	int l , r , Max;
	tree(int a = 0 , int b = 0 , int c = 0){
		l = a , r = b , Max = c;
	}
}a[4*maxn];
int n , q , cnt;

void build(int l , int r , int k){
	a[k].l = l;
	a[k].r = r;
	if(l == r){
		a[k].Max = v[l];
	}else{
		int mid = (l+r)/2;
		build(l , mid , 2*k);
		build(mid+1 , r , 2*k+1);
		a[k].Max = max(a[2*k].Max , a[2*k+1].Max);
	}
}

int querry(int l , int r , int k){
	if(l <= a[k].l && a[k].r <= r){
		return a[k].Max;
	}else{
		int mid = (a[k].l+a[k].r)/2;
		if(mid >= r) return querry(l , r , 2*k);
		else if(mid < l) return querry(l , r , 2*k+1);
		else return max(querry(l , mid , 2*k) , querry(mid+1 , r , 2*k+1));
	}
}

void initial(){
	cnt = 1;
	for(int i = 0; i < maxn; i++){
		v[i] = 0;
		sum[i] = 0;
	}
}

void readcase(){
	cin >> q;
	int num , pre;
	cin >> pre;
	v[1] = 1;
	for(int i = 1; i < n; i++){
		scanf("%d" , &num);
		if(num == pre) v[cnt]++;
		else{
			cnt++;
			v[cnt]++;
			pre = num;
		}
	}
	for(int i = 1; i <= cnt; i++){
		sum[i] = sum[i-1]+v[i];
	}
}

int binary_search(int k){
	int l = 1 , r = cnt;
	while(l < r){
		int mid = (l+r)/2;
		if(sum[mid] >= k){
			r = mid;
		}else{
			l = mid+1;
		}
	}
	return l;
}

void computing(){
	build(1 , cnt , 1);
	int i , j , ans;
	while(q--){
		//cin >> i >> j;
		scanf("%d%d" , &i ,&j);
		int L = binary_search(i);
		int R = binary_search(j);
		if(L == R) printf("%d\n" , j-i+1);
		else{
			ans = max(sum[L]-i+1 , j-sum[R-1]);
			if(L+1 <= R-1) ans = max(ans , querry(L+1 , R-1 , 1));
			printf("%d\n" , ans);
		}
	}
}

int main(){
	while(cin >> n && n){
		initial();
		readcase();
		computing();
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值