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

Line 1: Two space-separated integers, N and Q.
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 ≤ ABN), representing the range of cows from A to B inclusive.

Output

Lines 1.. Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

Sample Input

6 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #define INF 1e8
 6 #define lson l , m , rt << 1
 7 #define rson m+1,r,  rt << 1 | 1
 8 using namespace std;
 9
10 const int maxn=55000;
11
12 int n,q;
13 int mi[maxn<<2],ma[maxn<<2];
14
15 void Pushup(int rt){
16     mi[rt]=min(mi[rt<<1],mi[(rt<<1)|1]);
17     ma[rt]=max(ma[rt<<1],ma[(rt<<1)|1]);
18 }
19
20 void Build(int l,int r,int rt){
21     if(l==r){
22         scanf("%d",&mi[rt]);
23         ma[rt]=mi[rt];
24         return;
25     }
26     int m=(l+r)>>1;
27     Build(lson);
28     Build(rson);
29     Pushup(rt);
30 }
31
32 int Query_min(int L,int R,int l,int r,int rt){
33     if(L<=l&&r<=R) return mi[rt];
34     int m=(l+r)>>1;
35     int temp_min=INF;
36     if(L<=m) temp_min=min(temp_min,Query_min(L,R,lson));
37     if(R>m)  temp_min=min(temp_min,Query_min(L,R,rson));
38     return temp_min;
39 }
40
41 int Query_max(int L,int R,int l,int r,int rt){
42     if(L<=l&&r<=R) return ma[rt];
43     int m=(l+r)>>1;
44     int temp_max=0;
45     if(L<=m) temp_max=max(temp_max,Query_max(L,R,lson));
46     if(R>m)  temp_max=max(temp_max,Query_max(L,R,rson));
47     return temp_max;
48 }
49
50 int main()
51 {   cin>>n>>q;
52     Build(1,n,1);
53     while(q--){
54         int a,b;
55         scanf("%d%d",&a,&b);
56         int ans=Query_max(a,b,1,n,1)-Query_min(a,b,1,n,1);
57         cout<<ans<<endl;
58     }
59     return 0;
60 }
时间: 2024-08-03 05:52:12

Balanced Lineup POJ - 3264的相关文章

G - Balanced Lineup POJ 3264 (线段树+区间查询无更新)

G - Balanced Lineup 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 line up in the same order. One day Farmer John de

Balanced Lineup POJ 3264(线段树)

http://poj.org/problem?id=3264 题意:现有N个数字,问你在区间[a, b]之间最大值与最小值的差值为多少? 分析:线段树模板,不过需要有两个查询,一个查询在该区间的最大值,一个查询在该区间的最小值,最后两者结果相减即可. #include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <vector> #inc

Gold Balanced Lineup - poj 3274 (hash)

这题,看到别人的解题报告做出来的,分析: 大概意思就是: 数组sum[i][j]表示从第1到第i头cow属性j的出现次数. 所以题目要求等价为: 求满足 sum[i][0]-sum[j][0]=sum[i][1]-sum[j][1]=.....=sum[i][k-1]-sum[j][k-1] (j<i) 中最大的i-j 将上式变换可得到 sum[i][1]-sum[i][0] = sum[j][1]-sum[j][0] sum[i][2]-sum[i][0] = sum[j][2]-sum[j]

G - Balanced Lineup

G - Balanced Lineup POJ - 3264 思路:水题,线段树的基本操作即可. #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 50010 using namespace std; int n,q; struct nond{ int l,r,min,max; }tree[MAXN*4]; void up(int no

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(线段数求区间最大最小值)

链接: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 Rmq模板

Balanced Lineup 题目链接: http://poj.org/problem?id=3264 题意: 求区间最大值和最小值的差 题解: Rmq模板题 代码 #include<stdio.h> #include<math.h> const int N=5e4+1; int dpmax[N][17]; int dpmin[N][17]; int mmax(int x,int y) { return x>y?x:y; } int mmin(int x,int y) {

POJ 3264 Balanced Lineup (线段树单点更新 区间查询)

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