zoukankan      html  css  js  c++  java
  • poj3264 最大值与最小值的差

    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.. NQ+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

    题意:就是取最大值与最小值的差。
    题解:就是线段树的基本功能里的。
     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cmath>
     5 #include<cstring>
     6 const int MAXN=50007;
     7 char s[20];
     8 int a[MAXN]={0},tree[MAXN*4]={0},fuck[MAXN*4]={0},t,num,x,n,y,q;
     9 using namespace std;
    10 void build(int l,int r,int p)
    11 {
    12     if (l==r) tree[p]=a[l];
    13     else
    14     {
    15         int mid=(l+r)>>1;
    16         build(l,mid,p*2);
    17         build(mid+1,r,p*2+1);
    18         tree[p]=max(tree[p*2],tree[p*2+1]);
    19     } 
    20 }
    21 void build_fuck(int l,int r,int p)
    22 {
    23     if (l==r) fuck[p]=a[l];
    24     else
    25     {
    26         int mid=(l+r)>>1;
    27         build_fuck(l,mid,p*2);
    28         build_fuck(mid+1,r,p*2+1);
    29         fuck[p]=min(fuck[p*2],fuck[p*2+1]);
    30     } 
    31 }
    32 int query(int l,int r,int p,int x,int y)
    33 {
    34     if (l==x&&r==y) return tree[p];
    35     else
    36     {
    37         int mid=(l+r)>>1;
    38         if (y<=mid) return query(l,mid,p*2,x,y);
    39         else if(x>=mid+1) return query(mid+1,r,p*2+1,x,y);
    40             else return max(query(l,mid,p*2,x,mid),query(mid+1,r,p*2+1,mid+1,y));
    41     }
    42 }
    43 int query_fuck(int l,int r,int p,int x,int y)
    44 {
    45     if (l==x&&r==y) return fuck[p];
    46     else
    47     {
    48         int mid=(l+r)>>1;
    49         if (y<=mid) return query_fuck(l,mid,p*2,x,y);
    50         else if(x>=mid+1) return query_fuck(mid+1,r,p*2+1,x,y);
    51         else return min(query_fuck(l,mid,p*2,x,mid),query_fuck(mid+1,r,p*2+1,mid+1,y));
    52     }
    53 }
    54 int main()
    55 {
    56     scanf("%d%d",&n,&q);
    57     for (int i=1;i<=n;i++)
    58         scanf("%d",&a[i]);
    59     build(1,n,1);
    60     build_fuck(1,n,1);
    61     while (q--)
    62     {
    63         scanf("%d%d",&x,&y);
    64         printf("%d
    ",query(1,n,1,x,y)-query_fuck(1,n,1,x,y));
    65     }
    66 }
     
  • 相关阅读:
    Ubuntu下启用IPV6
    时间倒计时(天数+时+分+秒)
    移动端触摸移动相互调换位置
    js移动端点击隐藏和出现指定div(包含少许特效)
    将页面内容保存为图片显示,长按保存至本地(html2canvas)
    移动端触摸上拉隐藏指定模块内容,有过度效果(同时页面iscroll滚动)
    移动端图片缩放(包括滑动查看)
    移动端图片缩放(不包括滑动查看)
    IOS返回页面不刷新的问题
    datePicker(可以同时选择日期和时间)
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/7535566.html
Copyright © 2011-2022 走看看