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 ...
随机推荐
- [scala] scala 集合(⑧)
1.List 基础操作 2.Set 基础操作 3. TreeSet 排序Set 4. 拉链操作 5. 流 import scala.collection.immutable.TreeSet impor ...
- MTK 永不熄屏
步骤一: 源码/frameworks/base/packages/SettingsProvider/res/values/defaults.xml 修改<integername=</int ...
- Linux+Redis实战教程_day02_3、redis数据类型_4、String命令_5、hash命令_6、java操作redis数据库技术
3. redis数据类型[重点] redis 使用的是键值对保存数据.(map) key:全部都是字符串 value:有五种数据类型 Key名:自定义,key名不要过长,否则影响使用效率 Key名不要 ...
- SpringMVC -- 梗概--源码--壹--数据传递
附:实体类 Class : User package com.c61.entity; import java.text.SimpleDateFormat; import java.util.Date; ...
- ios开发之--调试方法
概述 基本操作 全局断点 条件断点 开启僵尸对象 LLDB命令 概述 在开发项目的工程中,肯定会遇到各种各样的bug,且大多数的bug都和自己有关:那么在和bug斗智斗勇的过程中,如果能快速准确的一击 ...
- linux下中文文件名乱码解决
在windows上使用ftp上传文件到Linux上,中文名称在Linux系统中显示为乱码.虽然将Linux的env设置了LANG=en_US.UTF-8,并且本地的Shell客户端编码也设置成UTF- ...
- IIS日志清理(VBS版,JS版)
IIS默认日志记录在C:\WINDOWS\system32\LogFiles,时间一长,特别是子站点多的服务器,一个稍微有流量的网站,其日志每天可以达到上百兆,这些文件日积月累会严重的占用服务器磁盘空 ...
- Selenium 查找节点
Selenium 可以驱动浏览器完成各种操作,比如填充表单.模拟点击等.比如,我们想要完成向某个输入框输入文字的操作,总需要知道这个输入框在哪里吧?而 Selenium 提供了一系列查找节点的方法,我 ...
- flask路由中增加正则表达式
#coding=utf-8 from flask import Flask from werkzeug.routing import BaseConverter class RegexConverte ...
- 使用CreateProcess创建新的process 并返回process运行结束返回值
转自:http://blog.csdn.net/zgl7903/article/details/5975284 转载这篇主要是记住:获得create的新进程运行结束时的返回值的方法 如下: #in ...