• <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免费人成_看片中文| 性做久久久久久久久老女人| 狠狠色丁香久久婷婷综合| 久久99精品国产麻豆| 99久久国产综合精品五月天喷水| 欧美久久久久久精选9999| 国产精品久久久久a影院| 久久99国产综合精品女同| 国产综合成人久久大片91| 狠狠色综合网站久久久久久久高清 | 精品国产日韩久久亚洲| 久久婷婷五月综合色高清 | 99久久精品午夜一区二区| 久久久久久国产a免费观看不卡| 日韩人妻无码一区二区三区久久 | 久久国产亚洲高清观看| 久久久久亚洲AV无码去区首| 久久精品亚洲中文字幕无码麻豆| 中文字幕无码久久人妻| 久久国产成人| 99热成人精品免费久久| 久久国产劲爆AV内射—百度| 久久黄色视频| 久久er国产精品免费观看8| 国内精品久久人妻互换| 99久久99久久精品国产片果冻 | 99久久无色码中文字幕| 一本久久知道综合久久| 久久亚洲精品国产精品婷婷| 久久久久亚洲精品无码网址 | 国产亚洲精久久久久久无码77777 国产亚洲精品久久久久秋霞 | 久久国产影院| 久久高潮一级毛片免费| 九九久久精品国产| 热久久国产精品| 99国产精品久久| 国产69精品久久久久9999| 91精品婷婷国产综合久久| 久久免费精品一区二区|