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 102 31 105 100
Sample Output
143
Source
分析
1 #include2 #include 3 #include 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< <=n+1;j++)//n+121 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<