NC25045 [USACO 2007 Jan S]Balanced Lineup
题目
题目描述
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的更多相关文章
- 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 ...
- BZOJ 1634 洛谷2878 USACO 2007.Jan Protecting the flowers护花
[题意] 约翰留下他的N只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽花朵!为了使接下来花朵的损失最小 ...
- BZOJ1699: [Usaco2007 Jan]Balanced Lineup排队
1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 933 Solved: 56 ...
- BZOJ1636: [Usaco2007 Jan]Balanced Lineup
1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 476 Solved: 345[ ...
- BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队( RMQ )
RMQ.. ------------------------------------------------------------------------------- #include<cs ...
- BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队
1699: [Usaco2007 Jan]Balanced Lineup排队 Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. ...
- bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树
1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 772 Solved: 560线 ...
- bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块
1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MB Description 每天,农夫 John ...
- [Usaco2007 Jan]Balanced Lineup排队
[Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 2333 Solved: 1424 Des ...
- bzoj1699[Usaco2007 Jan]Balanced Lineup排队*&bzoj1636[Usaco2007 Jan]Balanced Lineup*
bzoj1699[Usaco2007 Jan]Balanced Lineup排队 bzoj1636[Usaco2007 Jan]Balanced Lineup 题意: 询问区间最大值减区间最小值的差. ...
随机推荐
- citespace 文献计量工具初探
先放几个教程: 知乎 - CiteSpace 使用教程 - 312 赞同 知乎 - CiteSpace 入门教程 - 949 赞同 简书 - 研究方法 | 用 CiteSpace 进行科学文献可视化分 ...
- 在线photoshop网页版工具开发
基于javascript开发的在线ps工具,打包方式webpack 在线预览 在线ps网页版 源码地址 https://github.com/geeeeeeeek 功能介绍 在线图像编辑器允许您使用H ...
- 【MicroPython】生成模块表py\objmodule.c中结构mp_rom_map_elem_t - py\makemoduledefs.py
查找文件中的模块注册标记MP_REGISTER_MODULE pattern = re.compile(r"[\n;]\s*MP_REGISTER_MODULE\((.*?),\s*(.*? ...
- 【TouchGFX】AnalogClock 小部件使用小记
- 函数指针、std::function、std::bind
函数指针.std::function.std::bind 函数指针: C++语法中可以直接将函数名作为指针, void fun(int a, int b); 在这个函数声明中,函数指针即为fun,传入 ...
- Kubernerts - 概览
1. Kubernerts K8s,是用于自动部署.扩容和管理容器化应用程序的开源系统 1.1 特性 自动化上线与回滚 分步骤针对应用或者配置更改上线,监控应用的运行状态同时不会终止所有实例,若出现问 ...
- JS逆向实战27——pdd的anti_content 分析与逆向
声明 本文章中所有内容仅供学习交流,抓包内容.敏感网址.数据接口均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关,若有侵权,请联系我立即删除! 本文已在微信公众号发布 目 ...
- [转帖]一文浅析Nginx线程池!
https://zhuanlan.zhihu.com/p/616500765 Nginx通过使用多路复用IO(如Linux的epoll.FreeBSD的kqueue等)技术很好的解决了c10k ...
- 【DP】DMOPC '21 Contest 8 P5 - Tree Building
Problem Link 给定 \(n,m\) 和一个长为 \(m\) 的代价序列,对于一棵 \(n\) 个节点,每个节点度数不超过 \(m\) 的树,定义它的代价为 \(\sum\limits_{i ...
- 【K哥爬虫普法】房产数据刑吗?爬虫多年没踩过缝纫机,劝你找找自己原因!
我国目前并未出台专门针对网络爬虫技术的法律规范,但在司法实践中,相关判决已屡见不鲜,K哥特设了"K哥爬虫普法"专栏,本栏目通过对真实案例的分析,旨在提高广大爬虫工程师的法律意识,知 ...