• <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>

            為生存而奔跑

               :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
              271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks

            留言簿(5)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            積分與排名

            • 積分 - 328403
            • 排名 - 74

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            (所有數(shù)組下標(biāo)從1計(jì))計(jì)算出原數(shù)列中每個(gè)數(shù)出現(xiàn)次數(shù)的數(shù)列,并用RMQ預(yù)處理
            如 a[]={ -1 -1 1 1 1 1 3 10 10 10} 得到 sum[]={ 2 4 1 3 }
            用index數(shù)組記錄a[i]在sum中的下標(biāo)。index[]={1,1,2,2,2,2,3,4,4,4};
            first數(shù)組記錄每組數(shù)據(jù)中第一個(gè)出現(xiàn)的在a中的下標(biāo)
            first[]={1,3,7,8};

            對(duì)于每個(gè)詢問(wèn)的區(qū)間[from,to],首先計(jì)算出from to在sum中對(duì)應(yīng)的位置f,t
            1、如果f==t,則[from,to]區(qū)間中數(shù)據(jù)是一樣的
            2、如果f+1=t,則[from,to]區(qū)間只有兩種數(shù)據(jù)。
            3、如果f+1<t,則[from,to]區(qū)間有大于兩種數(shù)據(jù)
            #include<iostream>
            #include
            <vector>
            #include
            <string>
            #include
            <cmath>
            using namespace std;
            const int maxsize=100001;
            int sum[maxsize],st[maxsize][20],a[maxsize],index[maxsize],first[maxsize];
            void rmq_init(int len)
            {
                
            for(int i=1;i<=len;i++)
                    st[i][
            0]=sum[i];
                
            int m=floor(log((double)len)/log(2.0));
                
            for(int i=1;i<=m;i++)
                    
            for(int j=len;j>0;j--)
                    {
                        st[j][i]
            =st[j][i-1];
                        
            if(i+(1<<(i-1))<=len) st[j][i]=max(st[j][i],st[j+(1<<(i-1))][i-1]);
                    }
            }
            int query(int l,int r)
            {
                
            int m=floor(log((double)(r-l+1))/log(2.0));
                
            return max(st[l][m],st[r-(1<<m)+1][m]);
            }
            int solve(const int & from,const int & to)
            {
                
            int f=index[from],t=index[to];
                
            if(f==t) return to-from+1;
                
            else if(f+1==t) return max(first[t]-from,to-first[t]+1);
                
            else
                {
                    
            int res;
                    res
            =max(first[f+1]-from,to-first[t]+1);
                    res
            =max(res,query(f+1,t-1));
                    
            return res;
                }
            }

            int main()
            {
                
            int from,to;
                
            int n,q;
                
            int len;
                
            while(scanf("%d",&n)!=EOF)
                {
                    
            if(n==0break;
                    scanf(
            "%d",&q);
                    a[
            0]=INT_MAX;
                    
                    len
            =0;
                    memset(sum,
            0,sizeof(sum));
                    
            for(int i=1;i<=n;i++)
                    {
                        scanf(
            "%d",&a[i]);
                        
            if(a[i]==a[i-1])
                        {
                            index[i]
            =len;
                            sum[len]
            ++;
                        }
                        
            else
                        {
                            index[i]
            =++len;
                            sum[len]
            =1;
                            first[len]
            =i;
                        }
                    }
                    rmq_init(len);
                    
            while(q--)
                    {
                        scanf(
            "%d%d",&from,&to);
                        printf(
            "%d\n",solve(from,to));
                    }
                }
            }


            posted on 2009-10-25 21:58 baby-fly 閱讀(228) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Algorithm
            久久精品国产亚洲AV麻豆网站| 草草久久久无码国产专区| 亚洲国产欧美国产综合久久| 国产精品99久久精品| 大美女久久久久久j久久| 国产免费久久精品99re丫y| 色婷婷综合久久久中文字幕| 91精品观看91久久久久久 | 久久久中文字幕| 国内精品伊人久久久久妇| 久久亚洲欧美国产精品| 久久亚洲国产成人精品无码区| 久久发布国产伦子伦精品| 久久久噜噜噜久久| 欧美久久综合性欧美| 色88久久久久高潮综合影院| 欧美色综合久久久久久| 日本三级久久网| 久久国产亚洲高清观看| 99久久无色码中文字幕人妻| 久久久久国产精品麻豆AR影院 | 国产韩国精品一区二区三区久久| 一本色道久久综合| 久久久久久A亚洲欧洲AV冫| 久久99国产精一区二区三区| 欧美亚洲国产精品久久高清| 久久AⅤ人妻少妇嫩草影院| 秋霞久久国产精品电影院| 久久婷婷五月综合色高清| 亚洲精品无码久久一线| 色狠狠久久综合网| 久久婷婷午色综合夜啪| 亚洲精品99久久久久中文字幕| 91久久香蕉国产熟女线看| 亚洲成色999久久网站| 国产欧美一区二区久久| 国产精品久久久久aaaa| 97超级碰碰碰久久久久| 久久精品国产99久久无毒不卡| 精品久久久中文字幕人妻| 久久久久人妻一区二区三区|