zoukankan      html  css  js  c++  java
  • POJ3264


    基础
    Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
    Submit Status



    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 ≤ A ≤ B ≤ N), 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
    <span style="color:#3366ff;">/*********************************************************************
        author    :   Grant Yuan
        time      :   2014.7.26
        algorithm :   线段树
    
    **********************************************************************/
    #include <iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #define MAX 50003
    #define INF 999999999
    using namespace std;
    int n,m;
    struct node{
    int l;
    int r;
    int Min;
    int Max;
    };
    int m1,m2;
    node tree[4*MAX];
    int h[MAX];
    
    void build(int left,int right,int root)
    {
         tree[root].l=left;
         tree[root].r=right;
    
        if(left==right){
         tree[root].Max=h[left];
         tree[root].Min=h[left];
        return;
        }
        int mid=(left+right)>>1;
        build(left,mid,root*2);
        build(mid+1,right,root*2+1);
        tree[root].Max=max(tree[root*2].Max,tree[root*2+1].Max);
        tree[root].Min=min(tree[root*2].Min,tree[root*2+1].Min);
    }
    
    void findMax(int left,int right,int root)
    {
       if(left<=tree[root].l&&right>=tree[root].r)
       {m1=max(m1,tree[root].Max);
       return;}
        int mid=(tree[root].l+tree[root].r)>>1;
        if(mid>=right)
            findMax(left,right,root*2);
        else if(mid<left)
            findMax(left,right,root*2+1);
        else{
            findMax(left,right,2*root);
            findMax(left,right,2*root+1);
        }
    }
    
    void findMin(int left,int right,int root)
    {
        if(left<=tree[root].l&&right>=tree[root].r)
          {m2=min(m2,tree[root].Min);
          return;}
        int mid=(tree[root].l+tree[root].r)>>1;
        if(mid>=right)
            findMin(left,right,root*2);
        else if(mid<left)
            findMin(left,right,root*2+1);
        else{
           findMin(left,right,2*root);
           findMin(left,right,2*root+1);
    
        }
    }
    
    int main()
    {
      cin>>n>>m;int a,b;
      for(int i=1;i<=n;i++)
        scanf("%d",&h[i]);
    
        build(1,n,1);
    
        for(int i=0;i<m;i++)
            {scanf("%d%d",&a,&b); m1=0;m2=INF;
    
         findMax(a,b,1);
           findMin(a,b,1);
            printf("%d
    ",m1-m2);
            }
        return 0;
    }
    </span>


  • 相关阅读:
    8051单片机指令和寻址方式
    C/C++的关系
    go JSON 读写到文件
    Oracle 对未提交事务的查询
    win8 iis 安装
    Silverlight 项目 对话框
    VisualSVN错误 Cannot query proxy blanket解决办法
    silverlight浏览器自适应问题
    windows server2003 多用户登陆问题解决办法
    silverlight 缺少对象错误
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4254484.html
Copyright © 2011-2022 走看看