UVA 11235 Frequent values

Frequent values

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 Specification

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 Specification

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
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<vector>
 4 #include<cmath>
 5 #include<queue>
 6 #include<string>
 7 #include<map>
 8 #include<cstring>
 9 #include<algorithm>
10 using namespace std;
11 typedef long long ll;
12 typedef unsigned long long ull;
13 const int maxn=1e5+5;
14 struct code
15 {
16     int l,r,id;
17 }p[maxn];
18 int val[maxn],cnt[maxn],d[maxn][32],no,n,q;
19 void rmq_init()
20 {
21     for(int i=1;i<=no;i++)d[i][0]=cnt[i];
22     for(int j=1;(1<<j)<=no;j++)
23         for(int i=1;i+(1<<j)<=no;i++)
24             d[i][j]=max(d[i][j-1],d[i+(1<<(j-1))][j-1]);
25 }
26 int rmq(int l,int r)
27 {
28     if(l>r)return 0;
29     int k=0;
30     while(1<<(1+k)<=r-l+1)k++;
31     return max(d[l][k],d[r-(1<<k)+1][k]);
32 }
33 int main()
34 {
35     while(scanf("%d",&n),n)
36     {
37         scanf("%d",&q);
38         memset(cnt,0,sizeof(cnt));
39         memset(d,0,sizeof(d));
40         no=0;
41         for(int i=1;i<=n;i++)
42         {
43             int tt;
44             scanf("%d",&tt);
45             if(tt!=val[no-1]||i==1)
46             {
47                 val[no++]=tt;
48                 cnt[no]=1;
49             }
50             else
51                 cnt[no]++;
52         }
53         int i=1,num=1;
54         for(int j=1;j<=no;j++)
55         {
56             for(int t=0;t<cnt[j];t++)
57             {
58                 p[i].id=j;
59                 p[i].l=num;
60                 p[i].r=num+cnt[j]-1;
61                 i++;
62             }
63             num+=cnt[j];
64         }
65         rmq_init();
66         while(q--)
67         {
68             int l,r,ans;
69             scanf("%d%d",&l,&r);
70             if(p[l].id==p[r].id)
71                 ans=r-l+1;
72             else
73             {
74                 ans=rmq(p[l].id+1,p[r].id-1);
75                 ans=max(ans,p[l].r-l+1);
76                 ans=max(ans,r-p[r].l+1);
77             }
78             printf("%d
",ans);
79         }
80     }
81     return 0;
82 }
原文地址:https://www.cnblogs.com/homura/p/5021956.html