题目链接

题目

题目描述

For the daily milking, Farmer John's N cows (1 ≤ N ≤ 100,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 ≤ 30) 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.

输入描述

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.

输出描述

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.

示例1

输入

6 3
1
7
3
4
2
5
1 5
4 6
2 2

输出

6
3
0

题解

方法一

知识点:线段树。

区间最值板子题,熟悉一下模板。

时间复杂度 \(O((n+q)\log n)\)

空间复杂度 \(O(n)\)

方法二

知识点:ST表。

不需要修改,因此同样也可以ST表做。

时间复杂度 \(O(n\log n + q)\)

空间复杂度 \(O(n)\)

代码

方法一

#include <bits/stdc++.h>
using namespace std;
using ll = long long; struct T {
int mx, mi;
static T e() { return { (int)-2e9,(int)2e9 }; }
friend T operator+(const T &a, const T &b) { return { max(a.mx, b.mx),min(a.mi,b.mi) }; }
}; template<class T>
class SegmentTree {
int n;
vector<T> node; T query(int rt, int l, int r, int x, int y) {
if (r < x || y < l) return T::e();
if (x <= l && r <= y) return node[rt];
int mid = l + r >> 1;
return query(rt << 1, l, mid, x, y) + query(rt << 1 | 1, mid + 1, r, x, y);
} public:
SegmentTree() {}
SegmentTree(const vector<T> &src) { init(src); } void init(const vector<T> &src) {
assert(src.size());
n = src.size() - 1;
node.assign(n << 2, T::e());
function<void(int, int, int)> build = [&](int rt, int l, int r) {
if (l == r) return node[rt] = src[l], void();
int mid = l + r >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
node[rt] = node[rt << 1] + node[rt << 1 | 1];
};
build(1, 1, n);
} T query(int x, int y) { return query(1, 1, n, x, y); }
}; int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, q;
cin >> n >> q;
vector<T> a(n + 1);
for (int i = 1, x;i <= n;i++) cin >> x, a[i] = { x,x };
SegmentTree<T> sgt(a);
while (q--) {
int l, r;
cin >> l >> r;
auto [mx, mi] = sgt.query(l, r);
cout << mx - mi << '\n';
}
return 0;
}

方法二

#include <bits/stdc++.h>
using namespace std;
using ll = long long; struct T {
int mx, mi;
static T e() { return { (int)-2e9,(int)2e9 }; }
friend T operator+(const T &a, const T &b) { return { max(a.mx, b.mx),min(a.mi,b.mi) }; }
}; template<class T>
class ST {
vector<vector<T>> node; public:
ST() {}
ST(const vector<T> &src) { init(src); } void init(const vector<T> &src) {
assert(src.size());
int n = src.size() - 1;
int sz = log2(n);
node.assign(sz + 1, vector<T>(n + 1));
for (int i = 1;i <= n;i++) node[0][i] = src[i];
for (int i = 1;i <= sz;i++)
for (int j = 1;j + (1 << i) - 1 <= n;j++)
node[i][j] = node[i - 1][j] + node[i - 1][j + (1 << i - 1)];
} T query(int l, int r) {
int k = log2(r - l + 1);
return node[k][l] + node[k][r - (1 << k) + 1];
}
}; int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, q;
cin >> n >> q;
vector<T> a(n + 1);
for (int i = 1, x;i <= n;i++) cin >> x, a[i] = { x,x };
ST<T> st(a);
while (q--) {
int l, r;
cin >> l >> r;
auto [mx, mi] = st.query(l, r);
cout << mx - mi << '\n';
}
return 0;
}

NC25045 [USACO 2007 Jan S]Balanced Lineup的更多相关文章

  1. NC25043 [USACO 2007 Jan S]Protecting the Flowers

    NC25043 [USACO 2007 Jan S]Protecting the Flowers 题目 题目描述 Farmer John went to cut some wood and left ...

  2. BZOJ 1634 洛谷2878 USACO 2007.Jan Protecting the flowers护花

    [题意] 约翰留下他的N只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽花朵!为了使接下来花朵的损失最小 ...

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

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

  4. BZOJ1636: [Usaco2007 Jan]Balanced Lineup

    1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 476  Solved: 345[ ...

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

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

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

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

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

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

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

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

  9. [Usaco2007 Jan]Balanced Lineup排队

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

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

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

随机推荐

  1. docker 原理之 user namespace(下)

    1. user namespace user namespace 主要隔离了安全相关的标识符和属性,包括用户 ID,用户组 ID,key 和 capabilities 等.同样一个用户 id 在不同 ...

  2. 每天学五分钟 Liunx | 有趣的 log

    说明:看 systemd log 的时候发现了一段有意思的打印,不太明白为什么会这样,贴出来与朋友们分享,欢迎知道的朋友们说明下,非常感谢.   问题描述:服务启动时,会执行 python 脚本,该脚 ...

  3. centos7使用nginx+uwsgi部署python django项目

    在django框架中,我们一般直接通过python manage.py runserver来启动提供服务,但是如果生产环境此方法不可行,而且容易导致异常退出,于是需要借助uwsgi来作为守护进程. 操 ...

  4. C++中vector容器详解

    参考链接:https://www.runoob.com/w3cnote/cpp-vector-container-analysis.html 一.什么是vector? 向量(Vector)是一个封装了 ...

  5. IDEA:端口号被占用解决办法

    idea遇到这样的问题:如下图 解决办法 步骤1:通过端口号找到pid打开dos命令行,输入netstat -ano | find "9009"得到下列内容,看到最后一行就是pid ...

  6. [转帖]细说ASCII、GB2312/GBK/GB18030、Unicode、UTF-8/UTF-16/UTF-32编码

    参考: <编码标准-GB2312 GBK GB18030> <字符编码笔记:ASCII,Unicode 和 UTF-8> <字体编辑用中日韩汉字Unicode编码表> ...

  7. [转帖]【linux命令学习】— sar 命令学习

    https://blog.csdn.net/u013332124/article/details/101075521 一.命令使用介绍 sar命令全称 System Activity Report,它 ...

  8. [转帖]prometheus node-exporter 全部指标说明

    https://www.cnblogs.com/276815076/p/16383615.html Basic CPU / Mem / Disk Info Basic CPU / Mem / Disk ...

  9. [转帖]K8S 挂载 minio csi 的方式.

    对象存储   前置条件 安装Minio(在102主机上操作) 安装csi-s3插件(在103主机上操作) 使用 参考 本文介绍kubernetes如何基于对象存储(minio)创建PV与PVC 前置条 ...

  10. 【转帖】MySQL 8.0.32如期而至

    MySQL 8.0版本计划 MySQL 8.0开始采用快速迭代开发模式,基本上是每隔3个月就发布一个新的小版本.去年1月18日(2022.1.18)发布MySQL 8.0.28,今年1月17日发布My ...