• H


    Problem F: Frequent values

    You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consisting of indices i and j (1 ≤ i ≤ j ≤ n). For each query, determine the most frequent value among the integers ai , ... , aj.

    Input Specification

    The input consists of several test cases. Each test case starts with a line containing two integers n and q (1 ≤ n, q ≤ 100000). The next line contains n integers a1 , ... , an (-100000 ≤ ai ≤ 100000, for each i ∈ {1, ..., n}) separated by spaces. You can assume that for each i ∈ {1, ..., n-1}: ai ≤ ai+1. The following q lines contain one query each, consisting of two integers i and j (1 ≤ i ≤ j ≤ n), which indicate the boundary indices for the query.

    The last test case is followed by a line containing a single 0.

    Output Specification

    For each query, print one line with one integer: The number of occurrences of the most frequent value within the given range.

    Sample Input

    10 3
    -1 -1 1 1 1 1 3 10 10 10
    2 3
    1 10
    5 10
    0
    

    Sample Output

    1
    4
    3

    RMQ问题
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <string>
     7 #include <vector>
     8 #include <stack>
     9 #include <queue>
    10 #include <set>
    11 #include <map>
    12 #include <list>
    13 #include <iomanip>
    14 #include <cstdlib>
    15 #include <sstream>
    16 using namespace std;
    17 typedef long long LL;
    18 const int INF=0x5fffffff;
    19 const double EXP=1e-6;
    20 const int MS=100005;
    21 
    22 int dp[MS][20];
    23 int a[MS],cnt[MS];
    24 int num[MS],l[MS],r[MS];
    25 int n,q;
    26 
    27 void RMQ_init()
    28 {
    29     for(int i=0;i<n;i++)
    30         dp[i][0]=cnt[i];
    31     for(int j=1;(1<<j)<=n;j++)
    32     {
    33         for(int i=0;i+(1<<j)-1<n;i++)
    34             dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
    35     }
    36 }
    37 
    38 int RMQ(int l,int r)
    39 {
    40     int k=0;
    41     while(1<<(k+1)<=r-l+1)
    42         k++;
    43     return max(dp[l][k],dp[r-(1<<k)+1][k]);
    44 }
    45 
    46 
    47 
    48 int main()
    49 {
    50     while(scanf("%d%d",&n,&q)==2&&n)
    51     {
    52         for(int i=0;i<n;i++)
    53             scanf("%d",&a[i]);
    54         a[n]=a[n-1]+1;
    55         int start=0;
    56         int id=0;
    57         for(int i=0;i<=n;i++)
    58         {
    59             if(i>0&&a[i]>a[i-1])
    60             {
    61                 for(int j=start;j<i;j++)
    62                     r[j]=i-1;
    63                 cnt[id]=i-start;
    64                 id++;
    65                 start=i;
    66             }
    67             l[i]=start;
    68             num[i]=id;
    69         }
    70         n=id;
    71         RMQ_init();
    72         int x,y;
    73         while(q--)
    74         {
    75             scanf("%d%d",&x,&y);
    76             x--;
    77             y--;
    78             int ans=0;
    79             if(num[x]==num[y])
    80             {
    81                 printf("%d
    ",y-x+1);
    82                 continue;
    83             }
    84             ans=max(r[x]-x+1,y-l[y]+1);
    85             if(num[x]+1<num[y])
    86                 ans=max(ans,RMQ(num[x]+1,num[y]-1));
    87             printf("%d
    ",ans);
    88         }
    89 
    90     }
    91     return 0;
    92 }
    
    
    

  • 相关阅读:
    child-selector解释
    a:link a:visited a:hover a:active四种伪类选择器的区别
    Java API —— BigDecimal类
    Java API —— BigInteger类
    Java API —— Random类
    Java API —— Math类
    Java API —— Pattern类
    Shuffle和排序
    剖析MapReduce 作业运行机制
    MapReduce编程系列 — 6:多表关联
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/4345394.html
Copyright © 2020-2023  润新知