hdu Frequent values (线段树 求最值)

Problem Description

You are given a sequence of n integers a1 , a2 , ... , an 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 ai , ... , aj.

 

 

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 a1 , ... , an (-100000 ≤ ai ≤ 100000, for each i ∈ {1, ..., n}) separated by spaces. You can assume that for each i ∈ {1, ..., n-1}: ai ≤ ai+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
**************************************************************************************************************************
先说题意:
给定一个非降的数列,以下有Q个询问,每个询问给定一个区间的首尾标号,求这个区间中,出现次数最多的数字个数。
典型的线段树,求区间内的最值
**************************************************************************************************************************
  1 #include <iostream>
  2 #include <string>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <queue>
  6 #include <cstdio>
  7 #define MAX 100050
  8 #define INF 0X7FFFFFFFF
  9 #define LL long long
 10 #define mem(a,b) memset(a,b,sizeof(a))
 11 #define L(x) x<<1
 12 #define R(x) x<<1|1
 13 using namespace std;
 14 int data[MAX],vis[MAX],maxx;
 15 int n,i,j,k,q;
 16 struct node
 17 {
 18     int le,ri,mid,value;
 19 }tree[4*MAX];
 20 
 21 struct point
 22 {
 23     int st,en;
 24 }cnt[MAX];
 25 void up(int num)
 26 {
 27     tree[num].value=max(tree[L(num)].value,tree[R(num)].value);
 28 }
 29 void build(int le,int ri,int root)
 30 {
 31     tree[root].le=le;
 32     tree[root].ri=ri;
 33     tree[root].mid=(le+ri)>>1;
 34     if(le==ri)
 35     {
 36         tree[root].value=cnt[le].en-cnt[le].st+1;
 37         return;
 38     }
 39     build(le,tree[root].mid,L(root));
 40     build(tree[root].mid+1,ri,R(root));
 41     up(root);
 42 }
 43 
 44 int query(int le,int ri,int root)
 45 {
 46     if(le<=tree[root].le&&tree[root].ri<=ri)
 47         return tree[root].value;
 48     if(le>tree[root].mid)
 49         return query(le,ri,R(root));
 50     else
 51         if(ri<=tree[root].mid)
 52           return query(le,ri,L(root));
 53     else
 54     {
 55         return max(query(le,tree[root].mid,L(root)),query(tree[root].mid+1,ri,R(root)));
 56     }
 57 }
 58 
 59 int main()
 60 {
 61 
 62    while(scanf("%d",&n)==1)
 63    {
 64        if(n==0)
 65         break;
 66         scanf("%d",&q);
 67        for(i=1;i<=n;i++)
 68         scanf("%d",&data[i]);
 69        memset(vis,0,sizeof(vis));
 70        int t=1;
 71        vis[1]=t;
 72        cnt[t].st=1;
 73        if(n==1)
 74         cnt[t].en=1;
 75        for(i=2;i<=n;i++)
 76        {
 77           if(data[i]!=data[i-1])
 78           {
 79               cnt[t].en=i-1;
 80               t++;
 81               cnt[t].st=i;
 82               vis[i]=t;
 83           }
 84           else
 85             vis[i]=t;
 86        }
 87        vis[n]=t;
 88        cnt[t].en=n;
 89        build(1,t,1);
 90        int a,b;
 91        for(i=1;i<=q;i++)
 92        {
 93            scanf("%d%d",&a,&b);
 94            if(vis[a]==vis[b])
 95            {
 96                printf("%d\n",b-a+1);
 97                continue;
 98            }
 99            if(vis[b]-vis[a]==1)
100            {
101                printf("%d\n",max(cnt[vis[a]].en-a+1,b-cnt[vis[b]].st+1));
102                continue;
103            }
104            int p=max(cnt[vis[a]].en-a+1,b-cnt[vis[b]].st+1);
105            printf("%d\n",max(p,query(vis[a]+1,vis[b]-1,1)));
106        }
107     }
108 }
View Code

 

posted @ 2014-01-20 17:16  persistent codeants  阅读(492)  评论(0编辑  收藏  举报