Ultraviolet A 11235 Frequent values (RMQ)
Question: to give a non-descending integer array a, your task is to answer ai, ai + 1 for a series of queries (I, j... the number of occurrences of the most frequently occurring value in aj.
Thought: because the number of columns is non-descending, all equal numbers will be together. In this way, we can encode the entire array. Such as-, 4 can be encoded as (-), () represents (a, B) a in the array appears B consecutive times. The following table uses num [I] to indicate that the number of I in the original array is in the encoded num [I] segment. Left [I], right [I] indicates the left boundary and right boundary of segment I, and coun [I] indicates that segment I has the same number of conu [I. In this case, each query (L, R) requires calculation (right [L]-L + 1), (R-left [R] + 1) and RMQ (num [L] + 1, num [R]-1.
RMQ is the result of the coun array retrieval query.
Specifically, if L and R are in the same interval, the result is (R-L + 1)
AC code:
#include
#include
#includeusing namespace std;typedef long long LL;const int MAXN = 100005;const int MAXM = 100005;const double eps = 1e-12;int num[MAXN], coun[MAXN], Left[MAXN], Right[MAXN];int n, q, a, last, tot;int DP[MAXN][20];void init_RMQ(){ memset(DP,0,sizeof(DP)); for(int i=1;i<=tot;i++) DP[i][0] = coun[i]; for(int j=1;(1<
R) return 0; int k = 0; while((1<<(1+k)) <= R-L+1) k++; return max(DP[L][k], DP[R-(1<