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

            搜索

            積分與排名

            最新隨筆

            最新評論

            閱讀排行榜

            久久精品成人免费观看97| 久久91精品国产91久久小草| 国产免费久久精品99久久| 国产呻吟久久久久久久92| 午夜精品久久久久久影视riav | 久久人人超碰精品CAOPOREN| 久久久久女教师免费一区| 亚洲国产婷婷香蕉久久久久久| 亚洲精品白浆高清久久久久久| 国产综合久久久久| 久久99精品九九九久久婷婷| 日韩人妻无码精品久久免费一| 国产精品成人99久久久久91gav | 亚洲中文字幕无码久久精品1| 国产精品99久久精品| 精品国产乱码久久久久久浪潮 | 99久久精品国产免看国产一区| 国产高潮久久免费观看| 久久精品国产亚洲AV忘忧草18 | 日韩欧美亚洲综合久久| 99久久婷婷免费国产综合精品| 色狠狠久久综合网| 一级做a爰片久久毛片16| 久久久久久夜精品精品免费啦| 热综合一本伊人久久精品| 国产91久久精品一区二区| 久久久高清免费视频| 精品水蜜桃久久久久久久| 久久久久夜夜夜精品国产| 亚洲国产精品无码久久久蜜芽 | 亚洲国产二区三区久久| 久久经典免费视频| 香蕉久久影院| 无码乱码观看精品久久| 久久久久亚洲精品中文字幕 | 国产精品久久久久久福利69堂| 久久99国产精品久久99小说 | 久久久久久国产精品无码下载| 国产AⅤ精品一区二区三区久久| 久久99精品国产99久久| 久久精品国产精品亚洲精品|