POJ 3264 Balanced Lineup(RMQ)
来源:互联网 发布:投资软件 编辑:程序博客网 时间:2024/06/06 08:34
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 range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.
Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.
Input
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.
Output
Sample Input
6 31734251 54 62 2
Sample Output
630
Source
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 200000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
bool cmp(int a,int b){
return a>b;
}
vector<int>a;
int d[maxn][24],p[maxn][24];
int n,m;
void init(const vector<int>&a)
{
int n=a.size();
for(int i=0;i<n;i++)
{
d[i][0]=a[i];
p[i][0]=a[i];
}
for(int j=1;(1<<j)<=n;j++)
for(int i=0;i+(1<<j)-1<n;i++)
{
d[i][j]=min(d[i][j-1],d[i+(1<<(j-1))][j-1]);
p[i][j]=max(p[i][j-1],p[i+(1<<(j-1))][j-1]);
}
}
int rmq(int l,int r)
{
int k=0;
while((1<<(k+1))<=r-l+1)k++;
return max(p[l][k],p[r-(1<<k)+1][k])-min(d[l][k],d[r-(1<<k)+1][k]);
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d%d",&n,&m)!=EOF)
{
int t;
a.clear();
for(int i=1;i<=n;i++)
{
scanf("%d",&t);
a.push_back(t);
}
init(a);
int x,y;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
printf("%d\n",rmq(x-1,y-1));
}
}
return 0;
}
- poj 3264 Balanced Lineup (RMQ))
- poj 3264 Balanced Lineup(RMQ)
- POJ 3264 Balanced Lineup(简单RMQ)
- Balanced Lineup(RMQ)(POJ 3264)
- POJ 3264--Balanced Lineup(RMQ问题)
- POJ 3264 Balanced Lineup(RMQ详解)
- POJ 3264 Balanced Lineup(RMQ模板)
- POJ 题目3264 Balanced Lineup(RMQ)
- poj 3264 Balanced Lineup(RMQ)
- POJ 3264 Balanced Lineup(RMQ)
- poj 3264 Balanced Lineup(RMQ)
- POJ 3264 Balanced Lineup(RMQ)
- POJ 3264 Balanced Lineup(RMQ)
- POJ 3264 Balanced Lineup(RMQ)
- POJ 3264 Balanced Lineup(RMQ)
- POJ - 3264 - Balanced Lineup(RMQ)
- POJ 3264 Balanced Lineup(RMQ)
- POJ 3264 Balanced Lineup (RMQ)
- HDU 5166 Missing number
- POJ 2528 Mayor\'s posters
- POJ 2828 Buy Tickets
- POJ 3468 A Simple Problem with Integers(区间更新)
- Can you find it?(二分)
- POJ 3264 Balanced Lineup(RMQ)
- POJ 3368 Frequent values (RMQ)
- HDU 1698 Just a Hook
- HDU 3974 Assign the task
- POJ 3321 Apple Tree
- POJ 2155 Matrix(二维线段树)
- 加载顺序,一个类的
- HDU 2492 Ping pong
- POJ 2299 Ultra-QuickSort