poj 3264RMQ问题(线段树,ST算法)
来源:互联网 发布:防伪底纹软件 编辑:程序博客网 时间:2024/05/20 06:08
Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 34766 Accepted: 16368Case 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 31734251 54 62 2
Sample Output
630
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<stack>#include<queue>#include<set>#include<map>using namespace std;typedef long long LL;const int MAX=0xfffffff;int n;const int mx=50050;int num[mx];class node{public: int l,r,maxv,minv;}tree[mx*4];void pushup(int v){ tree[v].maxv=max(tree[v*2].maxv,tree[v*2+1].maxv); tree[v].minv=min(tree[v<<1].minv,tree[v<<1|1].minv);}void build(int v,int l,int r){ tree[v].l=l,tree[v].r=r; if(l==r) { tree[v].maxv=num[l]; tree[v].minv=num[l]; return ; } int mid=(l+r)/2; build(v*2,l,mid),build(v*2+1,mid+1,r); pushup(v);}int query1(int v,int l,int r){ if(l==tree[v].l&&tree[v].r==r) return tree[v].maxv; else { int mid=(tree[v].l+tree[v].r)/2; if(r<=mid) return query1(v*2,l,r); //如果不添加等号,程序无法运行 else if(l>mid) return query1(v*2+1,l,r); //如果加上=,就发生结果变化了 else return max(query1(v*2,l,mid),query1(v*2+1,mid+1,r)); }}int query2(int v,int l,int r){ if(l==tree[v].l&&tree[v].r==r) return tree[v].minv; else { int mid=(tree[v].l+tree[v].r)/2; if(r<=mid) return query2(v*2,l,r); //如果不添加等号,程序无法运行 else if(l>mid) return query2(v*2+1,l,r); //如果加上=,就发生结果变化了 else return min(query2(v*2,l,mid),query2(v*2+1,mid+1,r)); }}int main( ){ //freopen("1.txt","r",stdin); int q; while(scanf("%d %d",&n,&q)!=EOF) { for(int i=1;i<=n;i++) scanf("%d",&num[i]); build(1,1,n); while(q--) { int a,b; scanf("%d %d",&a,&b); int ma=query1(1,a,b); int mi=query2(1,a,b); printf("%d\n",ma-mi); } } return 0;}
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<stack>#include<queue>#include<set>#include<map>#include<cmath>using namespace std;typedef long long LL;const int MAX=0xfffffff;const int mx=50050;int ma[mx][20], mi[mx][20];int n;void RMQ_ST(){ int lg=floor(log10(double(n))/log10(double(2))); for(int j=1;j<=lg;j++) for(int i=1;i<=n+1-(1<<j);i++) { ma[i][j] = max(ma[i][j-1], ma[i+(1<<(j-1))][j-1]); mi[i][j] = min(mi[i][j-1], mi[i+(1<<(j-1))][j-1]); }}int main( ){ int q; while(scanf("%d %d",&n,&q)!=EOF) { for(int i=1;i<=n;i++) { int a; scanf("%d",&a); ma[i][0]=mi[i][0]=a; } RMQ_ST(); while(q--) { int a,b; scanf("%d %d",&a,&b); if(a>b) a=a^b^(b=a); int lg=floor(log10(double(b-a+1))/log10(double(2))); printf("%d\n",max(ma[a][lg],ma[b-(1<<lg)+1][lg])-min(mi[a][lg],mi[b-(1<<lg)+1][lg])); } }}
0 0
- poj 3264RMQ问题(线段树,ST算法)
- 【POJ】3264 - Balanced Lineup(RMQ - ST算法 || 线段树)
- POJ-3264 RMQ 线段树与ST
- poj 3264 RMQ问题 ST算法
- POJ 3264 RMQ问题 ST算法
- RMQ问题 (st+线段树)
- POJ 3264 RMQ--ST 算法
- POJ3264 RMQ问题 裸 线段树 OR ST算法
- 线段树 ST算法 RMQ poj 3264 Balanced Lineup 解题报告
- Balanced Lineup-POJ - 3264-RMQ线段树/st表
- POJ 3264 解题报告 RMQ 问题 ST算法
- POJ 3264 Balanced Lineup RMQ问题 ST算法
- POJ 3264-Balanced Lineup(RMQ-ST算法)
- Poj 3264 Balanced Lineup【RMQ----ST算法】
- RMQ问题ST算法
- RMQ问题 ST算法
- poj 3264 RMQ问题 zkw线段树
- RMQ(ST算法)
- 2.4G wifi 的频道/信道 20M 40M的概念,区别
- 有关任务和日程的20个建议,大大提升你的工作效率
- 有沃更精彩,沃课堂理想的移动学习平台
- android -- 蓝牙 bluetooth (二) 打开蓝牙
- Java Filter过滤机制详解
- poj 3264RMQ问题(线段树,ST算法)
- Linux程序设计:调试
- fstat、stat和lstat 区别
- auto_ptr智能指针的缺点
- [小技巧] 使用df按不同SIZE的单位查看磁盘容量
- 二叉树计算的一些规律
- Use Case新手上路
- C语言枚举类型
- maven 教程一 入门