zoukankan      html  css  js  c++  java
  • poj3264_Balanced Lineup

    Balanced Lineup
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 42349   Accepted: 19917
    Case Time Limit: 2000MS

    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
    线段树维护区间最大值、最小值,然后相减。AC代码:
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstdio>
     4 #include <map>
     5 #include <string>
     6 #include <string.h>
     7 #include <queue>
     8 #include <vector>
     9 #include <set>
    10 #include <cmath>
    11 #define inf 0x7fffffff
    12 using namespace std;
    13 const int maxn=50000;
    14 struct b{
    15 int imax,imin;
    16 }tree[maxn*4+100];
    17 int n,q,x,y,a[maxn+100];
    18 void build(int p,int l,int r){
    19      if(l==r) {tree[p].imax=a[l],tree[p].imin=a[l];return ;}
    20      int mid=(l+r)/2;
    21      build(p*2,l,mid);
    22      build(p*2+1,mid+1,r);
    23      tree[p].imax=max(tree[p*2].imax,tree[p*2+1].imax);
    24      tree[p].imin=min(tree[p*2].imin,tree[p*2+1].imin);
    25 }
    26 int findmin(int p,int l,int r,int x,int y){
    27      if(x<=l&&r<=y) return tree[p].imin;
    28      int mid=(l+r)/2;
    29      if(x>mid) return findmin(p*2+1,mid+1,r,x,y);
    30      if(y<=mid) return findmin(p*2,l,mid,x,y);
    31      return min(findmin(p*2+1,mid+1,r,x,y),findmin(p*2,l,mid,x,y));
    32 }
    33 int findmax(int p,int l,int r,int x,int y){
    34      if(x<=l&&r<=y) return tree[p].imax;
    35      int mid=(l+r)/2;
    36      if(x>mid) return findmax(p*2+1,mid+1,r,x,y);
    37      if(y<=mid) return findmax(p*2,l,mid,x,y);
    38      return max(findmax(p*2+1,mid+1,r,x,y),findmax(p*2,l,mid,x,y));
    39 }
    40 int main()
    41 {
    42     scanf("%d%d",&n,&q);
    43     for(int i=1;i<=n;i++)
    44         scanf("%d",&a[i]);
    45         build(1,1,n);
    46     for(int i=1;i<=q;i++){
    47         scanf("%d%d",&x,&y);
    48         printf("%d
    ",findmax(1,1,n,x,y)-findmin(1,1,n,x,y));
    49     }
    50     return 0;
    51 }
     
  • 相关阅读:
    Django框架---- 自定义分页组件
    Django框架----Form组件补充
    ARM体系的异常中断
    ARM处理机模式--内部寄存器
    产品概述
    简单应用程序的设计 -重复前缀
    简单应用程序的设计字符串处理
    基本输入输出系统BIOS---显示输出
    基本输入输出系统BIOS---键盘输入
    中断
  • 原文地址:https://www.cnblogs.com/GeniusYang/p/5225783.html
Copyright © 2011-2022 走看看