• <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 3264 RMQ問題

            Description

            For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

            Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

            Input

            Line 1: Two space-separated integers, N and Q.
            Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
            Lines N+2..N+Q+1: Two integers A and B (1 ≤ ABN), representing the range of cows from A to B inclusive.

            Output

            Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

            Sample Input

            6 3
            1
            7
            3
            4
            2
            5
            1 5
            4 6
            2 2

            Sample Output

            6
            3
            0

            Source

                題目大意:有N頭牛,對于第i頭牛,它的高度是h[i];有Q個查詢,每次查詢給定一個區間[a,b],求第a頭牛到第b頭牛中最高的和最矮的相差多少。對于這種典型的RMQ問題,可以建立一棵線段樹,然后查詢。
            #include <iostream>

            #define max(a,b) (a>b?a:b)
            #define min(a,b) (a<b?a:b)
            const int MAXN = 50001;

            struct segment{
                
            int left,right;
                
            int high,low;
            }
            tree[MAXN*2];
            int t,s,height[MAXN];

            void create(int l,int r,int index){
                tree[index].left
            =l,tree[index].right=r;
                
            if(l==r){
                    tree[index].high
            =height[l];
                    tree[index].low
            =height[l];
                    
            return;
                }

                
            int mid=(l+r)>>1;
                create(l,mid,
            2*index);
                create(mid
            +1,r,2*index+1);
                tree[index].high
            =max(tree[2*index].high,tree[2*index+1].high);
                tree[index].low
            =min(tree[2*index].low,tree[2*index+1].low);
            }

            void update(int l,int r,int index){
                
            if(tree[index].left==&& tree[index].right==r){
                    
            if(tree[index].high>t) t=tree[index].high;
                    
            if(tree[index].low<s) s=tree[index].low;
                    
            return;
                }

                
            int mid=(tree[index].left+tree[index].right)>>1;
                
            if(r<=mid)
                    update(l,r,
            2*index);
                
            else if(l>mid)
                    update(l,r,
            2*index+1);
                
            else{
                    update(l,mid,
            2*index);
                    update(mid
            +1,r,2*index+1);
                }

            }

            int main(){
                
            int i,n,q,x,y;
                scanf(
            "%d %d",&n,&q);
                
            for(i=1;i<=n;i++) scanf("%d",&height[i]);
                create(
            1,n,1);
                
            for(i=1;i<=q;i++){
                    scanf(
            "%d %d",&x,&y);
                    t
            =0,s=10000000;
                    update(x,y,
            1);
                    printf(
            "%d\n",t-s);
                }

                
            return 0;
            }

            posted on 2009-05-14 16:05 極限定律 閱讀(371) 評論(0)  編輯 收藏 引用 所屬分類: ACM/ICPC

            <2009年5月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            導航

            統計

            常用鏈接

            留言簿(10)

            隨筆分類

            隨筆檔案

            友情鏈接

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产产无码乱码精品久久鸭| 亚洲精品乱码久久久久久| 久久久精品国产亚洲成人满18免费网站 | 亚洲欧美成人久久综合中文网| 午夜精品久久久久久影视riav| 婷婷久久香蕉五月综合加勒比| 久久国产精品久久精品国产| 久久国产精品无| 日本精品久久久久中文字幕8| 国内精品久久久久影院亚洲 | 国产精品欧美亚洲韩国日本久久| 久久人人爽人爽人人爽av| 久久久久久无码Av成人影院| 久久亚洲天堂| 国产精品丝袜久久久久久不卡 | 亚洲午夜无码久久久久小说| 亚洲一区中文字幕久久| 久久综合国产乱子伦精品免费| 久久www免费人成看国产片| 国产精品久久久久久影院 | 久久精品男人影院| 久久婷婷五月综合97色一本一本| 久久综合九色欧美综合狠狠| 伊人久久大香线焦综合四虎| 久久99精品国产99久久| 久久久精品人妻一区二区三区四| 亚洲国产成人精品91久久久 | 97久久天天综合色天天综合色hd| 精品综合久久久久久97| 久久笫一福利免费导航| 久久免费视频一区| 人人狠狠综合久久亚洲高清| 久久综合九色欧美综合狠狠 | 亚洲va久久久噜噜噜久久狠狠 | 久久精品国产亚洲av高清漫画| 亚洲午夜无码AV毛片久久| 亚洲欧洲久久久精品| 久久无码国产专区精品| 国产69精品久久久久久人妻精品| 精品久久久久久中文字幕大豆网 | 久久婷婷是五月综合色狠狠|