Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 16543 | Accepted: 5985 |
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
Source
分析
1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #define MAX 100010 5 using namespace std; 6 int f[MAX][20]; 7 int num[MAX]; 8 int sum[MAX]; 9 int n,Q,ans,a,b; 10 int _max(int a,int b) 11 { 12 return a>b?a:b; 13 } 14 void ST() 15 { 16 for(int i=1;i<=n;i++) 17 f[i][0]=sum[i]; 18 int k=log((double)(n+1))/log(2.0); 19 for(int i=1;i<=k;i++) 20 for(int j=1;j+(1<<i)<=n+1;j++)//n+1 21 f[j][i]=_max(f[j][i-1],f[j+(1<<(i-1))][i-1]); 22 } 23 int RMQ_Query(int l,int r) 24 { 25 if(l>r)return 0; 26 int k=log((double)(r-l+1))/log(2.0); 27 return _max(f[l][k],f[r-(1<<k)+1][k]);//r-(1<<k)一定要+1 28 } 29 int main() 30 { 31 while(scanf("%d",&n)!=EOF,n) 32 { 33 scanf("%d",&Q); 34 for(int i=1;i<=n;i++) 35 { 36 scanf("%d",num+i); 37 if(i==1) 38 { 39 sum[i]=1; 40 continue; 41 } 42 if(num[i]==num[i-1]) 43 sum[i]=sum[i-1]+1; 44 else sum[i]=1; 45 } 46 ST(); 47 while(Q--) 48 { 49 scanf("%d%d",&a,&b); 50 if(a==b||num[a]==num[b]) 51 { 52 cout<<b-a+1<<endl; 53 continue; 54 } 55 int t=a; 56 while(num[t]==num[t-1]&&t<=b)//t 必须小于 b 57 t++; 58 int cnt=RMQ_Query(t,b); 59 ans=_max(cnt,t-a); 60 printf("%d ",ans); 61 } 62 } 63 return 0; 64 }