• <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>
            隨筆 - 97, 文章 - 22, 評論 - 81, 引用 - 0
            數據加載中……

            PKU 1990 MooFest

            題目鏈接:http://poj.org/problem?id=1990

            /*
            題意:
                約翰農夫有N(N <= 20000)頭牛,每頭牛有一個權值Vi,他將它們排成一排,
            牛i和牛j的閾值是  兩者的距離差*max(Vi, Vj),現在給出每頭牛的權值和它的
            位置,求所有兩頭牛之間的閾值之和。

            題解:
                樹狀數組

            思路:
                我們準備兩個樹狀數組,以每頭牛的位置作為樹狀數組的下標,其中一個用
            來表示當前位置的牛的位置的值,另一個則記錄當前位置牛的個數,然后對所有
            牛以Vi為關鍵字進行遞增排序。
                接下來對每頭牛進行一次線掃,首先統計比當前這頭牛的位置小的和大的牛
            的數目和位置和,然后做差求出以當前牛的權值為最大值的閾值總和。之后將這
            頭牛的數量和位置插入到樹狀數組中進行更新。
            */


            #include 
            <iostream>
            #include 
            <algorithm>

            using namespace std;

            #define maxn 20010
            #define ll __int64

            struct point {
                
            int V;
                
            int pos;
            }
            pt[maxn];

            ll c[
            2][maxn];
            int n, Max;

            ll ABS(ll v) 
            {
                
            return v < 0 ? -v : v;
            }


            int cmp(point a, point b) {
                
            return a.V < b.V;
            }


            int lowbit(int x) {
                
            return x & (-x);
            }


            void add(int idx, int pos, int v) {
                
            while(pos <= Max) {
                    c[idx][pos] 
            += v;
                    pos 
            += lowbit(pos);
                }

            }


            ll sum(
            int idx, int pos) {
                ll s 
            = 0;
                
            while(pos > 0{
                    s 
            += c[idx][pos];
                    pos 
            -= lowbit(pos);
                }

                
            return s;
            }



            int main() {
                
            int i;
                
            while(scanf("%d"&n) != EOF) {
                    Max 
            = 0;
                    
            for(i = 0; i < n; i++{
                        scanf(
            "%d %d"&pt[i].V, &pt[i].pos);
                        
            if(pt[i].pos > Max)
                            Max 
            = pt[i].pos;
                    }


                    
            for(i = 1; i <= Max; i++)
                        c[
            0][i] = c[1][i] = 0;
                    sort(pt, pt 
            + n, cmp);

                    ll ans 
            = 0;
                    
            for(i = 0; i < n; i++{
                        ans 
            += ABS((sum(0, Max) - sum(0, pt[i].pos)
                             
            - (sum(1, Max) - sum(1, pt[i].pos)) * pt[i].pos)) * pt[i].V;

                        ans 
            += ABS((sum(0, pt[i].pos)
                             
            - sum(1, pt[i].pos) * pt[i].pos)) * pt[i].V;

                        add(
            0, pt[i].pos, pt[i].pos);
                        add(
            1, pt[i].pos, 1);
                    }

                    printf(
            "%I64d\n", ans);
                }

                
            return 0;
            }

            posted on 2011-04-09 22:06 英雄哪里出來 閱讀(1325) 評論(0)  編輯 收藏 引用 所屬分類: 樹狀數組

            亚洲人成无码www久久久 | 伊人久久亚洲综合影院| 国产呻吟久久久久久久92| 亚洲美日韩Av中文字幕无码久久久妻妇 | 91久久婷婷国产综合精品青草| 久久99毛片免费观看不卡 | 国产精品中文久久久久久久| 国产成人久久精品一区二区三区 | 久久婷婷国产综合精品| 亚洲午夜久久久精品影院 | 夜夜亚洲天天久久| 亚洲中文字幕无码久久综合网| 亚洲国产二区三区久久| 亚洲级αV无码毛片久久精品| 亚洲嫩草影院久久精品| 人妻精品久久无码专区精东影业 | 91久久精品无码一区二区毛片| 亚洲国产成人精品女人久久久| 久久人人妻人人爽人人爽| 亚洲精品国产自在久久| 97r久久精品国产99国产精| 国产精品永久久久久久久久久 | 久久国产免费直播| 日本亚洲色大成网站WWW久久| 久久精品国产亚洲av影院| 久久亚洲熟女cc98cm| 人妻无码精品久久亚瑟影视| 成人精品一区二区久久久| 精品久久久久久久久午夜福利| 精品国产青草久久久久福利| 无码人妻少妇久久中文字幕| 久久国产精品偷99| 国产成人精品久久亚洲| 国产69精品久久久久99| 99久久国产综合精品成人影院| 久久国产色AV免费看| 久久久久无码精品国产| 国内精品久久久久久99| 国产精品一久久香蕉国产线看观看| 色综合久久久久综合体桃花网| 国产精品中文久久久久久久 |