The range-max problem, O (nlogn) preprocessing, O (1) query.
The problem is to first run the sequence of these numbers, repeating the elements only record the number of repetitions.
For the queried [L, R] if it completely covers some contiguous repeating fragments, the query is the number of times that these elements repeat the most, that is, RMQ.
If [L, R] also covers a certain portion of the boundary, the maximum value is calculated separately.
Another special case is that the query interval is in a repeating fragment of an element, and the answer is directly r-l+1
1#include <cstdio>2#include <vector>3#include <algorithm>4 using namespacestd;5 6 Const intMAXN =100000+Ten;7 Const intMAXL = -;8 9 structRMQTen { One intD[MAXN][MAXL]; A voidInit (Constvector<int>&a) - { - intn =a.size (); the for(inti =0; I < n; i++) d[i][0] =A[i]; - for(intj =1; (1<<J) <= N; J + +) - for(inti =0; i + (1<<J)-1< n; i++) -D[I][J] = max (d[i][j-1], D[i + (1<< (J-1))][j-1]); + } - + intQueryintLintR) A { at intK =0; - while(1<< (k +1) <= r-l+1) k++; - returnMax (D[l][k], d[r-(1<<K) +1][k]); - } - }; - in intA[MAXN], NUM[MAXN], LEFT[MAXN], RIGHT[MAXN]; - RMQ RMQ; to + intMain () - { the //freopen ("In.txt", "R", stdin); * $ intN, Q;Panax Notoginseng while(SCANF ("%d%d", &n, &q) = =2) - { the for(inti =0; I < n; i++) scanf ("%d", &a[i]); +A[n] = a[n-1] +1; Avector<int>count; the for(inti =0; I <N;) + { - intj =i; $ while(A[j] = = A[i]) J + +; $Count.push_back (J-i); - for(intK = i; K < J; k++) - { theNum[k] = count.size ()-1; -LEFT[K] =i;WuyiRight[k] = J-1; the } -i =J; Wu } - About //for (int i = 0; i < count.size (); i++) printf ("%d\n", Count[i]); $ - RMQ. Init (count); - while(q--) - { A intL, R, ans; +scanf"%d%d", &l, &r); l--; r--; the if(Num[l] = = Num[r]) ans = r-l +1; - Else $ { theans = max (right[l]-l+1, r-left[r]+1); the if(Num[l] +1<Num[r]) the { theans = max (ans, rmq.query (num[l]+1, num[r]-1)); - } in } theprintf"%d\n", ans); the } About } the the return 0; the}code June
UVa 11235 (RMQ) Frequent values