poj 3264 Balanced Lineup(查询区间最大值与最小值的差)

1.代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define N 100000

int a[N];
int ST1[N][20];
int ST2[N][20];
int n,q;

void make_ST()
{
    for(int j=1;(1<<j)<=n;j++)
    {
        for(int i=1;(i+(1<<j)-1)<=n;i++)
        {
            ST1[i][j]=Max(ST1[i][j-1],ST1[i+(1<<(j-1))][j-1]);
            ST2[i][j]=Min(ST2[i][j-1],ST2[i+(1<<(j-1))][j-1]);
        }
    }
}

int Query(int l,int r)
{
    int k=floor(log((double)(r-l+1))/log((double)(2)));
    return Max(ST1[l][k],ST1[r-(1<<k)+1][k])-Min(ST2[l][k],ST2[r-(1<<k)+1][k]);
}

int main()
{
    while(scanf("%d%d",&n,&q)==2)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            ST1[i][0]=ST2[i][0]=a[i];
        }
        make_ST();
        while(q--)
        {
            int l,r;
            scanf("%d%d",&l,&r);
            printf("%d\n",Query(l,r));
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-07 19:56:26

poj 3264 Balanced Lineup(查询区间最大值与最小值的差)的相关文章

poj 3264 Balanced Lineup(线段数求区间最大最小值)

链接:http://poj.org/problem?id=3264 Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32772   Accepted: 15421 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 Balanced Lineup ST表

链接:http://poj.org/problem?id=3264 题意:给一串数字,多次询问,求区间最大值和区间最小值的差. 思路:RMQ问题,可以用O(N^2)的预处理,然后每次O(1)的查询,可以用线段树,O(N)的建树,O(logN)的查询,可以用ST表记录,O(NlogN)的预处理,O(1)的查询. 实际上ST表的预处理过程也是一个DP的过程dp[i][j]表示从第i位开始连续2^j位的区间最值. 预处理:dp[i][j]=min(dp[i][j],dp[i+2^j][j]),查询:q

poj 3264 Balanced Lineup

题目链接:http://poj.org/problem?id=3264 题目大意:就是给你一串数,问你最大数和最小数的差值....... 思路:最基本的线段树,只需要建树和查询,修改都省啦,但是查询要写两个,一个查询最大值,一个查询最小值......然后就能AC掉.....但是话说poj把它分类到RMQ中.... code: #include<cstdio> #include<cmath> #include<algorithm> #include<iostream

[POJ] 3264 Balanced Lineup [ST算法]

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 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-ST查询区间最大最小值之差 +模板应用】

题目地址:http://poj.org/problem?id=3264 Sample Input 6 3 1 7 3 4 2 5 1 5 4 6 2 2 Sample Output 6 3 0分析:标准的模板题,可以用线段树写,但用RMQ-ST来写代码比较短.每次输出区间[L, R]内最大值和最小值的差是多少.注意一个地方,代码里面用到了log2()函数,但是我用包含<math.h>和<cmath>头文件的代码以C++的方式提交到POJ反馈是编译错误.改成g++提交才AC了.(注意

poj 3264 Balanced Lineup 区间极值RMQ

题目链接:http://poj.org/problem?id=3264 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 wil

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 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

POJ 3264 Balanced Lineup (线段树)

Balanced Lineup 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