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

    Balanced Lineup
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 47419   Accepted: 22270
    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 ≤ ABN), 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

     
     
      线段树基础题。
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <map>
     4 #include <vector>
     5 #include <functional>
     6 #include <string>
     7 #include <cstring>
     8 #include <queue>
     9 #include <set>
    10 #include <cmath>
    11 #include <cstdio>
    12 using namespace std;
    13 #define IOS ios_base::sync_with_stdio(false)
    14 #define TIE std::cin.tie(0)
    15 #define MIN2(a,b) (a<b?a:b)
    16 #define MIN3(a,b) (a<b?(a<c?a:c):(b<c?b:c))
    17 #define MAX2(a,b) (a>b?a:b)
    18 #define MAX3(a,b,c)  (a>b?(a>c?a:c):(b>c?b:c))
    19 typedef long long LL;
    20 typedef unsigned long long ULL;
    21 const int INF = 0x3f3f3f3f;
    22 const double PI = 4.0*atan(1.0);
    23 
    24 const int MAX_N = 50005;
    25 int tot, n, qmax, qmin;
    26 struct Node{
    27     int L, R, a, b, maxr, minr;
    28 }T[4*MAX_N];
    29 inline void Up(int x)
    30 {
    31     T[x].maxr = MAX2(T[T[x].L].maxr, T[T[x].R].maxr);
    32     T[x].minr = MIN2(T[T[x].L].minr, T[T[x].R].minr);
    33 }
    34 void Build(int L, int R)
    35 {
    36     int x = ++tot;
    37     T[x].a = L; T[x].b = R;
    38     T[x].L = T[x].R = 0;
    39     if (L == R) {
    40         int temp;
    41         scanf("%d", &temp); 
    42         T[x].maxr = T[x].minr = temp;
    43         return; 
    44     }
    45     int mid = (L + R) >> 1;
    46     T[x].L = tot + 1; Build(L, mid);
    47     T[x].R = tot + 1; Build(mid + 1, R);
    48     Up(x);
    49 }
    50 
    51 void Query(int x, int a, int b)
    52 {
    53     if (T[x].a >= a&&T[x].b <= b){ 
    54         qmax =MAX2(qmax, T[x].maxr); 
    55         qmin = MIN2(qmin, T[x].minr);
    56         return; 
    57     }
    58     int mid = (T[x].a + T[x].b) >> 1;
    59     int maxl = -INF, maxr = -INF, minl = INF, minr = INF;
    60     if (mid >= a&&T[x].L)
    61         Query(T[x].L, a, b);
    62     if (mid < b&&T[x].R)
    63         Query(T[x].R, a, b);
    64 }
    65 
    66 int main()
    67 {
    68     int qn,a,b,ma=0,mi=0;
    69     while (~scanf("%d%d", &n, &qn)){
    70         tot = 0;
    71         Build(1, n);
    72         for (int i = 0; i < qn; i++){
    73             scanf("%d%d", &a, &b);
    74             qmax = -INF; qmin = INF;
    75             Query(1, a, b);
    76             printf("%d
    ", qmax - qmin);
    77         }
    78     }
    79 }
  • 相关阅读:
    AndroidAlarmManager(全局定时器/闹钟)
    Android dialog,activity 屏蔽Home键的教程详解
    实例教程一:电话拨号器
    Android的进程优先级与进程回收详解
    Android Bitmap内存限制
    【30篇突击 android】源码统计七
    在access中支持2个以上left join的方法,又是access的变态规则
    修改vs2005的键盘风格设置
    IE与Firefox的CSS兼容大全(转载)
    实现页面内多个表格在滚动时,表头浮动的效果(是同时多个表格哟)
  • 原文地址:https://www.cnblogs.com/cumulonimbus/p/5837558.html
Copyright © 2011-2022 走看看