[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 ...
随机推荐
- InputStream转为byte数组
InputStream is = request.getSession().getServletContext().getResourceAsStream("/WEB-INF/swdjzbg ...
- MVC3路由设置访问后缀 html jsp
C# Code 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 publ ...
- 【转】USB协议架构及驱动架构
1. USB协议 1.1 USB主机系统 在USB主机系统中,通过根集线器与外部USB从机设备相连的处理芯片,称为USB主机控制器.USB主机控制器包含硬件.软件和固件一部分. 1.2 USB设备系统 ...
- 【转】iOS 解决ipv6问题
解决ipv6的方法有很多种,由于现在国内的网络运营商还在使用ipv4的网络环境,所以appstore应用不可能大范围去修改自己的服务器, 而且国内的云服务器几乎没有ipv6地址. 这里附上苹果开发平台 ...
- Java 常调用的Webservice接口的方法
WebService是基于Web的服务,WebService使用SOAP协议实现跨编程语言和跨操作系统平台,接收和响应外部系统的某种请求,从而实现远程调用.WebService采用HTTP协议传输数据 ...
- (转)Android创建桌面快捷方式两种方法
[IT168技术]Android在桌面上生成快捷方式有两种情况,一种是直接在桌面直接生成;一种是长按桌面,在弹出的快捷菜单中生成. 谈谈在桌面上直接生成.个人觉得这个比较爽快,既然都是快捷方式了干嘛还 ...
- 在Ubuntu上下载、编译和安装Android最新源代码
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6559955 看完了前面说的几本书之后,对Lin ...
- open(),close() 打开/关闭文件
Open open()是一个系统调用函数,用来打开或创建一个文件,通过不同的oflag选项实现不同功能. 使用时open()函数需要包含的头文件:<sys/types.h>,<sys ...
- Android 打造任意层级树形控件 考验你的数据结构和设计
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40212367,本文出自:[张鸿洋的博客] 1.概述 大家在项目中或多或少的可能会 ...
- sql 表名为关键字
user在sql server中时一个关键字,如上面说所的,有时候我们无意中将其作为表的名称,当我们在sql语句中要使用该名称时例如:select *from user这是会提示user附近有语法错误 ...