[POJ] 3264 Balanced Lineup [线段树]
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 34306 | Accepted: 16137 | |
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
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
Sample Input
6 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
Source
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#include<stdlib.h>
#include<stdbool.h> #define rep(i,a,b) for(i=(a);i<=(b);i++)
#define clr(x,y) memset(x,y,sizeof(x))
#define sqr(x) (x*x)
#define LL long long const int INF=0xffffff0; struct {
int L,R;
int minV,maxV;
} tree[]; int i,j,n,q,minV,maxV; int min(int a, int b)
{
if(a<b) return a;
return b;
} int max(int a,int b)
{
if(a>b) return a;
return b;
} void BuildTree(int root,int L,int R)
{
tree[root].L=L;
tree[root].R=R;
tree[root].maxV=-INF;
tree[root].minV=INF; if(L!=R) {
BuildTree(*root,L,(L+R)/);
BuildTree(*root+,(L+R)/+,R);
} } void Insert(int root,int i,int v)
{
int mid; if(tree[root].L==tree[root].R) {
tree[root].maxV=tree[root].minV=v;
return ;
} tree[root].minV=min(tree[root].minV,v);
tree[root].maxV=max(tree[root].maxV,v); mid=(tree[root].L+tree[root].R)/;
if(i<=mid)
Insert(*root,i,v);
else
Insert(*root+,i,v); } void Query(int root,int s,int e)
{
int mid; if(tree[root].minV>=minV && tree[root].maxV<=maxV) return ;
if(tree[root].L==s && tree[root].R==e) {
minV=min(minV,tree[root].minV);
maxV=max(maxV,tree[root].maxV);
return ;
} mid=(tree[root].L+tree[root].R)/; if(e<=mid)
Query(*root,s,e);
else if(s>mid)
Query(*root+,s,e);
else {
Query(*root,s,mid);
Query(*root+,mid+,e);
} } int main()
{
int i,x,y; scanf("%d%d",&n,&q);
BuildTree(,,n);
rep(i,,n) {
scanf("%d",&x);
Insert(,i,x);
} while(q--) {
scanf("%d%d",&x,&y);
minV=INF;
maxV=-INF;
Query(,x,y);
printf("%d\n",maxV-minV);
} return ;
}
[POJ] 3264 Balanced Lineup [线段树]的更多相关文章
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- POJ 3264 Balanced Lineup 线段树RMQ
http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...
- POJ 3264 Balanced Lineup 线段树 第三题
Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...
- POJ 3264 Balanced Lineup (线段树)
Balanced Lineup For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the s ...
- POJ - 3264 Balanced Lineup 线段树解RMQ
这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...
- 【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34140 Accepted: 16044 ...
- Poj 3264 Balanced Lineup RMQ模板
题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...
- POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 53703 Accepted: 25237 ...
- POJ 3264 Balanced Lineup 【ST表 静态RMQ】
传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total S ...
随机推荐
- android:layout_weight属性的简单使用
效果: style.xml <style name="etStyle2"> <item name="android:layout_width" ...
- 枚举类:用enum关键字来定义一个枚举类
1)枚举类的两种定义方法 1>通过构造器 public enum Grade{ A("A", "90-100"),B("B",&quo ...
- 面试题 46 1+ 2+3+...+n
class Temp{ public: Temp(){ ++N; sum+=N; } static void Reset(){ N = ; sum = ; } static int getSum(){ ...
- 管理Undo数据
SQL> select sum(bytes),status from dba_undo_extents group by status; SUM(BYTES) STATUS ---------- ...
- ZZY的困惑
Description ZZY有很多爱好~~比如足球.电影.三国杀.A题,而他希望在这些爱好中能收获一些东西~~但是并不是所有爱好对所有目标都是起积极作用的..ZZY十分的困惑..于是列了下自己想获得 ...
- select与epoll分析
关于select与epoll的区别,网上的文章已是一大堆.不过别人的终究是别人的,总得自己去理解才更深刻.于是在阅读了大量的文章后,再装模作样的看下源码,写下了自己的一些理解. 在开始之前,要明白li ...
- Hadoop,HBase集群环境搭建的问题集锦(四)
21.Schema.xml和solrconfig.xml配置文件里參数说明: 參考资料:http://www.hipony.com/post-610.html 22.执行时报错: 23., /comm ...
- spring-data-mongodb必须了解的操作
http://docs.spring.io/spring-data/data-mongo/docs/1.0.0.M5/api/org/springframework/data/mongodb/core ...
- Ubuntu 12.04设置打开远程桌面登录1
teamviewer_linux.deb sudo dpkg --install teamviewer_linux.deb
- Layer中自定义属性的动画
转载自:http://blog.jobbole.com/69211/ 默认情况下,CALayer 及其子类的绝大部分标准属性都可以执行动画,无论是添加一个 CAAnimation 到 Layer(显式 ...