zoukankan      html  css  js  c++  java
  • POJ 3264 Balanced Lineup 【线段树】

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

    USACO 2007 January Silver
    用线段树查询每个区间上最大值与最小值的差值,用一个线段树维护最小值,一个维护最大值。

    #include <map>
    #include <set>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <iostream>
    #include <stack>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <cstdlib>
    //#include <bits/stdc++.h>
    //#define LOACL
    #define space " "
    using namespace std;
    typedef long long LL;
    typedef __int64 Int;
    typedef pair<int, int> paii;
    const int INF = 0x3f3f3f3f;
    const double ESP = 1e-5;
    const double PI = acos(-1.0);
    const int MOD = 1e9 + 7;
    const int MAXN = 200000 + 10;
    int data[MAXN];
    struct node {
        int l, r, value;
    } segmin[MAXN], segmax[MAXN];
    int build_segm_max(int x, int lson, int rson) {
        segmax[x].l = lson;
        segmax[x].r = rson;
        if (lson != rson) {
            int a = build_segm_max(x << 1, lson, (rson + lson)/2);
            int b = build_segm_max((x << 1) + 1, (rson + lson)/2 + 1, rson);
            return segmax[x].value = max(a, b);
        }
        return segmax[x].value = data[lson];
    }
    int build_segm_min(int x, int lson, int rson) {
        segmin[x].l = lson;
        segmin[x].r = rson;
        if (lson != rson) {
            int a = build_segm_min(x << 1, lson, (rson + lson)/2);
            int b = build_segm_min((x << 1) + 1, (rson + lson)/2 + 1, rson);
            return segmin[x].value = min(a, b);
        }
        return segmin[x].value = data[lson];
    }
    int query_seg_max(int x, int lson, int rson) {
        if (segmax[x].l > rson || segmax[x].r < lson) return 0;
        if (segmax[x].l >= lson && segmax[x].r <= rson) return segmax[x].value;
        int a = query_seg_max(x << 1, lson, rson);
        int b = query_seg_max((x << 1) + 1, lson, rson);
        return max(a, b);
    }
    int query_seg_min(int x, int lson, int rson) {
        if (segmin[x].l > rson || segmin[x].r < lson) return INF;
        if (segmin[x].l >= lson && segmin[x].r <= rson) return segmin[x].value;
        int a = query_seg_min(x << 1, lson, rson);
        int b = query_seg_min((x << 1) + 1, lson, rson);
        return min(a, b);
    }
    int main() {
        int N, Q, a, b;
        scanf("%d%d", &N, &Q);
        for (int i = 1; i <= N; i++) scanf("%d", &data[i]);
        build_segm_max(1, 1, N); build_segm_min(1, 1, N);
        for (int i = 0; i < Q; i++) {
            scanf("%d%d", &a, &b);
            printf("%d
    ", query_seg_max(1, a, b) - query_seg_min(1, a, b));
        }
        return 0;
    }
  • 相关阅读:
    bound 很靠近0的mle
    order statistics 为什么3维要把6块加起来
    正态分布的来历
    互斥和独立
    平稳随机过程 会不会和随机过程矛盾呢?
    请问如何理解同一个随机过程,里面的两个随机变量的期望不同呢?
    bagging和boosting以及rand-forest
    arr = map(float,arr)输出问题
    lasso数学解释
    岭回归的数学解释
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770747.html
Copyright © 2011-2022 走看看