POJ 3264-Balanced Lineup(段树:单点更新,间隔查询)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 34522 | Accepted: 16224 | |
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 <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#define LL long long
using namespace std;
const int INF=1<<27;
const int maxn=200010;
LL minn[maxn],maxx[maxn];
void update(LL root,LL l,LL r,LL p,LL v)//单点更新
{
if(l==r) maxx[root]=v;minn[root]=v;
if(l<r)
{
LL mid=(l+r)/2;
if(p<=mid) update(root*2,l,mid,p,v);
else update(root*2+1,mid+1,r,p,v);
maxx[root]=max(maxx[root*2],maxx[root*2+1]);
minn[root]=min(minn[root*2],minn[root*2+1]);
}
}
LL query_min(LL root,LL l,LL r,LL ql,LL qr)
{
LL mid=(l+r)/2,ans=INF;
if(ql<=l&&qr>=r) return minn[root];
if(ql<=mid) ans=min(ans,query_min(root*2,l,mid,ql,qr));
if(qr>mid) ans=min(ans,query_min(root*2+1,mid+1,r,ql,qr));
return ans;
}
LL query_max(LL root,LL l,LL r,LL ql,LL qr)
{
LL mid=(l+r)/2,ans=-INF;
if(ql<=l&&qr>=r) return maxx[root];
if(ql<=mid) ans=max(ans,query_max(root*2,l,mid,ql,qr));
if(qr>mid) ans=max(ans,query_max(root*2+1,mid+1,r,ql,qr));
return ans;
}
int main()
{
int N,Q,i,v;
while(~scanf("%lld%lld",&N,&Q))
{
for(i=1;i<=N;i++)
{
scanf("%lld",&v);
update(1,1,N,i,v);
}
while(Q--)
{
int ql,qr;
scanf("%lld%lld",&ql,&qr);
printf("%lld\n",query_max(1,1,N,ql,qr)-query_min(1,1,N,ql,qr));
}
}
return 0;
}
POJ 3264-Balanced Lineup(段树:单点更新,间隔查询)的更多相关文章
- 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 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
- 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)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- POJ 3264 Balanced Lineup 线段树RMQ
http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...
- POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值
题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...
- POJ - 3264 Balanced Lineup 线段树解RMQ
这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...
- POJ - 2828 Buy Tickets (段树单点更新)
Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...
- Poj 3264 Balanced Lineup RMQ模板
题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...
- POJ 3264 Balanced Lineup 【ST表 静态RMQ】
传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total S ...
随机推荐
- JAVA中IO和NIO的详解分析,内容来自网络和自己总结
用一个例子来阐释: 一辆客车上有10个乘客,他们的目的地各不相同,当没有售票员的时候,司机就需要不断的询问每一站是否有乘客需要下车,需要则停下,不需要则继续开车,这种就是阻塞的方式. 当有售票员的时候 ...
- J2SE学习小结
开始接触Java的学习,Java 2 Standard Edition为Java2平台的标准版,其包括了构成Java语言核心的类,此番学习算是学习了Java体系中的基础部分. 学习框架大致整理如下: ...
- 每次调用fork()函数之后,父线程和创建出的子线程都是从fork()后开始执行
Linux下多少个"-"将被打印: 1 2 3 4 5 6 7 8 int main(void){ int i; for(i=0;i<4;i++){ fork() ...
- Lisp: Common Lisp, Racket, Clojure, Emacs Lisp - Hyperpolyglot
Lisp: Common Lisp, Racket, Clojure, Emacs Lisp - Hyperpolyglot Lisp: Common Lisp, Racket, Clojure, E ...
- php 如何在有限的内存中读取大文件
突然遇到了一个要读取超过80M文件的需求,很悲剧的,不管是file_get_content还是file什么的,都会将读取的文件一次性加载到内存中. 正常情况下,我们可以使用fseek来读取,好处就是不 ...
- web desktop在线演示
http://mydesk.sinaapp.com 基于extjs的web desktop应用框架. 1.跨浏览器 2.动态载入所需css,js文件 3.权限管理 4.支持多语种 5.支持asp,js ...
- 随想录(关于ucore)
[ 声明:版权全部,欢迎转载.请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 之前用过一段时间skyeye,也对开发skyeye的陈渝有一些了解.近期在github上闲逛的 ...
- 以对象管理资源——C++智能指针auto_ptr简介
auto_ptr是C++标准库提供的类模板,它可以帮助程序员自动管理用new表达式动态分配的单个对象.auto_ptr对象被初始化为指向由new表达式创建的对象,当auto_ptr对象的生命期结束时, ...
- 14.4.2 Configuring InnoDB for Read-Only Operation 配置InnoDB 永于只读操作:
14.4.2 Configuring InnoDB for Read-Only Operation 配置InnoDB 永于只读操作: 你可以查询InnoDB 表 MySQL 数据目录是在只读介质里,通 ...
- stm32 ARM中的RO、RW和ZI DATA
一直以来对于ARM体系中所描述的RO,RW和ZI数据存在似是而非的理解,这段时间对其仔细了解了一番,发现了一些规律,理解了一些以前书本上有的但是不理解的东西,我想应该有不少人也有和我同样的困惑,因此将 ...