The complexity of rmq preprocessing is O (nlgn), and the complexity of inquiry is O (1)
// Discretization: records the starting position, ending position, and frequency of each value in the sequence from small to large # define n 100010int max (int A, int B) {return A> B? A: B;} int DP [18] [N]; // DP [I] [J] indicates that the length starts from J and ranges from 2 ^ I to the maximum (minimum) int f [N]; // records the frequency int s [N], t [N], and t [N] using the same number as an interval. // discretization records the subscript int N in the left and right intervals, m; void init_rmq () {int I, j; for (I = 1; I <= N; I ++) {DP [0] [I] = f [I];} int T = floor (log (double) N)/log (2.0 )); // round down (I = 1; I <= T; I ++) {for (j = 1; j + (1 <(I-1 )) <= N; j ++) {DP [I] [J] = max (DP [I-1] [J], DP [I-1] [J + (1 <(I-1)]) ;}} int find (int x) {int L = 1, R = N, mid; while (L <= r) {mid = (L + r)> 1; if (s [Mid]> X) {r = mid-1 ;} else if (T [Mid] <X) {L = Mid + 1;} else return mid;} int rmq (int I, Int J) {int A = find (I), B = find (j); int AA = a + 1, BB = B-1; int ans = 1; if (AA <= bb) {int K = floor (log (bb-AA + 1) * 1.0)/log (2.0); ans = max (DP [k] [AA], DP [k] [BB-(1 <k) + 1]);} if (a <B) {ans = max (ANS, T [a]-I + 1); ans = max (ANS, J-s [B] + 1);} else ans = max (ANS, j-I + 1); Return ans;} int main () {While (scanf ("% d", & N) {scanf ("% d ", & M); int A; int I, j, k = 0; scanf ("% d", & A); int last = A; int head = 1; int CNT = 1; for (I = 2; I <= N; I ++) {scanf ("% d", & A); if (a = last) {CNT ++;} else {f [++ K] = CNT; s [k] = head; t [k] = I-1; head = I; CNT = 1; last = A ;}} f [++ K] = CNT; s [k] = head; t [k] = N; n = K; init_rmq (); while (M --) {scanf ("% d", & I, & J); printf ("% d \ n", rmq (I, j) ;}} return 0;}/* 10 3-1-1 1 1 1 3 3 10 102 31 105 100 */