description

查询区间最大和最小

题解

线段树

愉悦身心啊

代码

 #include<cstring>
#include<cstdio>
#include<algorithm>
#define rd read()
#define lson nd << 1
#define rson nd << 1 | 1
using namespace std; const int N = 1e5;
const int inf = ~0U >> ; int MAX[N << ], MIN[N << ], a[N], q, n; int read() {
int X = , p = ; char c = getchar();
for(; c > '' || c < ''; c = getchar()) if(c == '-') p = -;
for(; c >= '' && c <= '';c = getchar()) X = X * + c - '';
return X * p;
} void print(int x) {
if(x < ) putchar('-'), x = -x;
if(x >= ) print(x / );
putchar(x % + '');
} void update(int nd) {
MIN[nd] = min(MIN[lson], MIN[rson]);
MAX[nd] = max(MAX[lson], MAX[rson]);
} void build(int l, int r, int nd) {
if(l == r) {
MIN[nd] = MAX[nd] = a[l];
return;
}
int mid = (l + r) >> ;
build(l, mid, lson);
build(mid + , r, rson);
update(nd);
} int query_max(int L, int R, int l, int r, int nd) {
if(L <= l && r <= R) return MAX[nd];
int mid = (l + r) >> , tmp = ;
if(L <= mid) tmp = max(tmp, query_max(L, R, l, mid, lson));
if(mid < R) tmp = max(tmp, query_max(L, R, mid + , r, rson));
return tmp;
} int query_min(int L, int R, int l, int r, int nd) {
if(L <= l && r <= R) return MIN[nd];
int mid = (l + r) >> , tmp = inf;
if(L <= mid) tmp = min(tmp, query_min(L, R, l, mid, lson));
if(mid < R) tmp = min(tmp, query_min(L, R, mid + , r, rson));
return tmp;
} int main()
{
n = rd; q = rd;
for(int i = ; i <= n; ++i) a[i] = rd;
build(, n, );
for(int i = ; i <= q; ++i) {
int l = rd, r = rd;
int mx = query_max(l, r, , n, ), mn = query_min(l, r, , n, );
print(mx - mn);
putchar('\n');
}
}

BZOJ1699: [Usaco2007 Jan]Balanced Lineup排队 - 线段树的更多相关文章

  1. BZOJ 1699 [Usaco2007 Jan]Balanced Lineup排队 线段树

    题意:链接 方法:线段树 解析: 题意即题解. 多次询问区间最大值与最小值的差.显然直接上线段树或者rmq维护区间最值就可以. 代码: #include <cstdio> #include ...

  2. bzoj1699[Usaco2007 Jan]Balanced Lineup排队*&bzoj1636[Usaco2007 Jan]Balanced Lineup*

    bzoj1699[Usaco2007 Jan]Balanced Lineup排队 bzoj1636[Usaco2007 Jan]Balanced Lineup 题意: 询问区间最大值减区间最小值的差. ...

  3. BZOJ1699: [Usaco2007 Jan]Balanced Lineup排队

    1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 933  Solved: 56 ...

  4. BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队

    1699: [Usaco2007 Jan]Balanced Lineup排队 Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. ...

  5. BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队( RMQ )

    RMQ.. ------------------------------------------------------------------------------- #include<cs ...

  6. bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块

    1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MB Description 每天,农夫 John ...

  7. [Usaco2007 Jan]Balanced Lineup排队

    [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 2333 Solved: 1424 Des ...

  8. bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队【st表||线段树】

    要求区间取min和max,可以用st表或线段树维护 st表 #include<iostream> #include<cstdio> using namespace std; c ...

  9. bzoj:1699;poj 3264: [Usaco2007 Jan]Balanced Lineup排队

    Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置 ...

随机推荐

  1. Kafka 基本原理

    Kafka 基本原理   来源:阿凡卢 , www.cnblogs.com/luxiaoxun/p/5492646.html 简介 Apache Kafka是分布式发布-订阅消息系统.它最初由Link ...

  2. [Python] 拉格朗日插值

    #-*— coding:utf-8 -*- #Program 0.3 Lagrange Interpolation import matplotlib.pyplot as plt import num ...

  3. 单例模式(Singleton)

    单例模式  Singletonn Pattern Ensure a class has only one instance, and provide  a global point of access ...

  4. 经典算法 KMP算法详解

    内容: 1.问题引入 2.暴力求解方法 3.优化方法 4.KMP算法 1.问题引入 原始问题: 对于一个字符串 str (长度为N)和另一个字符串 match (长度为M),如果 match 是 st ...

  5. WGCNA 分析

    https://www.jianshu.com/p/f80de3468c04 https://mp.weixin.qq.com/s/-DthUKY2RTY6vxtxapzLkw https://www ...

  6. CYQ.Data 轻量数据层之路 使用篇二曲 MAction 数据查询(十三)----002

    原文链接:https://blog.csdn.net/cyq1162/article/details/53303390 前言说明: 本篇继续上一篇内容,本节介绍所有相关查询的使用. 主要内容提要: 1 ...

  7. tornado-简单的服务器非阻塞

    1.服务器 非阻塞 import tornado.ioloop import tornado.web import tornado.httpserver # 非阻塞 import tornado.op ...

  8. uva146-枚举,排列

    题意: 输入最多150个小写字母,在字典序增大的方向,求下一个排列是什么. 模拟枚举,最后一个字符是递归的最后一层(n层),那么把它弹出栈(还剩n-1层),如果n-1层的字符比第n层小,说明把n层的字 ...

  9. MYSQL体系结构-来自期刊

    MySQL三层体系结构 |-----------------------------------------------------------------------------------| | ...

  10. vsphere使用笔记

    一,安装虚拟机,需要使用vsphere client上传镜像,提示错误:   vsphere client 上传文件:Failed to log into NFC Server   , 解决办法:用浏 ...