H-frequent values

Source: Internet
Author: User

Problem F:frequent Values

You are given a sequence of n integers a1, A2, ..., an in non-decreasing order. In addition to, 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 is starts with a line containing, 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 this for each i∈{1, ..., n-1}: Ai≤ai+1. The following Q lines contain one query each, consisting of the integers i and J (1≤ I≤j≤n), which indicate the boundary indices for the query.

The last test case was 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 frequent value within the given ran Ge.

Sample Input
10 3-1-1 1 1 1 1 3 10 10 102 31 105 100
Sample Output
143

RMQ problems
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>Ten#include <Set> One#include <map> A#include <list> -#include <iomanip> -#include <cstdlib> the#include <sstream> - using namespacestd; -typedefLong LongLL; - Const intinf=0x5fffffff; + Const Doubleexp=1e-6; - Const intms=100005; +  A intdp[ms][ -]; at intA[ms],cnt[ms]; - intNum[ms],l[ms],r[ms]; - intn,q; -  - voidRmq_init () - { in      for(intI=0; i<n;i++) -dp[i][0]=Cnt[i]; to      for(intj=1;(1&LT;&LT;J) <=n;j++) +     { -          for(intI=0; i+ (1&LT;&LT;J)-1<n;i++) theDp[i][j]=max (dp[i][j-1],dp[i+ (1<< (J-1))][j-1]); *     } $ }Panax Notoginseng  - intRMQ (intLintR) the { +     intk=0; A      while(1<< (k +1) <=r-l+1) thek++; +     returnMax (dp[l][k],dp[r-(1&LT;&LT;K) +1][k]); - } $  $  -  - intMain () the { -      while(SCANF ("%d%d", &n,&q) = =2&&N)Wuyi     { the          for(intI=0; i<n;i++) -scanf"%d",&a[i]); Wua[n]=a[n-1]+1; -         intstart=0; About         intId=0; $          for(intI=0; i<=n;i++) -         { -             if(i>0&&a[i]>a[i-1]) -             { A                  for(intj=start;j<i;j++) +r[j]=i-1; thecnt[id]=i-start; -id++; $start=i; the             } thel[i]=start; thenum[i]=ID; the         } -n=ID; in rmq_init (); the         intx, y; the          while(q--) About         { thescanf"%d%d",&x,&y); thex--; they--; +             intans=0; -             if(num[x]==Num[y]) the             {Bayiprintf"%d\n", y-x+1); the                 Continue; the             } -Ans=max (r[x]-x+1, y-l[y]+1); -             if(num[x]+1<Num[y]) theAns=max (ANS,RMQ (num[x]+1, num[y]-1)); theprintf"%d\n", ans); the         } the  -     } the     return 0; the}


H-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.