zoukankan      html  css  js  c++  java
  • BZOJ 1636: [Usaco2007 Jan]Balanced Lineup

    题目

    1636: [Usaco2007 Jan]Balanced Lineup

    Time Limit: 5 Sec  Memory Limit: 64 MB

    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

    Max difference in height

    Sample Input

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

    * 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 Output


    6
    3
    0

    HINT

     

    Source

    题解

    静态线段树水题Orz,区间查找最大值和最小值做差就可以了Orz

    代码

     1 /*Author:WNJXYK*/
     2 #include<cstdio>
     3 using namespace std;
     4 
     5 struct Btree{
     6     int left,right;
     7     int num;
     8     int max,min;
     9 }; 
    10 const int Maxn=50000; 
    11 Btree tree[Maxn*4+10];
    12 
    13 int n,q;
    14 int num[Maxn+10];
    15 
    16 inline int remin(int a,int b){
    17     if (a<b) return a;
    18     return b;
    19 }
    20 
    21 inline int remax(int a,int b){
    22     if (a>b) return a;
    23     return b;
    24 }
    25 
    26 void build(int x,int left,int right){
    27     tree[x].left=left;
    28     tree[x].right=right;
    29     if (left==right){
    30         tree[x].max=tree[x].min=tree[x].num=num[left];
    31     }else{
    32         int mid=(left+right)/2;
    33         build(x*2,left,mid);
    34         build(x*2+1,mid+1,right);
    35         tree[x].max=remax(tree[x*2].max,tree[x*2+1].max);
    36         tree[x].min=remin(tree[x*2].min,tree[x*2+1].min);
    37     }
    38 }
    39 
    40 int queryMax(int x,int left,int right){
    41     if (left<=tree[x].left && tree[x].right<=right){
    42         return tree[x].max;
    43     }else{
    44         int res=0;
    45         int mid=(tree[x].left+tree[x].right)/2;
    46         if (left<=mid) res=remax(res,queryMax(x*2,left,right));
    47         if (mid+1<=right) res=remax(res,queryMax(x*2+1,left,right));
    48         return res;
    49     }
    50 }
    51 
    52 int queryMin(int x,int left,int right){
    53     if (left<=tree[x].left && tree[x].right<=right){
    54         return tree[x].min;
    55     }else{
    56         int res=2147483647;
    57         int mid=(tree[x].left+tree[x].right)/2;
    58         if (left<=mid) res=remin(res,queryMin(x*2,left,right));
    59         if (mid+1<=right) res=remin(res,queryMin(x*2+1,left,right));
    60         return res;
    61     }
    62 }
    63 
    64 int main(){
    65     scanf("%d%d",&n,&q);
    66     for (int i=1;i<=n;i++) scanf("%d",&num[i]);
    67     build(1,1,n);
    68     for (int i=1;i<=q;i++){
    69         int left,right;
    70         scanf("%d%d",&left,&right);
    71         printf("%d
    ",queryMax(1,left,right)-queryMin(1,left,right));    
    72     }
    73     return 0;
    74 }
    View Code
  • 相关阅读:
    盛最多水容器
    罗马数字和整数互相转化
    v-if和v-for
    扫描二维码登录
    使用Promise实现红绿灯交替重复亮
    利用promise实现间隔1s打印1,2,3
    原生js拖拽
    react项目引入使用element-react报错
    php实现类似淘宝最近浏览商品的功能模型代码
    教你如何把php项目打包成EXE文件发布
  • 原文地址:https://www.cnblogs.com/WNJXYK/p/4162344.html
Copyright © 2011-2022 走看看