Balanced Lineup(RMQ)

原题传送门

就是裸RMQ啊。。

求区间最大值和区间最小值,一看就像RMQ,当然线段树貌似也可以。

至于算法嘛。自己学~(好吧,放个传送门。。。

然后就是最后把maxsum-minsum就好啦233~

时间效率:预处理O(N)查找O(1)

是不是很快~

下面贴代码

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int n,q;
int a,b;
int maxsum,minsum;
int maxn[50001][20];
int num[50001];
int minn[50001][20];
void RMQ(int s)
{
    for(int j=1;j<=19;j++)
    for(int i=1;i<=n;i++){
        if(i+(1<<j)-1<=n)
        {
            maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j-1]);
            minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j-1]);
        }
    }
}
int main(){
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&num[i]);
        maxn[i][0]=minn[i][0]=num[i];
    }
    RMQ(n);
    for(int i=1;i<=q;i++)
    {
        scanf("%d%d",&a,&b);
        int k=(int)(log(b-a+1.0)/log(2.0));
        maxsum=max(maxn[a][k],maxn[b-(1<<k)+1][k]);
        minsum=min(minn[a][k],minn[b-(1<<k)+1][k]);
        printf("%d\n",maxsum-minsum);
    }
}
时间: 2024-10-08 01:00:51

Balanced Lineup(RMQ)的相关文章

POJ 3264 Balanced Lineup(RMQ)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36513   Accepted: 17103 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: 39046   Accepted: 18291 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

P2880 [USACO07JAN]平衡的阵容Balanced Lineup(RMQ的倍增模板)

题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup RMQ问题:给定一个长度为N的区间,M个询问,每次询问Li到Ri这段区间元素的最大值/最小值. RMQ的高级写法一般有两种,即为线段树(并不很会╥﹏╥...)和ST表(一种利用dp求解区间最值的倍增算法) 定义:maxx[i][j]和minn[i][j]分别表示i到i+2^j-1这段区间的最大值和最小值. 预处理:maxx[i][0]=minn[i][0]=a[i].即i到i区间的最大值.最小值都是a[i]. 状

POJ 3264:Balanced Lineup(RMQ模板题)

http://poj.org/problem?id=3264 题意:给出n个数,还有q个询问,询问[l,r]区间里面最大值和最小值的差值. 思路:RMQ模板题,开两个数组维护最大值和最小值就行. 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 using namespace std; 6 #define N 50010 7 #define

POJ Balanced Lineup(RMQ_ST)

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

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

UVA 11235 Frequent values(RMQ)

Frequent values TimeLimit:3000Ms You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consisting of indices i and j (1 ≤ i ≤ j ≤ n). For each query, determine the most f

【POJ 3368】 Frequent values(RMQ)

[POJ 3368] Frequent values(RMQ) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15813   Accepted: 5749 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given seve

hdu 3183 A Magic Lamp(RMQ)

A Magic Lamp                                                                               Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Kiki likes traveling. One day she finds a magic lamp, u