题目链接:http://poj.org/problem?id=3264

一排牛按1~n标号记录重量,问每个区间最重的和最轻的差值。

线段树维护当前节点下属叶节点的两个最值,查询后作差即可。

 #include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; #define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
typedef pair<int, int> pii;
const int maxn = ;
int n, q, a, b;
pii node[maxn<<]; void pushUP(int rt) {
int ll = min(node[rt<<].first, node[rt<<|].first);
int rr = max(node[rt<<].second, node[rt<<|].second);
node[rt] = pii(ll, rr);
} void build(int l, int r, int rt) {
if(l == r) {
scanf("%d", &node[rt].first);
node[rt].second = node[rt].first;
return;
}
int m = (l + r) >> ;
build(lson);
build(rson);
pushUP(rt);
} int querymax(int L, int R, int l, int r, int rt) {
if(L <= l && r <= R) {
return node[rt].second;
}
int m = (l + r) >> ;
int ans = -;
if(L <= m) ans = max(ans, querymax(L, R, lson));
if(m < R) ans = max(ans, querymax(L, R, rson));
return ans;
} int querymin(int L, int R, int l, int r, int rt) {
if(L <= l && r <= R) {
return node[rt].first;
}
int m = (l + r) >> ;
int ans = 0x7f7f7f;
if(L <= m) ans = min(ans, querymin(L, R, lson));
if(m < R) ans = min(ans, querymin(L, R, rson));
return ans;
} int main() {
// freopen("in", "r", stdin);
while(~scanf("%d %d", &n, &q)) {
build(, n, );
while(q--) {
scanf("%d %d", &a, &b);
int ll = querymin(a, b, , n, );
int rr = querymax(a, b, , n, );
printf("%d\n", rr - ll);
}
}
return ;
}

[POJ3264]Balanced Lineup(线段树,区间最值差)的更多相关文章

  1. POJ3264 Balanced Lineup 线段树区间最大值 最小值

    Q个数 问区间最大值-区间最小值 // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <i ...

  2. BZOJ-1699 Balanced Lineup 线段树区间最大差值

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...

  3. 【POJ】3264 Balanced Lineup ——线段树 区间最值

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 ...

  4. POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值

    题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...

  5. POJ 3264 Balanced Lineup 【线段树/区间最值差】

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 62103 Accepted: 29005 Cas ...

  6. [POJ] 3264 Balanced Lineup [线段树]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

  7. bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树

    1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 772  Solved: 560线 ...

  8. poj3264 Balanced Lineup(树状数组)

    题目传送门 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 64655   Accepted: ...

  9. POJ 3264 Balanced Lineup 线段树 第三题

    Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...

  10. poj 3264 Balanced Lineup(线段树、RMQ)

    题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...

随机推荐

  1. Oracle 时间处理(加减)

    一. 类似SQL SERVER中DateAdd select sysdate,add_months(sysdate,12) from dual;        --加1年 select sysdate ...

  2. android中PreferenceScreen类的用法

    PreferenceScreen preference是偏好,首选的意思,PreferenceScreen个人翻译成 “偏好显示”,明白这个意思就好,就是说根据特点灵活的定义显示内容风格,一个屏幕可以 ...

  3. ssh 远程 centos 乱码

    今天,帮我们同学处理一下中文显示乱码的问题.这个是个国内Linux用户烦恼的问题,由于大部分的Linux发行版都是以英语为主体的,而且英文在通用性和稳定性上都比中文要好一些,各种奇怪的BUG也要少一点 ...

  4. Spinner学习

    我暂且把Spinner称作下拉选择框吧,先来看一下它的效果: 在layout文件中添加Spinner的代码如下: <Spinner android:id="@+id/planets_s ...

  5. js 阻止冒泡 兼容性方法

    function customstopPropagation(e){ var ev = e || window.event; if (ev.stopPropagation) { ev.stopProp ...

  6. Eclipse里面开发ExtJS程序

    首先在http://extjs.org.cn/download下载ext ,我下载的版本是ext-3.4.1.1-gpl,下载好以后解压. 然后在eclipse里面先建立一个web项目,建立好后再we ...

  7. .net web程序发布之后,出现编译错误

    .net web程序发布之后,在IIS上浏览的时候出现编译错误. CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temp ...

  8. Android /data/data/app_file/目录下面安装apk无权限问题

    当识别SDCard的时候 String filePath = null; String state = Environment.getExternalStorageState(); if (state ...

  9. 基于ant的jmeter自动化性能测试

    准备工作: 1.java的运行环境正常,及运行java -version.javac -version能正常输出java版本: 2.ant的运行环境正常,使用ant需要配置环境变量,编辑/etc/pr ...

  10. mq_send

    NAME mq_send - 将消息发送到消息队列 (REALTIME) SYNOPSIS #include <mqueue.h>int mq_send(mqd_t mqdes, cons ...