• <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 田兵 閱讀(422) 評論(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年8月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            導航

            統計

            常用鏈接

            留言簿(2)

            隨筆分類(65)

            隨筆檔案(65)

            文章檔案(2)

            ACM

            搜索

            積分與排名

            最新隨筆

            最新評論

            閱讀排行榜

            久久精品中文字幕一区| 国产精品视频久久| 少妇人妻综合久久中文字幕| 大香伊人久久精品一区二区| 色婷婷综合久久久久中文一区二区 | 一本伊大人香蕉久久网手机| segui久久国产精品| 伊人 久久 精品| 久久青草国产精品一区| 亚洲午夜无码AV毛片久久| 久久精品国产亚洲av日韩| 色综合久久天天综线观看| 久久国产色AV免费看| 久久99精品国产99久久6| 亚洲午夜久久久久久久久电影网| 伊人久久大香线焦综合四虎| 99久久国产亚洲综合精品| 久久精品国产亚洲精品| 久久精品国产亚洲欧美| 人妻精品久久久久中文字幕一冢本| 久久99精品久久久久久水蜜桃 | 7777精品伊人久久久大香线蕉| 99久久久精品免费观看国产| 久久久久亚洲AV片无码下载蜜桃| 久久99精品久久久久久不卡| 久久亚洲国产中v天仙www| 久久亚洲春色中文字幕久久久 | 国产精品视频久久| 久久99热这里只有精品国产| 久久久久亚洲AV成人网| 国产亚州精品女人久久久久久 | 国产成人久久777777| 国内精品久久久久影院免费| 成人久久免费网站| 久久亚洲天堂| 久久综合亚洲鲁鲁五月天| 中文字幕久久亚洲一区| 亚洲国产精品嫩草影院久久| 伊人久久大香线蕉精品不卡| 久久久久久极精品久久久| 久久无码人妻精品一区二区三区|