POJ_3264_Balanced Lineup

2018-06-17 21:46:53来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 57259   Accepted: 26831
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 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 ≤ A ≤ B ≤ N), 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

Source

USACO 2007 January Silver
 
  • 区间最值RMQ
  • ST打表

 

 

 1 #include <iostream>
 2 #include <string>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <cmath>
 8 #include <vector>
 9 #include <queue>
10 #include <stack>
11 #include <set>
12 #include <map>
13 using namespace std;
14 typedef long long           LL ;
15 typedef unsigned long long ULL ;
16 const int    maxn = 5e5 + 10   ;
17 const int    inf  = 0x3f3f3f3f ;
18 const int    npos = -1         ;
19 const int    mod  = 1e9 + 7    ;
20 const int    mxx  = 100 + 5    ;
21 const double eps  = 1e-6       ;
22 const double PI   = acos(-1.0) ;
23 
24 int n, m, k, A, B, u, v;
25 int a[maxn], mx[maxn][30], mi[maxn][30], fac[30];
26 int main(){
27     // freopen("in.txt","r",stdin);
28     // freopen("out.txt","w",stdout);
29     for(int i=0;i<30;i++)
30         fac[i]=(1<<i);
31     while(~scanf("%d %d",&n,&m)){
32         for(int i=1;i<=n;i++){
33             scanf("%d",&mx[i][0]);
34             mi[i][0]=mx[i][0];
35         }
36         int k=(int)(log((double)n)/log(2.0));
37         for(int j=1;j<=k;j++)
38             for(int i=1;i+fac[j]-1<=n;i++){
39                 mx[i][j]=max(mx[i][j-1],mx[i+fac[j-1]][j-1]);
40                 mi[i][j]=min(mi[i][j-1],mi[i+fac[j-1]][j-1]);
41             }
42         while(m--){
43             scanf("%d %d",&u,&v);
44             k=(int)(log((double)(v-u+1))/log(2.0));
45             A=max(mx[u][k],mx[v-fac[k]+1][k]);
46             B=min(mi[u][k],mi[v-fac[k]+1][k]);
47             printf("%d\n",A-B);
48         }
49     }
50     return 0;
51 }

 

 

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:【luogu 3372】【模板】线段树1

下一篇:POJ_2019_Cornfields