poj 3368 Frequent values(RMQまたは線分樹)
Frequent values
Time Limit: 2000 MS
メモリLimit: 65536 K
Total Submissions: 16959
Acceepted: 6125
Description
You are given a sequence of n integers a 1 , a 2 , ... , an in non-decrease 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
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ラインcontains n integersa 1 , ... , an (-100000≦ai ≦100000、for each i∈{1,…n}separated by spaces.You can asume that for each i∈{1,…,n-1}:ai ≦ai+1.The follwing qlines 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 followwed by line containing a single 0.
Output
For each query、print one line with one integer:The number of occurrences of the most frequent value within the given range.
Sample Input
RMQはしばしば、区間の一番値の問題を調べるために用いられ、任意の区間について、RMQアルゴリズムは、O(1)時間で最も高い値を検索するのに便利である.
最大値を例にとると、デリバリー方程式:f[i][j]=max(f[i][j-1],f[i+(1<>
f[i][j]:区間[i,i+2^j-1]を表し、j=0,1,2,区間内で結構です.
上式とは、大区間[i,i+2^j-1]区間=セル間[i,i+2^(j-1)-1]U[i+2^(j-1)、i+2^(j-1)+2^(j-1)-1]を表します.
等式の両側は明らかに等しい(同じ)ので、成立します.
この問題については、区間[l,r]を調べて、区間の左端点と区間の外側に等しい数字を単独で処理すればいいです.
RMQ解法:
Time Limit: 2000 MS
メモリLimit: 65536 K
Total Submissions: 16959
Acceepted: 6125
Description
You are given a sequence of n integers a 1 , a 2 , ... , an in non-decrease 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
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ラインcontains n integersa 1 , ... , an (-100000≦ai ≦100000、for each i∈{1,…n}separated by spaces.You can asume that for each i∈{1,…,n-1}:ai ≦ai+1.The follwing qlines 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 followwed by line containing a single 0.
Output
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 Output1
4
3
段の数字の区間の出現回数の最も多い数字の周波数を求めて、数列は単調で減らないです.数列を変換したら、RMQまたは線分樹で解くことができます.RMQはしばしば、区間の一番値の問題を調べるために用いられ、任意の区間について、RMQアルゴリズムは、O(1)時間で最も高い値を検索するのに便利である.
最大値を例にとると、デリバリー方程式:f[i][j]=max(f[i][j-1],f[i+(1<>
f[i][j]:区間[i,i+2^j-1]を表し、j=0,1,2,区間内で結構です.
上式とは、大区間[i,i+2^j-1]区間=セル間[i,i+2^(j-1)-1]U[i+2^(j-1)、i+2^(j-1)+2^(j-1)-1]を表します.
等式の両側は明らかに等しい(同じ)ので、成立します.
この問題については、区間[l,r]を調べて、区間の左端点と区間の外側に等しい数字を単独で処理すればいいです.
RMQ解法:
#include
#include
#include
#include
#include
using namespace std;
#define LL long long int
#define rep(i,a,n) for(int i=a;i=a;--i
#define N 100005
const int inf=1e9+10;
int a[N],num[N],rt[N];
int dp[N][20];
void rmq(int n)
{
int i,j;
rep(i,1,n+1) dp[i][0]=num[i];
for(j=1;j<20;++j){
for(i=1;i+(1<r)
return 0;
int k,t1,t2;
k=log(r-l+1.0)/log(2.0);
t1=dp[l][k];
t2=dp[r-(1<