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
题解
方法一
知识点:线段树。
区间最值板子题,熟悉一下模板。
时间复杂度
空间复杂度
方法二
知识点:ST表。
不需要修改,因此同样也可以ST表做。
时间复杂度
空间复杂度
代码
方法一
#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; }
本文来自博客园,作者:空白菌,转载请注明原文链接:https://www.cnblogs.com/BlankYang/p/17353872.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧