Balanced Lineup -POJ3264

题意:

告诉你n头奶牛的高度,然后给你一个区间,你需要求出这个区间最高的奶牛与最矮的奶牛之间相差多少

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

思路:

线段树区间查询,用两个查询函数,一个查最大值,另一个查最小值,将他们相减即可。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int MAXN=1e5+5;
const int INF=0x7fffffff;
typedef long long ll;
int lazy[MAXN<<2],max_[MAXN<<2],min_[MAXN<<2];
//这里在push_up的时候我们将区间最大值和区间最小值都更新一下
void push_up(int node)
{
    max_[node]=max(max_[node<<1],max_[node<<1|1]);
    min_[node]=min(min_[node<<1],min_[node<<1|1]);
}
void build(int node,int l,int r)
{
    if(l==r)
    {
        scanf("%d",&max_[node]);
        min_[node]=max_[node];
        return ;
    }
    int mid=(l+r)>>1;
    build(node<<1,l,mid);
    build(node<<1|1,mid+1,r);
    push_up(node);
}
int query1(int node,int l,int r,int x,int y)
{
    if(x<=l&&y>=r)
    {
        return max_[node];
    }
    int max1=0,min1=INF;
    int mid=(l+r)>>1;
    if(x<=mid)max1=max(max1,query1(node<<1,l,mid,x,y));
    if(y>mid)max1=max(query1(node<<1|1,mid+1,r,x,y),max1);
    return max1;
}
int query2(int node,int l,int r,int x,int y)
{
    if(x<=l&&y>=r)
    {
        return min_[node];
    }
    int min1=INF;
    int mid=(l+r)>>1;
    if(x<=mid)min1=min(min1,query2(node<<1,l,mid,x,y));
    if(y>mid)min1=min(query2(node<<1|1,mid+1,r,x,y),min1);
    return min1;
}
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    build(1,1,n);
    for(int i=1;i<=k;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        printf("%d\n",query1(1,1,n,a,b)-query2(1,1,n,a,b));
    }
    return 0;
}

原文地址:https://www.cnblogs.com/ljxdtc666/p/12222925.html

时间: 2024-10-18 21:01:27

Balanced Lineup -POJ3264的相关文章

G - Balanced Lineup - poj3264(区间查询)

题意:给你一组值,然后询问某个区间的最大值和最小值得差 分析:因为没有更新,所以只需要查找即可,节点保存一个最大值最小值就行了 ****************************************************************** #include<stdio.h>#include<math.h>#include<string.h>#include<algorithm>using namespace std;#define Ls

Balanced Lineup(POJ-3264)(线段树)

很基础的一道线段树的题,有个地方卡了我好久,我下面的这个代码所求的区间是左闭右开的,所以如果所求区间包括区间端点的话需要在右区间上+1 线段树是一种高效的数据结构,特点是求一个区间里的最小.最大值.      数据结构感觉像模板,但是其中的思想应该更值得我们学习,不过话说现在也没几个人能静下心去研究它的原理了吧.. #include<cstdio> #include<cstring> #include<iostream> #include<algorithm>

poj3264 Balanced Lineup(求区间的最大值与最小值之差)

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

POJ3264 Balanced Lineup RMQ 线段树

求区间内最大数和最小数的差,用两棵线段树,一个维护区间最大值,一个维护区间最小值. #include <stdio.h> #include <vector> #include <math.h> #include <string.h> #include <string> #include <iostream> #include <queue> #include <list> #include <algori

POJ3264——Balanced Lineup(线段树)

本文出自:http://blog.csdn.net/svitter 题意:在1~200,000个数中,取一段区间,然后在区间中找出最大的数和最小的数字,求这两个数字的差. 分析:按区间取值,很明显使用的线段树.区间大小取200000 * 4 = 8 * 10 ^5; 进行查询的时候,注意直接判断l, r 与mid的关系即可,一开始写的时候直接与tree[root].L判断,多余了, 逻辑不正确. #include <iostream> #include <stdio.h> #inc

POJ3264 Balanced Lineup 线段树 RMQ ST算法应用

Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 36813 Accepted: 17237 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 de

POJ-3264 Balanced Lineup(线段树)

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

poj3264 Balanced Lineup 2011-12-20

Balanced Lineup Time Limit: 5000MSMemory Limit: 65536K Total Submissions: 20569Accepted: 9552 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 decid

POJ3264 Balanced Lineup 【线段树】+【单点更新】

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