(最原始) Balanced Lineup (P3264)

来源:互联网 发布:淘宝网店铺装修实战 编辑:程序博客网 时间:2024/05/16 10:02

题意:给出一段数列,询问从i到j之间的最大值与最小值之差。


方法:就是用线段树分别存每段之间的最大值、最小值。然后作差即可。


注意:由于有些时间没有写线段树了,现在出现了一些问题。终于还是以比较低的时间过了

1、注意l  ,  r 与 中间值之间的查找关系。



#include<iostream>using namespace std;#define N 50010#define inf 1e9int n,q;struct my{int l,r,num;int m;} big[N*3],small[N*3];void init(my go[],int cur,int l,int r,int v){go[cur].l=l;go[cur].r=r;go[cur].m=(l+r)>>1;go[cur].num=v;if (l<r){init(go,cur<<1,l,go[cur].m,v);init(go,cur<<1|1,go[cur].m+1,r,v);}}void update_big(int cur,int l,int r,int v){big[cur].num=max(big[cur].num,v);if (big[cur].l==big[cur].r)return ;if (big[cur].m>=l)update_big(cur<<1,l,l,v);elseupdate_big(cur<<1|1,l,l,v);}void update_small(int cur,int l,int r,int v){small[cur].num=min(small[cur].num,v);if (small[cur].l==small[cur].r)return ;if (l<=small[cur].m)update_small(cur<<1,l,l,v);elseupdate_small(cur<<1|1,l,l,v);}int find_big(int cur,int l,int r){//cout<<cur<<' '<<big[cur].l<<' '<<big[cur].r<<' '<<big[cur].num<<' '<<l<<' '<<r<<endl;if (big[cur].l>=l && big[cur].r<=r)return big[cur].num;if (big[cur].l>r || big[cur].r<l)return 0;if (big[cur].m<l)return find_big(cur<<1|1,l,r);if (big[cur].m>=r)return find_big(cur<<1,l,r);return max(find_big(cur<<1,l,big[cur].m),find_big(cur<<1|1,big[cur].m+1,r));}int find_small(int cur,int l,int r){if (small[cur].l>=l && small[cur].r<=r)return small[cur].num;if (small[cur].r<l || small[cur].l>r)return inf;if (small[cur].m>=r)return find_small(cur<<1,l,r);if (small[cur].m<l)return find_small(cur<<1|1,l,r);return min(find_small(cur<<1,l,small[cur].m),find_small(cur<<1|1,small[cur].m+1,r));}int main(){freopen("in.txt","r",stdin);int i,j,k;cin>>n>>q;init(big,1,1,n,0);init(small,1,1,n,inf);for (i=1;i<=n;i++){scanf("%d",&k);update_big(1,i,i,k);update_small(1,i,i,k);}while (q--){scanf("%d%d",&i,&j);//cout<<find_big(1,i,j)<<' '<<find_small(1,i,j)<<endl;printf("%d\n",find_big(1,i,j)-find_small(1,i,j));}return 0;}





Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 26810 Accepted: 12570Case 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 andQ.
Lines 2..N+1: Line i+1 contains a single integer that is the height of cowi
Lines N+2..N+Q+1: Two integers A and B (1 ≤ABN), representing the range of cows from A toB 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 31734251 54 62 2

Sample Output

630

Source

USACO 2007 January Silver


原创粉丝点击