Frequent values
Time Limit: 2000 MS Memory Limit: 65536 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
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<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 #include<iostream> 5 using namespace std; 6 #define N 12220 7 int re,r[N],l[N]; 8 int mark[N]; 9 ///mark chucundeshicishushishuyudijigeshu tabashuhedijigeshuxiangduiying 10 ///left chucundeshicishushicongnalikaishi 11 ///rigjt chucundeshicishushicongnalijieshu 12 int num[N];///shijishuzuyijingxioashi,xianzai num baocundeshidejigeshuheyoujige 13 int maxsum[N][20]; 14 void RMQ(){///zheyangdeRMQdedabufenchucundedaxiaoshicuowude,zhiyoushaoshucaishizhengquede,womenyaozaisouxunshizhaodaozhengquede.zheyangwomenjiukeyiluanpaile 15 for(int i=1;i<=re;i++) maxsum[i][0]=num[i]; 16 for(int j=1;(1<<j)-1<=re;j++){ 17 for(int i=1;i+(1<<(j-1))-1<=re;i++) 18 maxsum[i][j]=max(maxsum[i][j-1],maxsum[i+(1<<(j-1))-1][j-1]); 19 } 20 } 21 int Max(int a,int b,int c){ 22 if(a<b) a=b; 23 if(a<c) a=c; 24 return a; 25 } 26 int main() 27 { 28 int n,q,i,j,k,L,R,val; 29 while(scanf("%d",&n)!=EOF&&n){ 30 re=1; 31 scanf("%d%d",&q,&k); 32 val=k; 33 num[1]=1; 34 mark[1]=1; 35 l[0]=1; 36 for(i=2;i<=n;i++){ 37 scanf("%d",&k); 38 if(k==val){ 39 num[re]++;//zhesjitongyangdeshudedijige 40 mark[i]=re;//renweidiyiduan 41 } 42 else{ 43 r[re]=i-1;///soudingshangyigeshudeyoubianjie 44 re++;///jinruxiayigeduan 45 mark[i]=re;///xiayiduanbianhao 46 l[re]=i;///xinyiduankaishidedifang 47 val=k;///biaojixiaygeshufangbianbijiao 48 num[re]=1;///xinshudediyigeshukaishi 49 } 50 } 51 RMQ(); 52 while(q--){ 53 scanf("%d%d",&L,&R); 54 if(mark[L]==mark[R]) printf("%d",R-L+1);//rugouzaitongyiqujian 55 else{ 56 int ans=0; 57 if(mark[L]+1<=mark[R]-1){//如果两段中存在段 58 //int k=(int)((log(mark[R]-1))/log(2.0)); 59 int k=(int)((log(mark[R]-mark[L]-1))/log(2.0)); 60 //ans=max(maxsum[mark[L]+1][k],maxsum[mark[R]-(1<<k)][k]); 61 ans=max(maxsum[mark[L]+1][k],maxsum[mark[R]-(1<<k)][k]); 62 } 63 ans=Max(r[mark[L]]-L+1,ans,R-l[mark[R]]+1); 64 //printf("%d-->%d-->%d ",r[mark[L]]-L+1,ans,R-l[mark[R]]+1); 65 printf("%d ",ans); 66 } 67 } 68 } 69 }
这里好多注释都是用拼音注释的,不过都很好理解。