zoukankan      html  css  js  c++  java
  • 18.1.5 POJ 3264 Balanced Lineup

    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

    Source

    USACO 2007 January Silver
     1 #include <iostream>
     2 #include <string.h>
     3 #include <algorithm>
     4 #include <stack>
     5 #include <string>
     6 #include <math.h>
     7 #include <queue>
     8 #include <stdio.h>
     9 #include <string.h>
    10 #include <set>
    11 #include <vector>
    12 #define maxn 50005
    13 #define inf 999999
    14 #define EPS 1e-10
    15 #define lowbit(x) (int)(x&(-x))
    16 using namespace std;
    17 
    18 struct node {
    19     int l, r;
    20     int max, min;
    21 }tree[maxn<<2];
    22 int n, m;
    23 int a[maxn];
    24 
    25 void build(int rt, int l, int r) {
    26     int mid = (l + r) / 2;
    27     tree[rt].l = l, tree[rt].r = r;
    28     if (l == r) {
    29         tree[rt].max = a[l];
    30         tree[rt].min = a[l];
    31         return;
    32     }
    33     build(2 * rt, l, mid), build(2 * rt + 1, mid + 1, r);
    34     int maxv = max(tree[2 * rt].max, tree[2 * rt + 1].max);
    35     int minv = min(tree[2 * rt].min, tree[2 * rt + 1].min);
    36     tree[rt].max = maxv, tree[rt].min = minv;
    37 }
    38 
    39 pair<int,int> query(int rt, int l, int r) {
    40     int ll = tree[rt].l, rr = tree[rt].r;
    41     int mid = (ll + rr) / 2;
    42     if (ll == l && rr == r) 
    43         return pair<int,int>(tree[rt].max,tree[rt].min);
    44     if (r <= mid)
    45         return query(rt * 2, l, r);
    46     else if (l > mid)
    47         return query(rt * 2 + 1, l, r);
    48     pair<int, int> ret1=query(rt*2,l,mid), ret2=query(rt*2+1,mid+1,r);
    49     int maxv = max(ret1.first, ret2.first);
    50     int minv = min(ret1.second, ret2.second);
    51     return pair<int, int>(maxv, minv);
    52 }
    53 
    54 void init() {
    55     scanf("%d%d", &n, &m);
    56     for (int i = 1; i <= n; i++)
    57         scanf("%d", &a[i]);
    58     build(1, 1, n);
    59     for (int i = 1; i <= m; i++) {
    60         int x, y;
    61         scanf("%d%d", &x, &y);
    62         pair<int, int>ans = query(1, x, y);
    63         printf("%d
    ", ans.first - ans.second);
    64     }
    65 }
    66 
    67 int main() {
    68     init();
    69     return 0;
    70 }
    View Code
    注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
  • 相关阅读:
    Notepad++ 配置python快捷键运行方法
    Python 安装setuptools方法
    Python 安装selenium方法
    Sublime 的汉化以及原因
    PHP书籍推荐
    PHP 语言结构(Language constructs)和函数的区别 (转)
    程序员问答网站:StackOverflow
    安装SQL SEVER 2005中的两个常见问题
    致IT同仁 —— IT人士常犯的17个职场错误
    PHP书写规范 PHP Coding Standard
  • 原文地址:https://www.cnblogs.com/yalphait/p/10226311.html
Copyright © 2011-2022 走看看