zoukankan      html  css  js  c++  java
  • Balanced Lineup(简单的线段树)

    Balanced Lineup
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 26339   Accepted: 12351
    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

     
    简单的线段树~轻松1A :)
     
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <string>
     4 #include <set>
     5 #include <map>
     6 #include <vector>
     7 #include <queue>
     8 #include <cstdio>
     9 #include <cstring>
    10 #include <cmath>
    11 using namespace std;
    12 
    13 const int maxn = 200001;
    14 struct Node
    15 {
    16     int l, r, ma, mi; //ma是最低高度,mi是最高高度
    17 }t[maxn<<2];
    18 
    19 int Max(int a, int b) {return a > b ? a : b;}
    20 int Min(int a, int b) {return a > b ? b : a;}
    21 
    22 void Build(int left, int right, int rt)
    23 {
    24     t[rt].l = left;
    25     t[rt].r = right;
    26     if(left == right)
    27     {
    28         scanf("%d", &t[rt].mi);
    29         t[rt].ma = t[rt].mi;
    30         return ;
    31     }
    32     int mid = (left + right) >> 1;
    33     Build(left, mid, rt<<1);
    34     Build(mid + 1, right, rt<<1|1);
    35     t[rt].mi = Min(t[rt<<1].mi, t[rt<<1|1].mi);
    36     t[rt].ma = Max(t[rt<<1].ma, t[rt<<1|1].ma);
    37 }
    38 
    39 void Query(int from, int to, int rt, int& ma, int& mi)
    40 {
    41     if(from <= t[rt].l && t[rt].r <= to)
    42     {
    43         if(ma < t[rt].ma) ma = t[rt].ma;
    44         if(mi > t[rt].mi) mi = t[rt].mi;
    45         return ;
    46     }
    47     int mid = (t[rt].l + t[rt].r) >> 1;
    48     if(to > mid) Query(from, to, rt<<1|1, ma, mi);
    49     if(from <= mid) Query(from, to, rt<<1, ma, mi);
    50     return ;
    51 }
    52 
    53 int main()
    54 {
    55     int n, q;
    56     int a, b, ma, mi;
    57     while(scanf("%d %d", &n, &q) != EOF)
    58     {
    59         Build(1, n, 1);
    60         while(q--)
    61         {
    62             scanf("%d %d", &a, &b);
    63             mi = 1000001, ma = -1;
    64             Query(a, b, 1, ma, mi);
    65             printf("%d\n", ma - mi);
    66         }
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    HTML总结
    Java 基础知识总结 (三、运算符)
    关于JS 事件冒泡和onclick,click,on()事件触发顺序
    Java 基础知识总结 (四、String)
    Java 基础知识总结 (二、基本数据类型)
    websocket实例(显示文件导入处理进度)
    Java 基础知识总结 (一、标识符)
    Java Calendar 注意事项
    Ajax调用SpringMVC ModelAndView 无返回情况
    关于Ajax load页面中js部分$(function(){})的执行顺序
  • 原文地址:https://www.cnblogs.com/cszlg/p/2966743.html
Copyright © 2011-2022 走看看