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;
    }
  • 相关阅读:
    Java之冒泡完整理解
    Java 之数组的复制,
    python 小白之路(跳动的球)
    小白之旅,Python运算符
    在学习枯燥的Java中遇见美丽的Jframe,窗体中的单选按钮(JRadioButton)
    自定义的无数据提示界面
    北京地铁站经纬度集合(包含大部分)
    自定义UItextFiled,限制TextFiled 的输入长度
    消息发送机制的利用
    app 后台持续定位
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770747.html
Copyright © 2011-2022 走看看