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

            poj 2299 Ultra-QuickSort 樹狀數(shù)組

            求逆序?qū)?shù),樹狀數(shù)組

            數(shù)據(jù)范圍較大,要離散化。

            給每一個數(shù)據(jù)一個id, 第i個數(shù)據(jù)的id為i。 然后從小到大排序,對于每個id做 ans += read(n) - read(array[i].id),read(n) - read(array[i].id)表示原來在當(dāng)前數(shù)的后面(其id大于當(dāng)前數(shù)的id),
            現(xiàn)在在當(dāng)前數(shù)前面的數(shù)個數(shù),也就是逆序?qū)?shù)。


            #include<iostream>
            #include
            <cstring>
            #include
            <cstdio>
            #include
            <algorithm>
            using namespace std;
            const int MAXVAL = 500005;

            int tree[MAXVAL] ;
            struct Type
            {
                
            int num, id;
            };

            int n;
            Type array[MAXVAL];

            void update(int idx, int inc)  //更新idx的頻率
            {
                
            while(idx <= n)
                {
                    tree[idx] 
            += inc;
                    idx 
            += (idx & - idx);
                }
            }

            int read(int idx)   //讀取1--idx的頻率和
            {
                
            int sum = 0;
                
            while(idx > 0)
                {
                    sum 
            += tree[idx];
                    idx 
            -= (idx & - idx);
                }
                
            return sum;
            }

            int readSingle(int idx) // 讀取某個位置的頻率, O(lg MAXVAL)
            {
                 
            int sum = tree[idx];
                 
            if(idx > 0)
                 {
                     
            int z = idx - ( idx & - idx);  
                     
                     idx 
            --;

                     
            while( idx != z)
                     {
                          sum 
            -= tree[idx];

                          idx 
            -= (idx & - idx);
                     }
                 }

                 
            return sum;
            }


            bool cmp(const  Type &a, const Type &b)
            {
                
            return a.num < b.num;
            }
            int main()
            {
                
            while (scanf("%d",&n)  && n != 0)    
                {
                    memset(array, 
            0sizeof (array));
                    memset(tree, 
            0sizeof tree);

                    
            // read the data
                    for(int i = 1; i <= n; i ++)
                    {
                        scanf(
            "%d",&array[i].num);
                        array[i].id 
            = i;
                    }
                
                    sort(array 
            + 1, array + 1 + n, cmp);

                    
            long long ans = 0;
                    
            for(int i = 1; i <= n; i ++)
                    {
                        
            //printf( "cal   %d \n",read(n) - read(array[i].id));
                        ans += read(n) - read(array[i].id);
                        update(  array[i].id, 
            1);
                    }
                        
                    cout 
            << ans << endl;
                }


                
            return 0;
            }

            posted on 2011-03-16 20:49 田兵 閱讀(432) 評論(2)  編輯 收藏 引用 所屬分類: 數(shù)據(jù)結(jié)構(gòu)

            評論

            # re: poj 2299 Ultra-QuickSort 樹狀數(shù)組 2011-04-12 09:25 銀志圓

            排序用sort不太妥當(dāng)吧 sort是不穩(wěn)定排序 如果給定的序列存在多個相同的元素會出現(xiàn)錯誤吧 盡管這個程序oj上能ac。
            大概oj上給定的數(shù)據(jù)是互不相同的吧   回復(fù)  更多評論   

            # re: poj 2299 Ultra-QuickSort 樹狀數(shù)組 2011-04-19 21:27 田兵

            有個id, id小的在前面  回復(fù)  更多評論   


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            <2010年8月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            留言簿(2)

            隨筆分類(65)

            隨筆檔案(65)

            文章檔案(2)

            ACM

            搜索

            積分與排名

            最新隨筆

            最新評論

            閱讀排行榜

            久久久久亚洲AV无码专区网站| 久久男人AV资源网站| 无码日韩人妻精品久久蜜桃 | 97精品伊人久久大香线蕉app | 午夜精品久久久久久中宇| 亚洲中文字幕无码久久精品1| 久久久久久久久久久精品尤物| 久久久久久人妻无码| 亚洲国产精品久久| 欧美午夜精品久久久久久浪潮| 99久久精品免费看国产一区二区三区| 国产成年无码久久久免费| AV狠狠色丁香婷婷综合久久| 久久伊人精品青青草原日本| 久久精品国产2020| 99久久99久久精品国产片果冻| 久久人人爽人人澡人人高潮AV| 婷婷久久久亚洲欧洲日产国码AV| 7国产欧美日韩综合天堂中文久久久久| 久久久久亚洲AV无码去区首| 人妻少妇久久中文字幕| 久久se精品一区精品二区国产| 久久久久久久97| 欧美日韩精品久久久久| 91久久精一区二区三区大全| 久久婷婷是五月综合色狠狠| 久久精品18| 好久久免费视频高清| 无码伊人66久久大杳蕉网站谷歌 | 久久青草国产手机看片福利盒子| 久久久久久久免费视频| 成人精品一区二区久久| 久久香蕉国产线看观看精品yw| 欧美性大战久久久久久| 久久精品夜色噜噜亚洲A∨| 久久99免费视频| 久久国产精品成人影院| 一本久道久久综合狠狠爱| 久久成人国产精品免费软件| 国产欧美久久久精品影院| 老男人久久青草av高清|