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
  • 相关阅读:
    开发移动端项目如何利用Chrome浏览器连接真机测试
    有关浏览器开发者工具使用的技巧
    有关前端实时可视化工具的使用 ==实现边改代码边看效果
    vue的生命周期介绍beforeCreate(创建前)、created(创建后)、beforeMount(载入前)、mounted(载入后)、beforeUpdate(更新前)、updated(更新后)、beforeDestroy(销毁前)、destroyed(销毁后)
    echart中重新定义引导线的文字换行<br>不起作用
    使用mock数据填写表格同时带点击查看更多
    有关前后端分离前端如何使用mock数据
    echar图柱状图和折线图混合加双侧y轴坐
    【Oracle】DBMS_STATS.GATHER_TABLE_STATS
    【PostgreSQL-9.6.3】Red Hat 4.4.7下的安装
  • 原文地址:https://www.cnblogs.com/zero-zz/p/4701328.html
Copyright © 2011-2022 走看看