This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "meetings.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ss second
#define ff first
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define mkp make_pair
typedef long double ld;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const ll inf = 1e18;
const int mod = 1e9+7; //998244353;
const int maxn = 1e5+5;
const int Xg[4] = {1,0,-1,0}, Yg[4] = {0,1,0,-1};
ll modpw(ll a,ll e) {if(e==0)return 1;ll x=modpw(a*a%mod,e>>1);return e&1?x*a%mod:x;}
int cep[maxn],sag[maxn];
int T[4*maxn];
ll p[maxn];
ll jog[maxn];
stack<pii>s;
vector<pii> v;
void build(int l,int r,int nd){
if(l==r){
T[nd]=v[l].ss;
return;
}
build(l,(l+r)/2,nd*2);
build((l+r)/2+1,r,nd*2+1);
T[nd]=max(T[nd*2],T[nd*2+1]);
}
int tap(int a,int b,int l,int r,int nd){
if(r < a or l > b) return 0;
if(l >= a and r <= b) return T[nd];
return max(tap(a,b,l,(l+r)/2,nd*2),tap(a,b,(l+r)/2+1,r,nd*2+1));
}
vll minimum_costs(vi H, vi L,vi R) {
int Q=sz(L); vll C;
if(Q <= 5e3){
for(int i = 0;i < Q;i++){
memset(jog,0,sizeof(jog));
memset(cep,0,sizeof(cep));
memset(sag,0,sizeof(sag));
memset(p,0,sizeof(p));
while(!s.empty()) s.pop();
for(int j = L[i];j <= R[i];j++){
while(!s.empty() and s.top().ss <= H[j]) s.pop();
if(!s.empty()) cep[j]=s.top().ff+1;
else cep[j]=L[i];
s.push({j,H[j]});
}
while(!s.empty()) s.pop();
for(int j = R[i];j >= L[i];j--){
while(!s.empty() and s.top().ss < H[j]) s.pop();
if(!s.empty()) sag[j]=s.top().ff-1;
else sag[j]=R[i];
s.push({j,H[j]});
}
for(int j = L[i];j <= R[i];j++){
ll num=j-cep[j];
ll num1= sag[j]-j;
p[cep[j]] += ((num1+1)*H[j]);
p[j] -= ((num1+1)*H[j]);
p[j+1] += ((num+1)*H[j]);
p[sag[j]+1] -= ((num+1)*H[j]);
jog[j] = (num+num1+1)*H[j];
}
ll ans = inf;
for(int j = L[i];j <= R[i];j++){
if(j) p[j] += p[j-1];
ans = min(ans,p[j]+jog[j]);
}
C.pb(ans);
}
return C;
}
else {
int n=sz(H);
for(int i = 0;i < n;i++){
if(H[i]==2) continue;
if(i==0) cep[0]=0;
else {
if(H[i-1]==1) cep[i]=cep[i-1];
else cep[i]=i;
}
}
for(int i = n-1;i >= 0;i--){
if(H[i]==2) continue;
if(i==n-1) sag[i]=i;
else {
if(H[i+1]==1) sag[i]=sag[i+1];
else sag[i]=i;
}
}
for(int i = 0;i < n;i++){
if(H[i]==0) continue;
int now=i;
while(H[now]==1) now++;
v.pb(mkp(i,now-i));
}
// cout<<"cnjldsc\n";
build(0,sz(v)-1,1);
ll ans;
for(int i = 0;i < Q;i++){
ans = -inf;
int siz=R[i]-L[i]+1;
if(L[i]==1) ans=max(ans,1ll*sag[i]-i+1),L[i]=sag[i]+1;
if(R[i]==1) ans=max(ans,1ll*i-cep[i]+1),R[i]=cep[i]-1;
if(L[i] < R[i]){
int sl=lower_bound(all(v),mkp(L[i],0))-v.begin();
int sr=upper_bound(all(v),mkp(R[i],0))-v.begin();
ans = max(ans,1ll*tap(sl,sr,0,sz(v)-1,1));
C.pb((siz-ans)*2+ans);
}
}
return C;
}
}
# | 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... |