Title index: http://poj.org/problem?id=3264 Balanced Lineup
Time Limit: 5000MS |
|
Memory Limit: 65536K |
Total Submissions: 58842 |
|
Accepted: 27537 |
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'll take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to has 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 G Roup.
Input line 1:two space-separated integers, N and Q.
Lines 2.. N+1:line i+1 contains a single integer so 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 so is a response to a reply and indicates the difference in height between the TA Llest 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 January Silver
The main topic: There are n number and M inquiry, each inquiry (L,R) to find the difference between the maximum and the minimum value of the sequence interval l~r.
The topic read the problem and do it well. First of all, violence is easy to think of, but it must be timed out; in fact, there are many ways to do this, and it is not difficult to think of line segments.
Because the problem is too simple, we just need to build a line tree that maintains the interval max and min (in fact there is no maintenance without modification).
The code is as follows:
#include <iostream> #include <cmath> #include <cstring> #include <algorithm> #include <
cstdio> #define _M 100000+5 #define INF 1e9 using namespace std;
struct node{int a,b,max,min;};
Node Tree[4*_m];
int n,q;
int num[_m];
int Read () {bool F=0;int X=0;char Ch=getchar (); while (ch> ' 9 ' | | ch< ' 0 ') {if (ch== '-') f=1;
Ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x= (x<<3) + (x<<1) +ch-' 0 ';
Ch=getchar ();}
return f?-x:x;
} void Write (int X) {if (x<0) X=-x,putchar ('-');
if (x>9) Write (X/10);
Putchar (x%10+48); } void Maketree (int p,int x,int Y) {//achievement tree[p].
A=x; TREE[P].
B=y;
if (x<y) {maketree (p<<1,x,x+y>>1);
Maketree (P<<1|1, (x+y>>1) +1,y); TREE[P]. Max=max (Tree[p<<1]. MAX,TREE[P<<1|1].
MAX); TREE[P]. Min=min (Tree[p<<1]. MIN,TREE[P<<1|1].
Min); } else tree[p]. MAX=TREE[P].
MIN=NUM[X]; } int Getmax (int p,int x,int Y) {//Seek interval maximum if (x<=tree[p]. A&&TREE[P]. b<=y) {return tree[P].
Max;
} int lmax=0;
int rmax=0; if (X<=tree[p]. A+TREE[P]. B>>1&&Y>=TREE[P].
A) {Lmax=getmax (p<<1,x,y); } if (Y>tree[p]. A+TREE[P]. B>>1&&X<=TREE[P].
B) {Rmax=getmax (p<<1|1,x,y);
} return Max (Lmax,rmax); } int getmin (int p,int x,int Y) {//Find interval minimum if (x<=tree[p]. A&&TREE[P]. b<=y) {return tree[p].
Min;
} int lmin=inf;
int rmin=inf; if (x<= (tree[p). A+TREE[P]. B) >>1&&y>=tree[p].
A) {lmin=getmin (p<<1,x,y); } if (Y>tree[p]. A+TREE[P]. B>>1&&X<=TREE[P].
B) {rmin=getmin (p<<1|1,x,y);
} return min (lmin,rmin);
} int main () {int i,j,k;
For (N=read (), Q=read (), i=1;i<=n;i++) {num[i]=read ();
}maketree (1,1,n);
for (i=1;i<=q;i++) {int start=read (), End=read ();
Write (Getmax (1,start,end)-getmin (1,start,end));
Putchar (' \ n ');
} return 0; }