This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
# pragma GCC optimize("Ofast")
# pragma GCC optimization ("unroll-loops")
# include "bits/stdc++.h"
std::pair<int,int> DR[] = {{-1,0},{0,1},{1,0},{0,-1},{-1,1},{-1,-1},{1,1},{1,-1}};
# define ll long long
# define clock (clock() * 1000.0 / CLOCKS_PER_SEC)
# define rc(s) return cout << s,0
# define rcg(s) cout << s;exit(0)
# define _ ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
# define db(x) cerr << #x << " = " << x << '\n'
# define pb push_back
# define mp make_pair
# define all(s) s.begin(),s.end()
# define sz(x) (int)((x).size())
# define int ll
using namespace std;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# define LOCAL
# define sim template < class c
# define ris return * this
# define dor > debug & operator <<
# define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define show(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
int gcd(int a, int b)
{
if(b) return gcd(b,a%b);
return a;
}mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/// LONG LONG BAGA
int t[1 << 22],a[1 << 20],nxt[2][1 << 20],n;
void upd(int nod,int l,int r,int pos,int val)
{
if(l == r)
{
t[nod] = val;
return ;
}
int mid = l + r >> 1;
if(pos <= mid) upd(nod << 1,l,mid,pos,val);
else upd(nod << 1 | 1,mid + 1,r,pos,val);
t[nod] = min(t[nod << 1],t[nod << 1 | 1]);
}
int qry(int nod,int tl,int tr,int l,int r)
{
if(l > r) return 1e18 + 1;
if(l == tl && tr == r) return t[nod];
int mid = tl + tr >> 1;
return min(qry(nod << 1,tl,mid,l,min(mid,r)),qry(nod << 1 | 1,mid + 1,tr,max(mid + 1,l),r));
}
int calcleft(int idx,int f)
{
if(f == 0) return 0;
int ans = 0;
while(idx)
{
if(idx > f && nxt[0][idx] <= f)
{
ans += a[idx] * (f - nxt[0][idx]);
}
else if(idx <= f)
{
ans += a[idx] * (idx - nxt[0][idx]);
}
idx = nxt[0][idx];
}
return ans;
}
int calright(int idx,int f)
{
if(f == n + 1) return 0;
int ans = 0;
while(idx != n + 1)
{
if(idx < f && nxt[1][idx] >= f)
{
ans += a[idx] * (nxt[1][idx] - f);
}
else if(idx >= f)
{
ans += a[idx] * (nxt[1][idx] - idx);
}
idx = nxt[1][idx];
}
return ans;
}
int Q(int l,int r)
{
set<int>ms;
ms.insert(l);
ms.insert(r);
ms.insert(r + 1);
int idx = nxt[1][l];
while(idx < r)
{
ms.insert(idx);
idx = nxt[1][idx];
}
idx = nxt[0][r];
while(idx >= l)
{
ms.insert(idx + 1);
idx = nxt[0][idx];
}
int ans = 1e18;
vector<int>vec;
for(auto it : ms) vec.pb(it);
for(int i = 0;i < sz(vec) - 1;i++)
{
int L = calcleft(vec[i],l - 1);
int R = calright(vec[i],r + 1);
ans = min(ans,qry(1,1,n,vec[i],vec[i + 1] - 1) - L - R);
}
return ans;
}
#undef int
vector<ll>minimum_costs(vector<int>H, vector<int>L, vector<int>R)
#define int ll
{
n = sz(H);
for(int i = 1;i <= n;i++)
{
a[i] = H[i - 1];
}
stack<int>st;
st.push(0);
a[0] = 1e9 + 1;
for(int i = 1;i <= n;i++)
{
while(a[st.top()] <= a[i])
{
st.pop();
}
nxt[0][i] = st.top();
st.push(i);
}
while(sz(st)) st.pop();
st.push(n + 1);
a[n + 1] = 1e9 + 1;
for(int i = n;i >= 1;i--)
{
while(a[st.top()] <= a[i])
{
st.pop();
}
nxt[1][i] = st.top();
st.push(i);
}
for(int i = 1;i <= 4 * n;i++) t[i] = 1e18 + 1;
for(int i = 1;i <= n;i++)
{
int x = 0;
int idx = i;
while(idx)
{
x += (idx - nxt[0][idx]) * a[idx];
idx = nxt[0][idx];
}
idx = i;
while(idx != n + 1)
{
x += (nxt[1][idx] - idx) * a[idx];
idx = nxt[1][idx];
}
x -= a[i];
upd(1,1,n,i,x);
}
vector<int>ans;
for(int i = 0;i < sz(L);i++)
{
L[i]++;
R[i]++;
ans.pb(Q(L[i],R[i]));
}
return ans;
}
/*
int32_t main(){_
//freopen("input","r",stdin);
return 0;
}*/
Compilation message (stderr)
meetings.cpp:2:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
# pragma GCC optimization ("unroll-loops")
meetings.cpp: In function 'void upd(long long int, long long int, long long int, long long int, long long int)':
meetings.cpp:67:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
meetings.cpp: In function 'long long int qry(long long int, long long int, long long int, long long int, long long int)':
meetings.cpp:77:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = tl + tr >> 1;
~~~^~~~
# | 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... |