poj3264 balanced lineup【线段树】
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
Line 1: Two space-separated integers, N and Q.
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
Lines 1.. Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.
Sample Input
6 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
两棵树分别保存区间最大最小值即可
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define inf 1e18
using namespace std;
int q, n;
const int maxn = 50005;
int cow[maxn], talltree[maxn << 2], shorttree[maxn<<2];
void tall_pushup(int rt)//更新
{
talltree[rt] = max(talltree[rt << 1], talltree[rt << 1 | 1]);
}
void short_pushup(int rt)
{
shorttree[rt] = min(shorttree[rt<<1], shorttree[rt<<1|1]);
}
void tall_build(int l, int r, int rt)
{
if(l == r){
talltree[rt] = cow[l];
return;
}
int m = (l + r) >> 1;
tall_build(l, m, rt << 1);
tall_build(m + 1, r, rt << 1 | 1);
tall_pushup(rt);
}
void short_build(int l, int r, int rt)
{
if(l == r){
shorttree[rt] = cow[l];
return;
}
int m = (l + r) >> 1;
short_build(l, m, rt << 1);
short_build(m + 1, r, rt << 1 | 1);
short_pushup(rt);
}
/*void pushdown(int rt, int ln, int rn)
{
if(lazy[rt]){
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
tree[rt << 1] += lazy[rt] * ln;
tree[rt << 1 | 1] += lazy[rt] * rn;
lazy[rt] = 0;
}
}
void update(int L, int C, int l, int r, int rt)
{
if(l == r){
tree[rt] += C;
return;
}
int m = (l + r) >>1;
if(L <= m) update(L, C, l, m, rt << 1);
else update(L, C, m + 1, r, rt << 1 | 1);
pushup(rt);
}
void update(int L, int R, int C, int l, int r, int rt)
{
if(L <= l && r <= R){//本区间完全在操作区间内
tree[rt] += C * (r - l + 1);
lazy[rt] += C;
return;
}
int m = (l + r) >> 1;
pushdown(rt, m - l + 1, r - m);
if(L <= m) update(L, R, C, l, m, rt << 1);
if(R > m) update(L, R, C, m + 1, r, rt << 1 | 1);
pushup(rt);
}*/
int tall_query(int L, int R, int l, int r, int rt)
{
if(L <= l && r <= R){
return talltree[rt];
}
int m = (l + r) >> 1;
//pushdown(rt, m - l + 1, r - m);
int ans = 0;
if(L <= m) ans = max(ans, tall_query(L, R, l, m, rt << 1));
if(R > m) ans = max(ans, tall_query(L, R, m + 1, r, rt << 1 | 1));
return ans;
}
int short_query(int L, int R, int l, int r, int rt)
{
if(L <= l && r <= R){
return shorttree[rt];
}
int m = (l + r) >> 1;
//pushdown(rt, m - l + 1, r - m);
int ans = inf;
if(L <= m) ans = min(ans, short_query(L, R, l, m, rt << 1));
if(R > m) ans = min(ans, short_query(L, R, m + 1, r, rt << 1 | 1));
return ans;
}
int main()
{
while(scanf("%d%d", &n, &q) != EOF){
for(int i = 1; i <= n; i++){
scanf("%d", &cow[i]);
}
tall_build(1, n, 1);
short_build(1, n, 1);
while(q--){
int a, b;
scanf("%d%d", &a, &b);
cout<<tall_query(a, b, 1, n, 1) - short_query(a, b, 1, n, 1)<<endl;
}
}
return 0;
}
poj3264 balanced lineup【线段树】的更多相关文章
- POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值
题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...
- POJ3264 Balanced Lineup 线段树区间最大值 最小值
Q个数 问区间最大值-区间最小值 // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <i ...
- BZOJ-1699 Balanced Lineup 线段树区间最大差值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...
- [POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
- 【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34140 Accepted: 16044 ...
- bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树
1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 772 Solved: 560线 ...
- poj3264 Balanced Lineup(树状数组)
题目传送门 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 64655 Accepted: ...
- 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(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- 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 ...
随机推荐
- go和python互调
https://www.cnblogs.com/huangguifeng/p/8931837.html Python调用go编写的高性能模块 https://yq.aliyun.com/artic ...
- linux环境变量的概述
https://blog.csdn.net/u010533843/article/details/54986646 https://www.linuxidc.com/Linux/2017-08/146 ...
- Extjs表单验证小结
//放在onReady的function(){}中 Ext.QuickTips.init(); //为组件提供提示信息功能,form的主要提示信息就是客户端验证的错误信息. Ext.form.Fiel ...
- css counter的使用方法
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【Ubuntu】Windows 远程桌面连接ubuntu及xrdp的一些小问题(远程桌面闪退、连接失败、tab补全功能,无菜单栏,error - problem connecting )【转】
转:https://blog.csdn.net/u014447845/article/details/80291678 1.远程桌面闪退,shell可以用的问题:(1)需要在该用户目录创建一个.xse ...
- linux下查看当前目录属于哪个分区?
下班之前写哈今天用的一个新命令. df -h /opt/test
- Django 定义数据模型
如何定义数据模型: (1) 在 MVC 设计模式中,M 表示数据模型 ( Model ),负责业务对象与数据库的映射,我们可以通过应用的 models.py 来定义数据模型(2) Model 采用了 ...
- [Android] Nexus 7 二代连接 Windows 7
Android 设备的三大 USB 连接模式 MTP:Media Transfer Protocol - 媒体传输协议,Windows 下最常见的连接模式,是微软一种可以管理便携存储设备的协议.MTP ...
- lua元表(metatable)和元方法(metamethod)
(一) 元表概念: 引言:Lua中的每个值都有一套预定义的操作集合,如数字相加等.但无法将两个table相加,此时可通过元表修改一个值的行为,使其在面对一个非预定义的操作时执行一个指定操作. 访问机制 ...
- Java面试题全集
Java面试题全集(上) Java面试题全集(中) Java面试题全集(下) http://www.importnew.com/21445.html