UVa 11235 (RMQ) Frequent values

Source: Internet
Author: User

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&LT;&LT;J) <= N; J + +) -              for(inti =0; i + (1&LT;&LT;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&LT;&LT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.