Skip to content

Commit eac2c02

Browse files
committedOct 21, 2020
boj 6549 히스토그램에서 가장 큰 직사각형
1 parent f8a6539 commit eac2c02

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
 

‎세그먼트 트리2/6549.cpp

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <iostream>
2+
using namespace std;
3+
typedef long long ll;
4+
5+
int list[100000], N;
6+
ll tree[400000];
7+
8+
ll Max(ll x, ll y) {
9+
return x > y ? x : y;
10+
}
11+
12+
ll Min(ll x, ll y) {
13+
return list[x] < list[y] ? x : y;
14+
}
15+
16+
ll init(int node, int s, int e) {
17+
if (s == e) {
18+
return tree[node] = s;
19+
}
20+
21+
int m = (s + e) / 2;
22+
return tree[node] = Min(init(node * 2, s, m), init(node * 2 + 1, m + 1, e));
23+
}
24+
25+
ll minindex(int node, int s, int e, int l, int r) {
26+
if (e < l || s > r) return -1;
27+
if (l <= s && e <= r) return tree[node];
28+
29+
int m = (s + e) / 2;
30+
int m1 = minindex(node * 2, s, m, l, r);
31+
int m2 = minindex(node * 2 + 1, m + 1, e, l, r);
32+
33+
if (m1 == -1) return m2;
34+
else if (m2 == -1) return m1;
35+
else {
36+
return Min(m1, m2);
37+
}
38+
}
39+
40+
ll maxarea(int s, int e) {
41+
int index = minindex(1, 0, N - 1, s, e);
42+
43+
ll area = (ll)(e - s + 1) *list[index];
44+
45+
if (s < index) area = Max(area, maxarea(s, index - 1));
46+
if (index < e) area = Max(area, maxarea(index + 1, e));
47+
48+
return area;
49+
}
50+
51+
int main() {
52+
cin.tie(NULL); cout.tie(NULL);
53+
ios::sync_with_stdio(false);
54+
55+
while (1) {
56+
cin >> N;
57+
if (!N) break;
58+
59+
for (int i = 0; i < N; i++) {
60+
cin >> list[i];
61+
}
62+
63+
init(1, 0, N - 1);
64+
cout << maxarea(0, N - 1) << '\n';
65+
}
66+
67+
return 0;
68+
}

0 commit comments

Comments
 (0)
Please sign in to comment.