最近在研究線段樹這個數據結構,發現這東西還挺耐玩的,它沒有固定的模式,具體的構建方法要依據不同題目的具體要求而定,雖然如此,不過大致的思路還是差不多,充其量不過改改節點里面的域罷了。
這題我看了半天,因為看到有區間了,而且數據量又很大,顯然要用log L的算法,于是只能是線段樹,可問題在于怎么維護這顆線段樹?
由于給出的序列一定是非遞減的,所以如果兩個數字相同的話,它們中間的數字肯定相同。
具體的算法是這樣的:
首先對給出的序列進行離散化統計,將相同的數字壓縮成一個節點,然后統計出這個壓縮后的節點在原序列中起點和終點的位置,以及出現的次數等。當然也要記錄原數字在離散化后的序列中的位置。
之后就是查詢,比方說[a,b]
1.如果a,b屬于同一個組,那么區間長度就是我們想要的答案 b-a+1;
2.如果a,b組號相差1,說明該區間被中間截斷了,只要分別研究兩側的區間,取大值即可Max(c-a+1,b-c) ---其中c是中間點---
3.如果a,b組號相差大于1,先取出兩側進行研究,取大值,然后再用線段樹,算出中間區間的最大值,與剛才的那個數比較,取出最大值即可。
//This is the source code for POJ 3368
//Coded by abilitytao
//Time:2009年7月24日21:03:20
//PS:This is the first time to using the method of discretization and the Data Structure Segment Tree to solve Problem.
//Worth to Memorize.
//Many thanks to the people on the Internet to share their codes.
//And Special thanks to the Blog owner "英雄哪里出來".
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
using namespace std;
#define MAX 100010

struct point


{
int left;
int right;
int num;
int count;
}p[MAX];//這個數組用來存儲離散化之后點的信息


int myarray[MAX];
int n,q,T;
int hash[MAX];//第i個數在新的分組中的編號

struct Segnode


{

int num;//統計那個頻率最高的數字
int count;//統計頻率最高的數字出現的次數
Segnode *lchild;//指向左孩子
Segnode *rchild;//指向右孩子
}Segnodeset[MAX];//預先開辟很多節點以備使用,可以提升效率


Segnode *Newnode()


{
static int countnum=0;
Segnode *temp=&Segnodeset[countnum++];
temp->lchild=NULL;
temp->rchild=NULL;
return temp;
}


void init()//離散化過程


{

T=1;
p[T].left=1;
p[T].right=1;
p[T].count=1;
p[T].num=myarray[1];
hash[1]=1;
int i;
for(i=2;i<=n;i++)

{

if(myarray[i]==myarray[i-1])

{
p[T].count++;
p[T].right=i;
}
else

{
T++;
p[T].num=myarray[i];
p[T].count=1;
p[T].left=i;
p[T].right=i;
}
hash[i]=T;
}
}


Segnode *Build (int l,int r)//建立線段樹,建立的同時統計區間上的最高頻率


{
Segnode *root=Newnode();
if(l==r)

{
root->count=p[l].count;
root->num=p[l].num;
return root;
}

int mid=(l+r)>>1;
root->lchild=Build(l,mid);
root->rchild=Build(mid+1,r);
if(root->lchild->count > root->rchild->count)

{

root->num=root->lchild->num;
root->count=root->lchild->count;

}
else

{

root->num=root->rchild->num;
root->count=root->rchild->count;
}
return root;
}


int Query(Segnode *root,int a,int b,int l,int r)//查詢函數


{

if(a==l&&b==r)

{

return root->count;
}

int mid=(l+r)>>1;
if(b<=mid)

{

return Query(root->lchild,a,b,l,mid);
}
else if(mid+1<=a)

{

return Query (root->rchild,a,b,mid+1,r);

}
else

{

int x=Query(root->lchild,a,mid,l,mid);
int y=Query(root->rchild,mid+1,b,mid+1,r);
return x>y?x:y;
}
}


int solve(Segnode *root,int a,int b)//算法核心在這個函數


{

if(hash[a]==hash[b])

{

return b-a+1;
}//如果查詢的兩個數在同一組中,很顯然最大的頻數應該等于區間長度

else if(hash[a]+1==hash[b])

{

int l=p[hash[a]].right-a+1;
int r=b-p[hash[b]].left+1;
return l>r?l:r;
}//如果組號相差一,則中間有分段點,截斷之后取大的

else

{

int l=p[hash[a]].right-a+1;
int r=b-p[hash[b]].left+1;
int MAXNUM=l>r?l:r;

int ans=Query(root,hash[a]+1,hash[b]-1,1,T);
return MAXNUM>ans?MAXNUM:ans;

}

}

inline void put(int x)//用字符串輸出,用以節省OI時間 PS:不過貌似作用不大


{
if(x< 0)

{
putchar('-');
x = -x;
}

if(x == 0)
{
putchar('0');
return;
}
char s[20];
int bas = 0;
for(;x;x/=10)s[bas++] = x%10+'0';
for(;bas--;)putchar(s[bas]);
return;
}



int main()


{

int i;
int a,b;
Segnode *root;
while(scanf("%d",&n))

{
if(n==0)
break;
scanf("%d",&q);
for(i=1;i<=n;i++)

{
scanf("%d",&myarray[i]);
}
init();
root=Build(1,T);
while(q--)

{

scanf("%d%d",&a,&b);
put(solve(root,a,b));
putchar('\n');

}
}
return 0;
}

