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 ...
随机推荐
- Visual Studio的SDK配置
Visual Studio的SDK Visual Studio 6.0自带的SDK是1998年的,目录为C:\Program Files\Microsoft Visual Studio\VC98\,这 ...
- 利用SQL语句实现分页
1.概述 在网页中如果显示的数据太多就会占据过多的页面,而且显示速度也会很慢.为了控制每次在页面上显示数据的数量,就可以利用分页来显示数据. 2.技术要点 在SQL Server中要实现SQL分页,需 ...
- Jquery事件的连接
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 使用ffmpeg 对视频截图,和视频转换格式
//执行CMD命令方法 public static void CmdProcess(string command)//调用CMD { //实例化一个进程类 ...
- SMART原则_百度百科
SMART原则_百度百科 SMART原则
- DatePicker的使用
activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android ...
- Object.Instantiate 实例
static function Instantiate (original : Object, position : Vector3, rotation : Quaternion) : Object ...
- 基于Servlet、JSP、JDBC、MySQL的一个简单的用户注冊模块(附完整源代码)
近期看老罗视频,做了一个简单的用户注冊系统.用户通过网页(JSP)输入用户名.真名和password,Servlet接收后通过JDBC将信息保存到MySQL中.尽管是个简单的不能再简单的东西,但麻雀虽 ...
- selenium + firefox/chrome/phantomjs登陆之模拟点击
登陆之模拟点击 工具:python/java + selenium + firefox/chrome/phantomjs (1)windows开发环境搭建 默认已经安装好了firefox 安装pip ...
- hdu2639(背包求第k优解)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2639 题意:给出一行价值,一行体积,让你在v体积的范围内找出第k大的值 分析:dp[i][j][k]表 ...