문제 링크
https://www.acmicpc.net/problem/6213
6213번: Balanced Lineup
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 t
www.acmicpc.net
https://www.acmicpc.net/problem/6218
6218번: Balanced Lineup
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 t
www.acmicpc.net
문제
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 <= 180,000(6213), 200,000(6218)) 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.
Note: on the largest test case, I/O takes up the majority of the runtime.(6213)
입력
- 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 an answer to an input query and tells the difference in height between the tallest and shortest cow in the input range.
예제 입력 1
6 3
1
7
3
4
2
5
1 5
4 6
2 2
예제 출력 1
6
3
0
알고리즘 분류
- 자료 구조
풀이
최솟값을 찾기 위한 세그먼트 트리, 최댓값을 찾기 위한 세그먼트 트리 총 2개의 세그먼트 트리를 구현하고 구간 [A, B] 내의 최댓값과 최솟값의 차이를 구한다.
코드
#include <iostream>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#include <climits>
#define FASTIO cin.tie(NULL); cout.tie(NULL); ios::sync_with_stdio(false);
#define MAX 50001
#define LL long long
#define INF 1e9
using namespace std;
int N, Q;
int Tree_Height, Tree_Size;
vector<int> MinTree;
vector<int> MaxTree;
int MAP[MAX];
void Input() {
cin >> N >> Q;
for (int i = 0; i < N; i++) {
cin >> MAP[i];
}
}
int Make_Min_SegTree(int Node, int S, int E) {
if (S == E) {
return MinTree[Node] = MAP[S];
}
int MID = (S + E) / 2;
int L = Make_Min_SegTree(Node * 2, S, MID);
int R = Make_Min_SegTree(Node * 2 + 1, MID + 1, E);
MinTree[Node] = min(L, R);
return MinTree[Node];
}
int Make_Max_SegTree(int Node, int S, int E) {
if (S == E) {
return MaxTree[Node] = MAP[S];
}
int MID = (S + E) / 2;
int L = Make_Max_SegTree(Node * 2, S, MID);
int R = Make_Max_SegTree(Node * 2 + 1, MID + 1, E);
MaxTree[Node] = max(L, R);
return MaxTree[Node];
}
int Find_Min_Value(int Node, int S, int E, int L, int R) {
if ((E < L) || (R < S)) {
return INF;
}
if ((L <= S) && (E <= R)) {
return MinTree[Node];
}
int MID = (S + E) / 2;
int LV = Find_Min_Value(Node * 2, S, MID, L, R);
int RV = Find_Min_Value(Node * 2 + 1, MID + 1, E, L, R);
return min(LV, RV);
}
int Find_Max_Value(int Node, int S, int E, int L, int R) {
if ((E < L) || (R < S)) {
return -1;
}
if ((L <= S) && (E <= R)) {
return MaxTree[Node];
}
int MID = (S + E) / 2;
int LV = Find_Max_Value(Node * 2, S, MID, L, R);
int RV = Find_Max_Value(Node * 2 + 1, MID + 1, E, L, R);
return max(LV, RV);
}
void Settings() {
Tree_Height = (int)ceil(log2(N));
Tree_Size = (1 << (Tree_Height + 1));
MinTree.resize(Tree_Size);
MaxTree.resize(Tree_Size);
Make_Min_SegTree(1, 0, N - 1);
Make_Max_SegTree(1, 0, N - 1);
}
void Find_Answer() {
for (int i = 0; i < Q; i++) {
int A, B;
cin >> A >> B;
A--;
B--;
cout << Find_Max_Value(1, 0, N - 1, A, B) - Find_Min_Value(1, 0, N - 1, A, B) << "\n";
}
}
int main() {
FASTIO
Input();
Settings();
Find_Answer();
return 0;
}
'BOJ > Gold' 카테고리의 다른 글
[BOJ/Gold 5] 백준 17352 여러분의 다리가 되어 드리겠습니다!(C++) (0) | 2022.03.03 |
---|---|
[BOJ/Gold 5] 백준 7511 소셜 네트워킹 어플리케이션(C++) (0) | 2022.03.03 |
[BOJ/Gold 1] 백준 20183 골목 대장 호석 - 효율성 2(C++) (0) | 2022.02.27 |
[BOJ/Gold 1] 백준 11781 퇴근 시간(C++) (0) | 2022.02.27 |
[BOJ/Gold 1] 백준 10776 제국(C++) (0) | 2022.02.27 |