zoukankan      html  css  js  c++  java
  • POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值

    题目链接:https://vjudge.net/problem/POJ-3264

    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.. NQ+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 <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 using namespace std;
    13 typedef long long LL;
    14 const double EPS = 1e-8;
    15 const int INF = 2e9;
    16 const LL LNF = 2e18;
    17 const int MAXN = 5e4+10;
    18 
    19 int maxv[MAXN<<2], minv[MAXN<<2];
    20 
    21 void push_up(int u)
    22 {
    23     maxv[u] = max(maxv[u*2], maxv[u*2+1]);
    24     minv[u] = min(minv[u*2], minv[u*2+1]);
    25 }
    26 
    27 void add(int u, int l, int r, int x, int val)
    28 {
    29     if(l==r)
    30     {
    31         maxv[u] = minv[u] = val;
    32         return;
    33     }
    34 
    35     int mid = (l+r)>>1;
    36     if(x<=mid) add(u*2, l, mid, x, val);
    37     else add(u*2+1, mid+1, r, x, val);
    38     push_up(u);
    39 }
    40 
    41 int maxx, minn;
    42 void query(int u, int l, int r, int x, int y)
    43 {
    44     if(x<=l && r<=y)
    45     {
    46         maxx = max(maxx, maxv[u]);
    47         minn = min(minn, minv[u]);
    48         return;
    49     }
    50 
    51     int mid = (l+r)>>1;
    52     if(x<=mid) query(u*2, l, mid, x, y);
    53     if(y>=mid+1) query(u*2+1, mid+1, r, x, y);
    54 }
    55 
    56 int main()
    57 {
    58     int n, m;
    59     while(scanf("%d%d", &n, &m)!=EOF)
    60     {
    61         int x, y;
    62         for(int i = 1; i<=n; i++)
    63         {
    64             scanf("%d", &x);
    65             add(1, 1, n, i, x);
    66         }
    67 
    68         for(int i = 1; i<=m; i++)
    69         {
    70             scanf("%d%d", &x, &y);
    71             maxx = -INF, minn = INF;
    72             query(1, 1, n, x, y);
    73             printf("%d
    ", maxx-minn);
    74         }
    75     }
    76 }
    View Code
  • 相关阅读:
    sleep() 和 wait() 区别是什么?
    JAVA面试中需要准备的点
    javascript 取掉空格自定义函数
    code manager tools myeclipse10 svn插件安装及使用
    javascript 常用兼容fire fox的解决办法
    javascript 获取标签内的内容
    javascript dom与字符串相互转换
    javascript window.showModalDialog不兼容goole解决方案
    javascript js获取url及url参数解析
    javascript table排序之jquery.tablesorter.js
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7726019.html
Copyright © 2011-2022 走看看