POJ 3264——Balanced Lineup(RMQ ,segment tree,树状数组)
2014-08-13 10:08
435 查看
Balanced Lineup
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
Sample Output
题目大意:
求区间 [ L , R ] 最大值与最小值的差
RMQ代码:
Segment Tree代码:
树状数组代码:
123132
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 34288 | Accepted: 16123 | |
Case Time Limit: 2000MS |
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
题目大意:
求区间 [ L , R ] 最大值与最小值的差
RMQ代码:
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<cmath> #define M 50000+10 using namespace std; int a[M],dp1[M][16],dp2[M][16]; int n,m; void RMQ_init() { for(int i=1;i<=n;++i) dp1[i][0]=dp2[i][0]=a[i]; for(int j=1;(1<<j)<=n;++j){ for(int i=1;i+(1<<j)-1<=n;++i){ dp1[i][j]=min(dp1[i][j-1],dp1[i+(1<<(j-1))][j-1]); dp2[i][j]=max(dp2[i][j-1],dp2[i+(1<<(j-1))][j-1]); } } } int RMQ(int l,int r) { int k=(int)(log(r-l+1.0)/log(2.0)); return max(dp2[l][k],dp2[r-(1<<k)+1][k])-min(dp1[l][k],dp1[r-(1<<k)+1][k]); } int main() { scanf("%d %d",&n,&m); for(int i=1;i<=n;++i){ scanf("%d",&a[i]); } RMQ_init(); int l,r; while(m--){ scanf("%d %d",&l,&r); printf("%d\n",RMQ(l,r)); } return 0; }
Segment Tree代码:
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #define M 50000+10 #define l(x) x<<1 #define r(x) x<<1|1 using namespace std; int a[M]; int n,m; int Max,Min; struct node { int l,r,minn,maxx,dif; }st[M<<2]; void push_up(int u) { st[u].minn=min( st[ l(u) ].minn, st[ r(u) ].minn ); st[u].maxx=max( st[ l(u) ].maxx, st[ r(u) ].maxx ); } void build(int u,int l,int r) { st[u].l=l,st[u].r=r;st[u].minn=st[u].maxx=0; if(l==r){ st[u].minn=st[u].maxx=a[st[u].l]; return ; } int m=(l+r)>>1; build(l(u),l,m); build(r(u),m+1,r); push_up(u); } /*void insert(int u,int p,int v) { if(st[u].l==st[u].r){ st[u].minn=st[u].maxx=v; return ; } int m=(st[u].l+st[u].r)>>1; if(p<=m) insert(l(u),p,v); else insert(r(u),p,v); push_up(u); }*/ void query(int u,int i,int j) { if(i<=st[u].l&&st[u].r<=j){ Max=max(Max,st[u].maxx); Min=min(Min,st[u].minn); return ; } int m=(st[u].l+st[u].r)>>1; if(i<=m) query(l(u),i,j); if(m<j) query(r(u),i,j); } int main() { scanf("%d %d",&n,&m); int x; for(int i=1;i<=n;++i){ scanf("%d",&a[i]); } build(1,1,n); while(m--){ int l,r; scanf("%d %d",&l,&r); Max=-1<<30,Min=1<<30; query(1,l,r); printf("%d\n",Max-Min); } return 0; }
树状数组代码:
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #define M 50000+10 using namespace std; int n,m,a[M]; int minv[M],maxv[M]; int lowbit(int x){return x&-x;} void update(int x,int v) { while(x<=n){ minv[x]=min(minv[x],v); maxv[x]=max(maxv[x],v); x+=lowbit(x); } } int query(int l,int r) { int Max=-1<<30,Min=1<<30; while(r>=l){ int low=lowbit(r); if(r-low>=l){ Max=max(Max,maxv[r]); Min=min(Min,minv[r]); r-=low; } else{ Max=max(Max,a[r]); Min=min(Min,a[r]); r--; } } return Max-Min; } int main() { scanf("%d %d",&n,&m); memset(minv,0x3f,sizeof minv); memset(maxv,-1,sizeof maxv); for(int i=1;i<=n;++i){ scanf("%d",&a[i]); update(i,a[i]); } while(m--){ int l,r; scanf("%d %d",&l,&r); printf("%d\n",query(l,r)); } return 0; }
123132
相关文章推荐
- poj 3264 Balanced Lineup【RMQ-ST查询区间最大最小值之差 +模板应用】
- poj 3264 Balanced Lineup(简单线段树 或 rmq)
- poj--3264 Balanced Lineup(裸的RMQ)
- poj 3264 Balanced Lineup(rmq vs 线段树)
- POJ 3264 Balanced Lineup,RMQ
- POJ 3264-Balanced Lineup-RMQ问题
- POJ 3264 Balanced Lineup (RMQ问题)
- POJ - 3264 Balanced Lineup 线段树解RMQ
- POJ 3264 Balanced Lineup(RMQ)
- poj 3264 Balanced Lineup【RMQ】
- POJ 3264 Balanced Lineup【RMQ-ST算法-区间最值】
- POJ 3264 Balanced Lineup (RMQ)
- POJ-3264 Balanced Lineup【RMQ】
- Poj 3264 Balanced Lineup【RMQ----ST算法】
- poj 3264 Balanced Lineup(RMQ求区间最值)
- POJ-3264 Balanced Lineup【RMQ】
- POJ 3264 Balanced Lineup 线段树 RMQ
- (简单) POJ 3264 Balanced Lineup,RMQ。
- POJ 3264 Balanced Lineup( 线段树&&RMQ )
- POJ 3264 Balanced Lineup 最基本的RMQ