G - Balanced Lineup POJ 3264 (线段树+区间查询无更新)
来源:互联网 发布:驱动程序软件安装设置 编辑:程序博客网 时间:2024/06/02 20:52
G - Balanced Lineup
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 3264
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
//#include<bits/stdc++.h>#include<iostream>#include<cstring>#include<string>#include<cstdlib>#include<cstdio>#include<map>#include<algorithm>using namespace std;const int maxn=50011;const int inf=999999999;#define lson (rt<<1),L,M#define rson (rt<<1|1),M+1,R#define M ((L+R)>>1)#define For(i,n) for(int i=0;i<(n);i++)template<class T>inline T read(T&x){ char c; while((c=getchar())<=32); bool ok=false; if(c=='-')ok=true,c=getchar(); for(x=0; c>32; c=getchar()) x=x*10+c-'0'; if(ok)x=-x; return x;}template<class T> inline void read_(T&x,T&y){ read(x); read(y);}template<class T> inline void read__(T&x,T&y,T&z){ read(x); read(y); read(z);}template<class T> inline void write(T x){ if(x<0)putchar('-'),x=-x; if(x<10)putchar(x+'0'); else write(x/10),putchar(x%10+'0');}template<class T>inline void writeln(T x){ write(x); putchar('\n');}//-------IO template------typedef long long LL;struct node{ int maxv,minv;}p[maxn<<3];void build(int rt,int L,int R){ p[rt].maxv=-inf; p[rt].minv=inf; if(L==R) { int a; read(a); p[rt].maxv=p[rt].minv=a; return ; } build(lson); build(rson); p[rt].maxv=max(p[rt<<1].maxv,p[rt<<1|1].maxv); p[rt].minv=min(p[rt<<1].minv,p[rt<<1|1].minv);}int a,b;void query(int rt,int L,int R,int x,int y){ if(L==x&&y==R) { a=max(a,p[rt].maxv); b=min(b,p[rt].minv); return ; } if(y<=M) query(lson,x,y); else if(x>M) query(rson,x,y); else { query(lson,x,M); query(rson,M+1,y); }}int main(){ int n,m; //freopen("in.txt","r",stdin); while(~scanf("%d%d",&n,&m)) { build(1,1,n); while(m--) { int x,y; read_(x,y); a=-inf;b=inf; query(1,1,n,x,y); writeln(a-b); } } return 0;}
0 0
- G - Balanced Lineup POJ 3264 (线段树+区间查询无更新)
- POJ 3264 Balanced Lineup (线段树单点更新 区间查询)
- POJ 3264 Balanced Lineup【简单线段树,区间最值,无更新】
- POJ 3264 Balanced Lineup 线段树的构建+区间查询
- POJ 3264 Balanced Lineup(线段树区间查询)
- POJ 3264-Balanced Lineup(线段树:单点更新,区间查询)
- Balanced Lineup (POJ_3264) 线段树+区间查询
- POJ 3264 Balanced Lineup 线段树 单点更新 求区间最值
- poj 3264 Balanced Lineup(线段树单点修改区间查询)
- 【POJ 3264】【RMQ 或者线段树】Balanced Lineup 【查询区间内最大最小值的差】
- POJ 3264-Balanced Lineup详解(线段树区间求值)
- poj 3264 Balanced Lineup线段树区间最值差
- POJ 3264 Balanced Lineup 求线段树区间最值
- poj 3264 Balanced Lineup(线段树 区间最值)
- 查询线段树 Balanced Lineup POJ
- poj-3264-Balanced Lineup-线段树-区域查询
- poj Balanced Lineup (线段树区间最值+优化)
- poj 3264 Balanced Lineup rmq/线段树
- 第26课时,实践2,成绩等级
- HDU 3905 Sleeping dp
- HDU 1159 - Common Subsequence [最长公共子序列]
- uva 11992 Fast Matrix Operations (线段树区间更新)
- 记一次Android开发GIF功能踩过的坑
- G - Balanced Lineup POJ 3264 (线段树+区间查询无更新)
- java中同步(synchronized)详解
- Form表单的验证
- ramdisk 和 ramfs 文件系统 (initrd 和 initramfs )
- 《管理的常识》第四章<什么是领导>读书笔记
- 第一篇博客
- csu 1562: Fun House (DFS)
- Linux网络编程学习之---简单局域网FTP文件传输服务器
- OC学习之道:OC对象,字典,集合的使用方法NSDictionary,NSMutableDictionary, NSSet,NSMutableSet,NSCountedSet