• <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 樹狀數組

            求逆序對數,樹狀數組

            數據范圍較大,要離散化。

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


            #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)  編輯 收藏 引用 所屬分類: 數據結構

            評論

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

            排序用sort不太妥當吧 sort是不穩定排序 如果給定的序列存在多個相同的元素會出現錯誤吧 盡管這個程序oj上能ac。
            大概oj上給定的數據是互不相同的吧   回復  更多評論   

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

            有個id, id小的在前面  回復  更多評論   

            <2010年7月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            導航

            統計

            常用鏈接

            留言簿(2)

            隨筆分類(65)

            隨筆檔案(65)

            文章檔案(2)

            ACM

            搜索

            積分與排名

            最新隨筆

            最新評論

            閱讀排行榜

            香蕉久久影院| 精品乱码久久久久久夜夜嗨| 久久久久久精品免费免费自慰| 久久精品女人天堂AV麻| 久久综合日本熟妇| 久久婷婷五月综合色高清| 91精品国产高清91久久久久久| 久久99精品久久久久久| 久久强奷乱码老熟女网站| 欧美熟妇另类久久久久久不卡| 成人精品一区二区久久| 综合久久国产九一剧情麻豆| 久久精品国产影库免费看| 区久久AAA片69亚洲| 99久久精品毛片免费播放| 久久性精品| 9久久9久久精品| 久久精品一区二区三区AV| 青青青青久久精品国产h| 无遮挡粉嫩小泬久久久久久久| 99精品久久久久久久婷婷| 色偷偷偷久久伊人大杳蕉| 久久国产精品免费| 久久国产乱子伦精品免费强| 国产亚洲精品久久久久秋霞| 久久天天躁狠狠躁夜夜2020老熟妇 | 亚洲午夜精品久久久久久浪潮 | 久久久WWW成人| aaa级精品久久久国产片| 77777亚洲午夜久久多喷| 色综合久久久久综合99| 久久精品国产第一区二区| 久久婷婷综合中文字幕| 久久中文娱乐网| 久久免费精品视频| 91精品国产91久久| 夜夜亚洲天天久久| 久久精品国产亚洲AV不卡| 久久av免费天堂小草播放| 精品久久久久中文字| 久久93精品国产91久久综合|