(最原始) 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;}
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
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 ≤A ≤ B ≤ N), representing the range of cows from A toB inclusive.
Output
Sample Input
6 31734251 54 62 2
Sample Output
630
Source
- (最原始) Balanced Lineup (P3264)
- 【POJ】【P3264】【Balanced Lineup】【题解】【ST表】
- POJ3264----区间最值Balanced Lineup
- Balanced Lineup
- Balanced Lineup
- Balanced Lineup
- Balanced Lineup
- Balanced Lineup
- Balanced Lineup
- Balanced Lineup
- Balanced Lineup
- poj 3264 Balanced Lineup(RMQ求区间最值)
- POJ 3264 Balanced Lineup 最基本的RMQ
- 士兵杀敌(三)&&Balanced Lineup RMQ区间最值
- HDU 3264 Balanced Lineup(线段树,最值查询)
- POJ - 3264 Balanced Lineup (RMQ问题求区间最值)
- POJ 3264 Balanced Lineup(RMQ求区间最值)
- poj Balanced Lineup (线段树区间最值+优化)
- 第八周上机项目一(2):用类的友元函数实现运算符的重载
- 数据挖掘十大经典算法
- 汇编学习笔记《一》
- 关于Visual studio2010 和 Microsoft SQL Server 2008 Management Studio Express 的安装
- Value '0000-00-00' can not be represented as java.sql.Timestamp
- (最原始) Balanced Lineup (P3264)
- adpatch OPTIONS
- SQL点滴19—T-SQL中的透视和逆透视
- java中的23中设计模式
- Pivot Tables Tutorial
- 深入了解java自动装箱和拆箱
- 数据挖掘十大经典算法(1) C4.5
- hdu 1853 Cyclic Tour (KM)
- 2-sat 讲解