Ultraviolet A 11235 Frequent values (RMQ)

Source: Internet
Author: User

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<
    
     

 

 

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.