PS
BOJ 1989 : 부분배열 고르기 2
lickelon
2024. 9. 5. 14:11
- 문제 링크 : boj.kr/1989
- 난이도 : P5
- 태그 : 세그먼트 트리, 분할 정복
코드
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define INF 0x7FFFFFFF
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
template<typename T>
class segTree {
private:
ll n;
T id;
T(*merge)(T, T);
vector<T> tree;
public:
segTree(ll n, T id, T(*merge)(T, T)) {
this->n = n;
this->id = id;
this->merge = merge;
tree.resize(n*4);
}
void update(ll idx, T value) {
_update(1, 1, n, idx, value);
}
T query(ll l, ll r) {
return _query(1, 1, n, l, r);
}
private:
void _update(int node, int s, int e, int idx, T value) {
if(idx < s || e < idx) return;
if(s == e) {
tree[node] = value;
return;
}
_update(node*2, s, (s+e)/2, idx, value);
_update(node*2+1, (s+e)/2+1, e, idx, value);
tree[node] = merge(tree[node*2], tree[node*2+1]);
}
T _query(int node, int s, int e, int l, int r) {
if(l > e || r < s) return id;
if(l <= s && e <= r) return tree[node];
T lq = _query(node*2, s, (s+e)/2, l, r);
T rq = _query(node*2+1, (s+e)/2+1, e, l, r);
return merge(lq, rq);
}
};
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
segTree<pll> s1(n, pll(-1, INF), [](pll a, pll b){return (a.second > b.second ? b : a);});
segTree<ll> s2(n, 0, [](ll a, ll b){return a+b;});
for(int i = 1; i <= n; i++) {
int input;
cin >> input;
s1.update(i, {i, input});
s2.update(i, input);
}
function<pair<ll, pll>(ll,ll)> find = [&](ll left, ll right)->pair<ll, pll>{
if(left > right) return {0, {0,0}};
pll q = s1.query(left, right);
ll w = s2.query(left, right);
ll h = q.second;
ll ans = w*h;
pair<ll, pll> ret, temp;
ret.first = ans;
ret.second = {left, right};
temp = find(left, q.first-1);
if(temp.first > ret.first) ret = temp;
temp = find(q.first+1, right);
if(temp.first > ret.first) ret = temp;
return ret;
};
auto ans = find(1, n);
cout << ans.first << "\n" << ans.second.first << " " << ans.second.second;
return 0;
}
풀이
#2104와 비슷하게 풀 수 있다.
l과 r값만 함께 구해주면 된다.
728x90