zoukankan      html  css  js  c++  java
  • POJ 3264 线段树模板题

    之前做的那道是区间求和的,这道题是求区间最大值和最小值之差的,感觉这道题更简单。只需在插入时把每个区间的最大值最小值求出来保存在根节点上就可以啦~(^o^)/

    Balanced Lineup
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 39475   Accepted: 18524
    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
     1 #include<iostream>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<cstdio>
     5 using namespace std;
     6 #define INF 0x7fffffff
     7 int minv=INF,maxv=-INF;
     8 struct node
     9 {
    10     int l,r;
    11     int minv,maxv;
    12     int mid()
    13     {
    14         return (l+r)/2;
    15     }
    16 }tree[8000010];
    17 void buildtree(int root,int l,int r)
    18 {
    19     tree[root].l=l;
    20     tree[root].r=r;
    21     tree[root].minv=INF;
    22     tree[root].maxv=-INF;
    23     if(l!=r)
    24     {
    25         buildtree(root*2+1,l,(l+r)/2);
    26         buildtree(root*2+2,(l+r)/2+1,r);
    27     }
    28 }
    29 void insert(int root,int i,int v)
    30 {
    31     if(tree[root].l==tree[root].r)
    32     {
    33         tree[root].r=i;
    34         tree[root].minv=tree[root].maxv=v;
    35         return;
    36     }
    37     tree[root].minv=min(tree[root].minv,v);
    38     tree[root].maxv=max(tree[root].maxv,v);
    39     if(i<=tree[root].mid())
    40         insert(2*root+1,i,v);
    41     else
    42         insert(2*root+2,i,v);
    43 }
    44 void query(int root,int s,int e)
    45 {
    46     if(tree[root].minv>minv&&tree[root].maxv<maxv)
    47         return;
    48     if(tree[root].l==s&&tree[root].r==e)
    49     {
    50         minv=min(minv,tree[root].minv);
    51         maxv=max(maxv,tree[root].maxv);
    52         return;
    53     }
    54     if(e<=tree[root].mid())
    55         query(2*root+1,s,e);
    56     else if(s>tree[root].mid())
    57         query(2*root+2,s,e);
    58     else
    59     {
    60         query(2*root+1,s,tree[root].mid());
    61         query(2*root+2,tree[root].mid()+1,e);
    62     }
    63 }
    64 int main()
    65 {
    66     int n,q,h;
    67     int i,j,k;
    68     scanf("%d%d",&n,&q);
    69     buildtree(0,1,n);
    70     for(i=1;i<=n;i++)
    71     {
    72         scanf("%d",&h);
    73         insert(0,i,h);
    74     }
    75     for(i=0;i<q;i++)
    76     {
    77         int s,e;
    78         scanf("%d%d",&s,&e);
    79         minv=INF;
    80         maxv=-INF;
    81         query(0,s,e);
    82         printf("%d
    ",maxv-minv);
    83     }
    84     return 0;
    85 }
    View Code
  • 相关阅读:
    hdu 1425 sort 解题报告
    codeforces B. Jeff and Periods 解题报告
    codeforces A. Jeff and Digits 解题报告
    codeforces B. Xenia and Spies 解题报告
    Python 条件判断的使用
    Python Apache日志处理脚本(初稿)
    Python函数定义
    Python多条件配合使用
    Python循环的使用(2)
    NavigationController导航栏中添加多个UIBarButtonItem
  • 原文地址:https://www.cnblogs.com/zero-zz/p/4701328.html
Copyright © 2011-2022 走看看