Frequent valuesdescription
You are given a sequence of n integers a1, A2, ..., an in non-decreasing order. In addition to, 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 is starts with a line containing, 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 this for each i∈{1, ..., n-1}: Ai≤ai+1. The following Q lines contain one query each, consisting of the integers i and J (1≤i ≤j≤n), which indicate the boundary indices for the
Query.
The last test case was followed by a line containing a single 0. (multiple sets of data)
Output
For each query, print one line with one integer:the number of occurrences of the frequent value within the given ran Ge.
Sample Input
10 3-1-1 1 1 1 1 3 10 10 102 31 105 100
Sample Output
143
Problem Solving Ideas:
You can find the number of the same number (can be discrete), but if the interval is divided into three sections, the left section, the right section, the middle of the line can be used to use a segment tree (no update, RMQ to Simple) or RMQ calculate the maximum value.
Specific code:
1#include <iostream>2#include <cstdio>3#include <cmath>4 #defineN 1000055 using namespacestd;6 intA[n],lo[n],pos[n];7 intn,m,x,y;8 structII9 {Ten intSta,end; One intnum; A }aa[n]; - intf[n][ -]; - intMain () the { -Freopen ("poj3246.in","R", stdin); -Freopen ("Poj3246.out","W", stdout); - //RQM initialization, put log2 (i), first hit the table out + //f[][] is the number of consecutive intervals, not n - while((scanf ("%d%d", &n,&m)) = =2) + { A intI=1, q=2, p=0; atlo[i]=0; - while(i<=N) - { -i++; - if(i==q) - { inq*=2; -p++; to } +lo[i]=p; - } the intPre=N; * intk=0; $ for(intI=1; i<=n;i++)//the number of points of the starting point of the AA record continuous intervalPanax Notoginseng { -scanf"%d",&a[i]); the if(a[i]!=pre) + { Ak++; theAa[k].sta=i; +Aa[k].end=i;//record the history of K -Pre=A[i]; $ } $ ElseAa[k].end=i; -Pos[i]=k;//record the subscript corresponding to the K - } the for(intI=1; i<=k;i++) -aa[i].num=aa[i].end-aa[i].sta+1;Wuyi for(intI=1; i<=k;i++) thef[i][0]=Aa[i].num; - for(intj=1; j<=lo[n]+1; j + +) Wu for(intI=1; i<=k;i++) - if(I+ (1<<J)-1<=k) AboutF[i][j]=max (f[i][j-1],f[i+ (1<<j-1)][j-1]);//divided into equal two segments of length 1<<j-1 $ for(intI=1; i<=m;i++) - { -scanf"%d%d",&x,&y); - if(Pos[x]==pos[y]) cout<<y-x+1<<Endl; A Else + { the inta1=Aa[pos[x]].end; - intb1=Aa[pos[y]].sta; $ intn1=a1-x+1; the intN2=0; the intn3=y-b1+1; the intK1=pos[x],k2=Pos[y]; the intp=lo[k2-k1+1-2];//are the number of consecutive intervals - if(k2-k1-1==0) Cout<<max (N1,N3) <<Endl; in Else the { theN2=max (f[k1+1][p],f[k2-1-(1<<P) +1][p]); AboutCout<<max (Max (N1,N2), N3) <<Endl; the } the } the } + } - return 0; the}
Summarize:
1.RMQ use for maximum value
2,RMQ (line segment tree) application, is not completely used, first divided into three parts, the middle part with RMQ
3,RMQ:F[I][J] Initialize f[i][0]=a[i];
F[i][j]=max (f[i][j-1],f[i+ (1<
4. See the topic, is a number of data sets
POJ3368 (RMQ)