Ultraviolet A 11235-Frequent values (RMQ)
Link to the question: Ultraviolet A 11235-Frequent values
Given a non-descending integer array, it is required to calculate some queries (I, j), answer ai, ai + 1 ,..., The maximum number of occurrences in aj.
Solution: Because the sequence is non-descending, the same numbers must be tied together, so we use the o (n) method to process the intervals of the same numbers in each segment. Then, for each query:
- Num [I] = num [j]: j? I + 1
- Numi = numj: max (RMQ (righti + 1, reftj? 1), max (righti? I + 1, j? Leftj + 1 ))
#include
#include
#include using namespace std;const int maxn = 1e5+5;int N, Q, num[maxn], rmq[maxn][20];int left[maxn], right[maxn];void RMQ_init () { memset(rmq, 0, sizeof(rmq)); for (int i = 1; i <= N; i++) rmq[i][0] = right[i] - left[i] + 1; for (int j = 1; (1<
= 1; i--) { if (num[i] == num[i+1]) right[i] = right[i+1]; else right[i] = i; } RMQ_init();}int RMQ (int L, int R) { if (L > R) return 0; int k = 0; while (1<<(k+1) <= R-L+1) k++; return max(rmq[L][k], rmq[R-(1<