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
  • 相关阅读:
    如何用视频云技术,搞一个爆红的 “反应视频” 项目?
    停车场事故频频,AI 达人将摄像头变身安全卫士
    WebRTC 传输安全机制第二话:深入显出 SRTP 协议
    阿里云视频云 Retina 多媒体 AI 体验馆开张啦!
    20 岁发表 SCI 的学霸,梦想用算法改变世界
    阿里绩效考核,简单到不可思议,员工死心塌地跟你干!(转)
    【官方】阿里巴巴合伙人制度全文(转)
    blob视频地址如何下载(转)
    软件开发项目规划时,SA、SD与SE的区别与重要性 【转】
    一分钟看懂公有云和私有云的区别
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7726019.html
Copyright © 2011-2022 走看看