Balanced Lineup---poj3264线段树基础

题目链接

求对应区间最大值与最小值的差;

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define INF 0xfffffff
#define N 50010
using namespace std;

#define Lson r<<1
#define Rson r<<1|1

struct SegTree
{
    int L, R, Min, Max;
    int mid()
    {
        return (L+R)>>1;
    }
}a[N*4];
int MIN,MAX;
void BuildSegTree(int r, int L, int R)
{
    a[r].L = L, a[r].R = R;
    if(L == R)
    {
        scanf("%d", &a[r].Max);
        a[r].Min = a[r].Max;
        return;
    }
    BuildSegTree(Lson, L, a[r].mid());
    BuildSegTree(Rson, a[r].mid()+1, R);

    a[r].Max = max(a[Rson].Max, a[Lson].Max);
    a[r].Min = min(a[Rson].Min, a[Lson].Min);
   // a[r].ans = a[r].Max - a[r].Min;
}
void Query(int r, int L, int R)
{
    if(a[r].L == L && a[r].R == R)
    {
        MAX=max(MAX, a[r].Max);
        MIN=min(MIN, a[r].Min);
        return ;
    }
    if(L>a[r].mid())
        Query(Rson, L ,R);
    else if(R <= a[r].mid())
        Query(Lson, L, R);
    else
    {
        Query(Lson, L, a[r].mid());
        Query(Rson, a[r].mid()+1, R);
    }
}
int main()
{
    int n, m, L, R;
    while(scanf("%d%d", &n, &m) != EOF)
    {
        BuildSegTree(1, 1, n);
        while(m--)
        {
            MIN=INF;
            MAX=-1;
            scanf("%d%d", &L,&R);
            Query(1, L, R);
            printf("%d\n", MAX-MIN);
        }
    }
    return 0;
}

时间: 2024-10-06 07:43:52

Balanced Lineup---poj3264线段树基础的相关文章

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

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

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 【线段树】+【单点更新】

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

poj 3264 Balanced Lineup RMQ线段树实现

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36613   Accepted: 17141 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 3246 Balanced Lineup(线段树)

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

A - Balanced Lineup Crawling in process... Crawling failed 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 li

POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 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 (线段树)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 39060   Accepted: 18299 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/线段树)

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