题目链接: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. GIS数据格式topojson

    Topojson源自于GeoJson,是D3中描述地理数据的格式,D3的作者觉得GeoJson太繁琐.同样的数据,TopoJson是GeoJson的1/5. 这里有一个转换TopoJson,GeoJs ...

  2. 迁移到MariaDB galera

    迁移到MariaDB galera [已注销] [已注销] -- :: [安装] ====== https://downloads.mariadb.org/mariadb/repositories/ ...

  3. 如何用 Parse 和 Swift 搭建一个像 Instagram 那样的应用?

    [编者按]本篇文章作者是Reinder de Vries,既是一名企业家,也是优秀的程序员,发表多篇应用程序的博客.本篇文章中,作者主要介绍了如何基于Parse特点,打造一款类似Instagram的应 ...

  4. Java异常类和自定义异常类

    自定义异常类: public class ExtendsException extends Exception { private static final long serialVersionUID ...

  5. java集合之ArrayList的实现原理

    1. ArrayList概述: ArrayList是List接口的可变数组的实现.实现了所有可选列表操作,并允许包括 null 在内的所有元素.除了实现 List 接口外,此类还提供一些方法来操作内部 ...

  6. BZOJ 1046: [HAOI2007]上升序列 LIS -dp

    1046: [HAOI2007]上升序列 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3438  Solved: 1171[Submit][Stat ...

  7. 【转】terminal 快捷键

    转自:http://www.jb51.net/os/Ubuntu/141723.html 1.gnome-terminal快捷键设置方法: 系统 —> 首选项 ->键盘快捷键 -> ...

  8. ffmpeg转码时对编码率和固定码率的处理

    http://www.rosoo.net/a/201107/14663.html 一般fps在代码里这样表示 Fps = den/num 如果den = 15,num=1,则fps = 15. 如果帧 ...

  9. 学了C语言,如何利用CURL写一个下载程序?—用nmake编译CURL并安装

    在这一系列的前一篇文章学了C语言,如何为下载狂人写一个磁盘剩余容量监控程序?中,我们为下载狂人写了一个程序来监视磁盘的剩余容量,防止下载的东西撑爆了硬盘.可是,这两天,他又抱怨他的下载程序不好用,让我 ...

  10. ios开发解决遍历删除数组对象报错

    //            删除对应数据 //            for (OrderModel *order in self.OrderList) { // //                ...