[POJ]

来源:互联网 发布:天数倒计时软件 编辑:程序博客网 时间:2024/06/13 14:57

Description

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

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

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
Source

Ulm Local 2007

线段树

线段树的精髓在于维护的过程

#include<cstdio>#include<algorithm>#define INF 0x3f3f3f3f#define MAX_N 100005using namespace std;int res[MAX_N];struct node{ int c_l,c_r,val,cnt;};node dat[MAX_N*3];node make_node(int a,int b,int c,int d){    node t;t.c_l=a,t.c_r=b,t.val=c,t.cnt=d;    return t;}void build(int k,int l,int r){    if(l==r) {        dat[k]=make_node(l,l,res[l],1);        return ;    }    int mid=(r+l)>>1;    int t=k<<1;    build(t,l,mid);    build(t+1,mid+1,r);    if(res[dat[t].c_l]==res[dat[t+1].c_l]) dat[k].c_l=dat[t+1].c_l;    else dat[k].c_l=dat[t].c_l;    if(res[dat[t].c_r]==res[dat[t+1].c_r]) dat[k].c_r=dat[t].c_r;    else dat[k].c_r=dat[t+1].c_r;    int u=dat[t].cnt>dat[t+1].cnt?t:t+1;    if(res[dat[t].c_r]==res[dat[t+1].c_l]&&dat[t+1].c_l-dat[t].c_r+1>dat[u].cnt){        dat[k].cnt=dat[t+1].c_l-dat[t].c_r+1;        dat[k].val=res[dat[t].c_r];    }else{        dat[k].cnt=dat[u].cnt;        dat[k].val=dat[u].val;    }}node query(int a,int b,int k,int l,int r){    if(a>r||b<l) return make_node(0,0,0,0);    if(a<=l&&b>=r){        return dat[k];    }    else{        node t1=query(a,b,k*2,l,(l+r)/2);        node t2=query(a,b,k*2+1,(l+r)/2+1,r);        if(t1.cnt==0) return t2;        if(t2.cnt==0) return t1;        node t;        node *p=t1.cnt>t2.cnt?&t1:&t2;        if(res[t1.c_l]==res[t2.c_l]) t.c_l=t2.c_l;        else t.c_l=t1.c_l;        if(res[t1.c_r]==res[t2.c_r]) t.c_r=t1.c_r;        else t.c_r=t2.c_r;        if(res[t1.c_r]==res[t2.c_l]&&t2.c_l-t1.c_r+1>(p->cnt)){            t.cnt=t2.c_l-t1.c_r+1;            t.val=res[t1.c_r];        }else{            t.cnt=p->cnt;            t.val=p->val;        }        return t;    }}int main(){    int n,m;    while(scanf("%d",&n)!=EOF){        if(!n) break;        scanf("%d",&m);        for(int i=1;i<=n;i++)            scanf("%d",&res[i]);        build(1,1,n);        while(m--){            int l,r;            scanf("%d%d",&l,&r);            node t=query(l,r,1,1,n);            printf("%d\n",t.cnt);        }    }    return 0;}
0 0