题目链接: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. ios containerViewController

    - (void)replaceViewController:(UIViewController *)existingViewController withViewController:(UIViewC ...

  2. 一个perfect 的解决 阴影拉伸的方法 shadow map strech

    因为在场景中做了,有的物体产生阴影比如人物,有的物体不产生阴影比如地面,这样在地面凹下去的地方,悬崖,池塘边,就会有阴影的拉伸. 实际上, 没办法上传图片.... L是光源 A 点(人物身上)产生阴影 ...

  3. Virtualbox网络设置和无UI启动

    因工作需要,在Macbook上安装Ubuntu 14.04.2虚拟机,需要ssh连接操作. 一番查找资料,实践后可以正常工作了,记录一些信息以备用 无UI启动虚拟机,可使用以下命令: VBoxMana ...

  4. ASP.NET中的事件处理

    一.ASP.NET中的事件主要支持3个主要的事件组:1.包含在asp.net生成页面时自动生成,我们使用这些事件建立页面(如page_load等)2.包含了用户与页面交互时发生的所有事件(这种最强大) ...

  5. 使用CSS3实现3D图片滑块效果

    使用 CSS3 的3D变换特性,我们可以通过让元素在三维空间中变换来实现一些新奇的效果. 这篇文章分享的这款 jQuery 立体图片滑块插件,利用了 3D transforms(变换)属性来实现多种不 ...

  6. HDOJ 1428 漫步校园

    漫步校园 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  7. POJ 2992 Divisors (求因子个数)

    题意:给n和k,求组合C(n,k)的因子个数. 这道题,若一开始先预处理出C[i][j]的大小,再按普通方法枚举2~sqrt(C[i][j])来求解对应的因子个数,会TLE.所以得用别的方法. 在说方 ...

  8. 转载:PHP,MySQL的安装与配置

    本文转自:http://www.cnblogs.com/janas/archive/2012/08/27/2659240.html 一.安装配置PHP 1.下载Php的版本zip包之后,解压缩到指定目 ...

  9. Android 源码 判断网络数据类型

    private final void updateDataNetType(int slotId) { int tempDataNetType; NetworkType tempDataNetType3 ...

  10. SqlServer 常用

    Sql的函数 newId() 获得guid: getDatatime() 获得当前时间: Row_number() 分页常用的函数. 比top 好用的函数select  Row_Number() ov ...