[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手机端保存xml数据
1.前面写的这个不能继续插入数据,今天补上,当文件不存在的时候就创建,存在就直接往里面添加数据. 2.代码如下: <pre name="code" class="j ...
- 学习javascript基础知识系列第二节 - this用法
通过一段代码学习javascript基础知识系列 第二节 - this用法 this是面向对象语言中的一个重要概念,在JAVA,C#等大型语言中,this固定指向运行时的当前对象.但是在javascr ...
- 一个资深java面试官的“面试心得”
在公司当技术面试官几年间,从应届生到工作十几年的应聘者都遇到过.先表达一下我自己对面试的观点: 1.笔试.面试去评价一个人肯定是不够准确的,了解一个人最准确的方式就是“路遥知马力,日久见人心”.通过一 ...
- joomla学习网站资料
http://joomlaol.com/joomla-chinese/joomla-study/340-joomla-skills.html joomla的强大相信学过和用过joomla的童鞋都知道吧 ...
- 算法导论(第三版)Exercises2.1(插入排序、线性查找、N位大数相加)
关于练习程序的说明参见置顶的那篇. 2.1-1: 31 41 59 26 41 58 31 41 59 26 41 58 31 41 59 26 41 58 26 31 41 59 41 58 26 ...
- sicily 1099 Packing Passengers
/**** 题意: 求x,y 满足 x*pa+y*pb=n 同时使得 p = x*ca+y*cb的值最小,若有多种可能,则选择最大x值的组合** 分析: x*pa+y*pb=n 可以用 线性同 ...
- table表格边框样式
; border-left:1px solid #aaa; border-top:1px solid #aaa; } td{border-right:1px solid #aaa; border-bo ...
- python高级编程:有用的设计模式2
# -*- coding: utf-8 -*- __author__ = 'Administrator' #python高级编程:有用的设计模式 #代理 """ 代理对一 ...
- [转]myeclipse 生成JAR包并引入第三方包
myeclipse 生成JAR包并引入第三方包 我用的是myeclipse8.0 首先用myeclipse生成JAR 一.生成JAR包 1.点选项目右键—>Export 2.Java—>J ...
- [Angular 2] WebStorm - Managing Imports
Some tips for import libaray by using webstorm: // Alt + Enter --> Auto Import // Ctrl + Alt + o ...