POJ 3264 Balanced Lineup
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 55414 | Accepted: 25969 | |
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.
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
题意:给定x,y区间
求区间内极差
#include <ctype.h> #include <cstdio> void read(int &x) { x=0;bool f=0; register char ch=getchar(); for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=1; for(; isdigit(ch);ch=getchar()) x=(x<<3)+(x<<1)+ch-'0'; x=f?(~x)+1:x; } int n,m; int max(int a,int b) {return a>b?a:b;} int min(int a,int b) {return a>b?b:a;} struct Typetree { int l,r,dis,Max,Min; Typetree *ch[2]; void pushup() { Max=max(ch[0]->Max,ch[1]->Max); Min=min(ch[0]->Min,ch[1]->Min); } void build(int l,int r,int *a) { if(l==r) { dis=Max=Min=a[l]; return; } int mid=(l+r)>>1; (ch[0]=new Typetree)->build(l,mid,a); (ch[1]=new Typetree)->build(mid+1,r,a); pushup(); } int Query_Max(int l,int r,int L,int R) { if(L<=l&&r<=R) return Max; int mid=(l+r)>>1,ret=-2; if(L<=mid) ret=max(ch[0]->Query_Max(l,mid,L,R),ret); if(R>mid) ret=max(ch[1]->Query_Max(mid+1,r,L,R),ret); return ret; } int Query_Min(int l,int r,int L,int R) { if(L<=l&&r<=R) return Min; int mid=(l+r)>>1,ret=0x7fffffff; if(L<=mid) ret=min(ch[0]->Query_Min(l,mid,L,R),ret); if(R>mid) ret=min(ch[1]->Query_Min(mid+1,r,L,R),ret); return ret; } }*root=new Typetree; int N,Q,data[50005]; int main() { read(N);read(Q); for(int i=1;i<=N;i++) read(data[i]); root->build(1,N,data); for(int x,y;Q--;) { read(x);read(y); printf("%d\n",root->Query_Max(1,N,x,y)-root->Query_Min(1,N,x,y)); } return 0; }
我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。