백준 알고리즘 6213,6218번 : Balanced Lineup

Zoo Da·2021년 9월 22일
0

백준 알고리즘

목록 보기
211/337
post-thumbnail

링크

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;
}

두 문제가 번호만 다른 똑같은 문제라서 위의 코드로 두 문제 모두 풀 수 있습니다.
최대 값 세그먼트 트리와 최소 값 세그먼트 트리 두개를 사용해서 답을 구해주면 됩니다.

profile
메모장 겸 블로그

0개의 댓글