이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "meetings.h"
#define fi first
#define se second
#define p_b push_back
#define m_p make_pair
#define sz(x) (int)x.size()
#define pii pair <int, int>
#define all(x) x.begin(),x.end()
using namespace std;
typedef long long ll;
const ll inf = 1e18;
struct node{
int pref, suf, size, ans;
};
vector <node> T;
node clr;
node merge(node a, node b){
if(a.size == 0)return b;
if(b.size == 0)return a;
node res;
res.size = a.size + b.size;
res.pref = a.pref;
if(a.pref == a.size)res.pref = a.size + b.pref;
res.suf = b.suf;
if(b.suf == b.size)res.suf = b.size + a.suf;
res.ans = max(a.ans, max(b.ans, a.suf + b.pref));
return res;
}
void build(int v, int tl, int tr, vector <int> &H){
if(tl == tr){
T[v].pref = T[v].suf = T[v].ans = (H[tl] == 2);
T[v].size = 1;
}else{
int tm = (tl + tr) >> 1;
build(v << 1, tl, tm, H);
build(v << 1 | 1, tm + 1, tr, H);
T[v] = merge(T[v << 1], T[v << 1 | 1]);
}
}
node get(int v, int tl, int tr, int l, int r){
if(l > r)return clr;
if(tl == l && tr == r)return T[v];
int tm = (tl + tr) >> 1;
return merge(get(v << 1, tl, tm, l, min(r, tm)), get(v << 1 | 1, tm + 1, tr, max(l, tm + 1), r));
}
vector<ll> minimum_costs(vector<int> H, vector<int> l, vector<int> r) {
int n = sz(H), q = sz(l);
if(n <= 5000 && q <= 5000){
vector <ll> ans(q);
vector < vector <ll> > pre_calc(n, vector <ll> (n, 0));
for(int x = 0; x < n; x++){
int mx = H[x];
ll sc = 0;
for(int j = x; j < n; j++){
mx = max(mx, H[j]);
sc += mx;
pre_calc[x][j] = sc;
}
mx = H[x];
sc = 0;
for(int j = x; j >= 0; j--){
mx = max(mx, H[j]);
sc += mx;
pre_calc[x][j] = sc;
}
}
for(int i = 0; i < q; i++){
ans[i] = pre_calc[l[i]][r[i]];
for(int x = l[i] + 1; x <= r[i]; x++){
ans[i] = min(ans[i], pre_calc[x - 1][l[i]] + pre_calc[x][r[i]]);
}
}
return ans;
}else{
vector <ll> ans(q);
T.resize(4 * n + 3);
build(1, 0, n - 1, H);
for(int i = 0; i < q; i++){
ans[i] = 2 * (r[i] - l[i] + 1) - get(1, 0, n - 1, l[i], r[i]).ans;
}
return ans;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |