POJ 3368 Frequent values(線段樹+離散化)
最近在研究線段樹這個數(shù)據(jù)結(jié)構(gòu),發(fā)現(xiàn)這東西還挺耐玩的,它沒有固定的模式,具體的構(gòu)建方法要依據(jù)不同題目的具體要求而定,雖然如此,不過大致的思路還是差不多,充其量不過改改節(jié)點里面的域罷了。這題我看了半天,因為看到有區(qū)間了,而且數(shù)據(jù)量又很大,顯然要用log L的算法,于是只能是線段樹,可問題在于怎么維護這顆線段樹?
由于給出的序列一定是非遞減的,所以如果兩個數(shù)字相同的話,它們中間的數(shù)字肯定相同。
具體的算法是這樣的:
首先對給出的序列進行離散化統(tǒng)計,將相同的數(shù)字壓縮成一個節(jié)點,然后統(tǒng)計出這個壓縮后的節(jié)點在原序列中起點和終點的位置,以及出現(xiàn)的次數(shù)等。當然也要記錄原數(shù)字在離散化后的序列中的位置。
之后就是查詢,比方說[a,b]
1.如果a,b屬于同一個組,那么區(qū)間長度就是我們想要的答案 b-a+1;
2.如果a,b組號相差1,說明該區(qū)間被中間截斷了,只要分別研究兩側(cè)的區(qū)間,取大值即可Max(c-a+1,b-c) ---其中c是中間點---
3.如果a,b組號相差大于1,先取出兩側(cè)進行研究,取大值,然后再用線段樹,算出中間區(qū)間的最大值,與剛才的那個數(shù)比較,取出最大值即可。
//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];//這個數(shù)組用來存儲離散化之后點的信息

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

{
int num;//統(tǒng)計那個頻率最高的數(shù)字
int count;//統(tǒng)計頻率最高的數(shù)字出現(xiàn)的次數(shù)
Segnode *lchild;//指向左孩子
Segnode *rchild;//指向右孩子
}Segnodeset[MAX];//預(yù)先開辟很多節(jié)點以備使用,可以提升效率

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)//建立線段樹,建立的同時統(tǒng)計區(qū)間上的最高頻率

{
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)//查詢函數(shù)

{
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)//算法核心在這個函數(shù)

{
if(hash[a]==hash[b])
{
return b-a+1;
}//如果查詢的兩個數(shù)在同一組中,很顯然最大的頻數(shù)應(yīng)該等于區(qū)間長度
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)//用字符串輸出,用以節(jié)省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;
}

posted on 2009-07-24 21:31 abilitytao 閱讀(1740) 評論(1) 編輯 收藏 引用

