POJ3264 Balanced Lineup 线段树 RMQ ST算法应用
来源:互联网 发布:股票收益率数据怎么找 编辑:程序博客网 时间:2024/05/30 23:01
Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 36813 Accepted: 17237
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.
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
操作:实现查询[a,b]中最大值与最小值的差值。
按理说ST要比线段树快,但是st跑下来用了3000多ms…
两个query操作一个实现查找最大值,一个实现查找最小值,最后两者一减就行了。
#include <cstdio>#include <algorithm>using namespace std;const int maxn = 200010;int n;int s[maxn];int dpmin[maxn][20];int dpmax[maxn][20];int minquery(int a,int b){ int k = 0; while(1<<(k+1) <= b-a+1) k++; return min(dpmin[a][k],dpmin[b-(1<<k)+1][k]);}int maxquery(int a,int b){ int k = 0; while(1<<(k+1) <= b-a+1) k++; return max(dpmax[a][k],dpmax[b-(1<<k)+1][k]);}int main(){ int q; scanf("%d%d",&n,&q); for(int i = 1 ; i <= n ; i ++) scanf("%d",&s[i]); for(int i = 1 ; i <= n ; i ++) dpmin[i][0] = s[i]; for(int j = 1 ; (1<<j) <= n ; j ++) { for(int i = 1 ; i+(2<<(j-1))-1 <= n ; i ++) { dpmin[i][j] = min(dpmin[i][j-1],dpmin[i+(1<<(j-1))][j-1]); } } for(int i = 1 ; i <= n ; i ++) dpmax[i][0] = s[i]; for(int j = 1 ; (1<<j) <= n ; j ++) { for(int i = 1 ; i+(2<<(j-1))-1 <= n ; i ++) { dpmax[i][j] = max(dpmax[i][j-1],dpmax[i+(1<<(j-1))][j-1]); } } while(q--) { int a,b; scanf("%d%d",&a,&b); printf("%d\n",maxquery(a,b)-minquery(a,b)); } return 0;}
线段树代码
#include <cstdio>#include <algorithm>using namespace std;const int maxn = 200010;int n,masegTree[maxn*4+10],misegTree[maxn*4+10];int s[maxn];void buildma(int node,int l,int r){ if(l == r) masegTree[node] = s[l]; else { buildma(node*2,l,(l+r)/2); buildma(node*2+1,(l+r)/2+1,r); masegTree[node] = max(masegTree[node*2],masegTree[node*2+1]); }}void buildmi(int node,int l,int r){ if(l == r) misegTree[node] = s[l]; else { buildmi(node*2,l,(l+r)/2); buildmi(node*2+1,(l+r)/2+1,r); misegTree[node] = min(misegTree[node*2],misegTree[node*2+1]); }}int maquery(int a,int b,int k,int l,int r){ if(l > b || r < a) return 1<<31; if(a <= l && r <= b) return masegTree[k]; return max(maquery(a,b,k*2,l,(l+r)/2),maquery(a,b,k*2+1,(l+r)/2+1,r));}int miquery(int a,int b,int k,int l,int r){ if(l > b || r < a) return 1<<31-1; if(a <= l && r <= b) return misegTree[k]; return min(miquery(a,b,k*2,l,(l+r)/2),miquery(a,b,k*2+1,(l+r)/2+1,r));}int main(){ int q; scanf("%d%d",&n,&q); for(int i = 1 ; i <= n ; i ++) scanf("%d",&s[i]); for(int i = 1 ; i < 4*maxn+10 ; i ++) { masegTree[i] = 1<<31; misegTree[i] = 1<<31-1; } buildma(1,1,n); buildmi(1,1,n); while(q--) { int a,b; scanf("%d%d",&a,&b); printf("%d\n",maquery(a,b,1,1,n)-miquery(a,b,1,1,n)); } return 0;}
- POJ3264 Balanced Lineup 线段树 RMQ ST算法应用
- poj3264 Balanced Lineup(RMQ +st)
- POJ3264 Balanced Lineup (RMQ & ST)
- POJ3264 Balanced Lineup 线段树|ST表
- POJ3264 Balanced Lineup RMQ 线段树
- POJ3264 Balanced Lineup 线段树||RMQ
- 【POJ】3264 - Balanced Lineup(RMQ - ST算法 || 线段树)
- POJ3264 Balanced Lineup(线段树应用)
- POJ3264--点更新的线段树--RMQ--Balanced Lineup
- poj3264——Balanced Lineup(ST算法及线段树操作)
- Balanced Lineup poj3264 线段树
- poj3264,Balanced Lineup,线段树
- poj3264 Balanced Lineup(线段树)
- poj3264 Balanced Lineup(线段树)
- POJ3264 Balanced Lineup(线段树)
- POJ3264 Balanced Lineup 【线段树】
- POJ3264-Balanced Lineup RMQ
- POJ3264 Balanced Lineup (RMQ)
- Visual C++ Tips: warning C4996: 'MBCS_Support_Deprecated_In_MFC': MBCS support in MFC is deprecated
- unity3d 加载和内存管理机制之二:进一步深入和细节
- 【逻辑回归笔记】牛顿法
- 基于贪心算法的几类区间覆盖问题
- linux gcc分步操作个人笔记
- POJ3264 Balanced Lineup 线段树 RMQ ST算法应用
- XSS攻击及防御
- 黑马程序员——java基础--IO流(篇二)
- C/C++学习:斐波那契数列
- 协议
- UVA - 10604 Chemical Reaction(6维dp+记忆化搜索)
- 软件工程师应该知道的9件事
- CentOS 7.0,启用iptables防火墙
- 淘宝首页 图片滑动切换效果 基于jQuery的animate方法实现