Backjunアルゴリズム62136218号:Baranced Linuop
22177 ワード
リンク
https://www.acmicpc.net/problem/6218
質問する
For the daily milking, Farmer John's N cows (1 <= N <= 50,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 <= 200,000) 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.
入力と出力の例
sol 1)セグメントツリー
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define X first
#define Y second
#define pb push_back
#define fastio cin.tie(0)->sync_with_stdio(0)
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define sz(v) (int)(v).size()
#define all(v) v.begin(), v.end()
#define rall(v) (v).rbegin(), (v).rend()
#define compress(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define OOB(x, y) ((x) < 0 || (x) >= n || (y) < 0 || (y) >= m)
#define debug(x) cout << (#x) << ": " << (x) << '\n'
using namespace std;
using ll = long long;
using ull = unsigned long long;
using dbl = double;
using ldb = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using tii = tuple<int,int,int>;
template<typename T> using wector = vector<vector<T>>;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const ll LNF = 1e18 + 7;
template<typename T = int64_t, size_t sz = 17, typename F = plus<T>>
struct SegTree {
vector<T> tree; T t; F f{};
SegTree(T t = T()) : tree(1 << sz + 1, t), t(t) {}
explicit SegTree(T t, const F& f) : tree(1 << sz + 1, t), t(t), f(f) {}
void Init() {
for (int i = (1 << sz) - 1; i; i--) {
tree[i] = f(tree[i << 1], tree[i << 1 | 1]);
}
}
void Update(int i, T val) {
--i |= 1 << sz; tree[i] = val;
while (i >>= 1) tree[i] = f(tree[i << 1], tree[i << 1 | 1]);
}
T Query(int l, int r) {
T L = t, R = t; --l |= 1 << sz, --r |= 1 << sz;
while (l <= r) {
if (l & 1) L = f(L, tree[l++]);
if (~r & 1) R = f(tree[r--], R);
l >>= 1, r >>= 1;
}
return f(L, R);
}
};
auto Min = [](int& a,int& b) -> int{return a < b ? a : b;};
auto Max = [](int& a,int& b) -> int{return a > b ? a : b;};
using MinSeg = SegTree<int,20,decltype(Min)>;
using MaxSeg = SegTree<int,20,decltype(Max)>;
MinSeg MinST(INF, Min);
MaxSeg MaxST(-INF, Max);
int main(){
fastio;
int n,m; cin >> n >> m;
for(int i = 1; i <= n; i++){
int t; cin >> t;
MinST.Update(i, t);
MaxST.Update(i, t);
}
while(m--){
int a,b; cin >> a >> b;
cout << MaxST.Query(a, b) - MinST.Query(a, b) << "\n";
}
return 0;
}
この2つの問題の番号は同じなので、上のコードでこの2つの問題を解決することができます.「最大セグメントツリー」と「最小セグメントツリー」を使用して、答えを求めることができます.
Reference
この問題について(Backjunアルゴリズム62136218号:Baranced Linuop), 我々は、より多くの情報をここで見つけました https://velog.io/@inwooleeme/백준-알고리즘-62136218번-Balanced-Lineupテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol