• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            The Fourth Dimension Space

            枯葉北風寒,忽然年以殘,念往昔,語默心酸。二十光陰無一物,韶光賤,寐難安; 不畏形影單,道途阻且慢,哪曲折,如渡飛湍。斬浪劈波酬壯志,同把酒,共言歡! -如夢令

            POJ 3368 Frequent values(線段樹+離散化)

            最近在研究線段樹這個數據結構,發現這東西還挺耐玩的,它沒有固定的模式,具體的構建方法要依據不同題目的具體要求而定,雖然如此,不過大致的思路還是差不多,充其量不過改改節點里面的域罷了。
            這題我看了半天,因為看到有區間了,而且數據量又很大,顯然要用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;
            }


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

            評論

            # re: POJ 3368 Frequent values(線段樹+離散化) 2009-07-25 15:49 凡客誠品

            路過看看!支持下!  回復  更多評論   

            久久国产热精品波多野结衣AV| 99久久精品免费| 热re99久久精品国99热| 国产成人精品久久| 波多野结衣中文字幕久久| 久久精品无码一区二区app| 狠狠色丁香久久婷婷综合蜜芽五月| 亚洲乱码中文字幕久久孕妇黑人| 亚洲精品乱码久久久久久蜜桃图片 | 久久无码人妻一区二区三区午夜 | 久久国产V一级毛多内射| 欧美久久久久久| 久久精品国产影库免费看 | 日日噜噜夜夜狠狠久久丁香五月 | 久久免费视频1| 精品国产一区二区三区久久久狼 | 久久婷婷五月综合97色直播| 欧美黑人又粗又大久久久| 国产午夜精品久久久久九九电影 | 伊人色综合久久天天人守人婷| 久久精品水蜜桃av综合天堂| 精品人妻伦九区久久AAA片69| 精品久久久久久亚洲精品| 性高湖久久久久久久久AAAAA| 伊人久久大香线蕉影院95| 久久久久亚洲av无码专区导航 | 久久久WWW成人免费精品| 久久天堂AV综合合色蜜桃网 | 久久成人小视频| 久久久久久久综合日本| 色综合久久最新中文字幕| 浪潮AV色综合久久天堂| 亚洲国产精品无码成人片久久| 麻豆av久久av盛宴av| 亚洲精品无码久久久久AV麻豆| 久久久无码精品午夜| 久久久久久国产精品美女| 久久久久久av无码免费看大片| 久久免费国产精品| 日本精品久久久久久久久免费| 国产精品久久久久蜜芽|