Balanced Lineup POJ 3264(线段树)

http://poj.org/problem?id=3264

题意:现有N个数字,问你在区间[a, b]之间最大值与最小值的差值为多少?

分析:线段树模板,不过需要有两个查询,一个查询在该区间的最大值,一个查询在该区间的最小值,最后两者结果相减即可。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <math.h>

using namespace std;

#define met(a, b) memset(a, b, sizeof(a))
#define maxn 51000
#define INF 0x3f3f3f3f
const int MOD = 1e9+7;

typedef long long LL;
#define lson rt<<1
#define rson rt<<1|1
int A[maxn], n;

struct node
{
    int l, r, maxs, mins;
}tree[maxn<<2];

void build(int l, int r, int rt)
{
    tree[rt].l = l;
    tree[rt].r = r;

    if(l == r)
    {
        tree[rt].maxs = A[l];
        tree[rt].mins = A[l];
        return ;
    }

    int mid = (l+r)/2;
    build(l, mid, lson);
    build(mid+1, r, rson);

    tree[rt].maxs = max(tree[lson].maxs, tree[rson].maxs);
    tree[rt].mins = min(tree[lson].mins, tree[rson].mins);
}

int query1(int l, int r, int rt)
{
    if(tree[rt].l==l && tree[rt].r==r)
        return tree[rt].maxs;

    int mid = (tree[rt].l+tree[rt].r)/2;

    if(r<=mid) return query1(l, r, lson);
    else if(l>mid) return query1(l, r, rson);
    else
    {
        int lmaxs = query1(l, mid, lson);
        int rmaxs = query1(mid+1, r, rson);
        int maxs = max(lmaxs, rmaxs);

        return maxs;
    }
}

int query2(int l, int r, int rt)
{
    if(tree[rt].l==l && tree[rt].r==r)
        return tree[rt].mins;

    int mid = (tree[rt].l+tree[rt].r)/2;

    if(r<=mid) return query2(l, r, lson);
    else if(l>mid) return query2(l, r, rson);
    else
    {
        int lmins = query2(l, mid, lson);
        int rmins = query2(mid+1, r, rson);
        int mins = min(lmins, rmins);

        return mins;
    }
}

int main()
{
    int q, a, b;

    while(scanf("%d %d", &n, &q)!=EOF)
    {
        for(int i=1; i<=n; i++)
            scanf("%d", &A[i]);

            build(1, n, 1);

        while(q --)
        {
            scanf("%d %d", &a, &b);

            int p = query1(a, b, 1);///查询最大值
            int q = query2(a, b, 1);///查询最小值

            printf("%d\n", p-q);
        }

    }
    return 0;
}

时间: 2024-10-16 18:21:04

Balanced Lineup POJ 3264(线段树)的相关文章

G - Balanced Lineup POJ 3264 (线段树+区间查询无更新)

G - Balanced Lineup Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3264 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 de

POJ - 3264——Balanced Lineup(入门线段树)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 68466   Accepted: 31752 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 Joh

poj 3264 Balanced Lineup(简单线段树 或 rmq)

Language: Default Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36833   Accepted: 17252 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.

poj 3264 线段树

线段树太弱了,题目逼格一高连代码都读不懂,今天开始重刷线段树,每天一题,风格用kuangbin大神和以前的,两种都写一遍 RMQ做法:poj3264 1 /* 2 POJ 3264 Balanced Lineup 3 题目意思:给定Q(1<=Q<=200000)个数A1,A2,```,AQ, 4 多次求任一区间Ai-Aj中最大数和最小数的差 5 */ 6 #include<stdio.h> 7 #include<algorithm> 8 #include<iost

POJ&mdash;&mdash;3264线段树

题目: 输入两个数(m,n),m表示牛的头数,n表示查询的个数.查询时输入两个数(x,y),表示查询范围的起始值和终止值,查询结果是,这个区间内牛重量的最大值减去牛重量的最小值,数量级为1000,000 设计每次查询的复杂度为logm. 例如, 输入: 6 3 1 7 3 4 2 5 1 5 4 6 2 2 输出: 6 3 0     分析:这道题是典型的空间换时间的题.一看到是区间的查找,我们应该想到线段树,这里有一篇我觉得写得挺好的介绍线段树的博客和大家分享一下:http://www.cnb

POJ - 3264(线段树实现)

题目:poj.org/problem?id=3264 题意:求一段区间内最大值与最小值的差. 看到区间最值首先想到RMQ--ST算法.但本题出现在了kuangbin专题的线段树里. 用线段树也无思维难点,但有两个坑: 1. 查询函数中,区间不包含时的返回值. 2.用cin,cout会TLE.用c的输入方式. 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<string&g

poj 3264 线段树 求区间最大最小值

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 rang

POJ 3264 线段树+二分

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 rang

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 Jo