Submission #251199

#TimeUsernameProblemLanguageResultExecution timeMemory
251199ryanseeMeetings (IOI18_meetings)C++14
100 / 100
3774 ms604136 KiB
#include "meetings.h"

#include "bits/stdc++.h"
using namespace std;

#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr<<"hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return rng() % (y+1-x) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }

using ll=long long; 
using ld=long double;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
using pi=pair<ll,ll>; using spi=pair<ll,pi>; using dpi=pair<pi,pi>; 

#define LLINF ((long long)1e18)
#define INF int(1e9+1e6)
#define MAXN (750006)
ll n, A[MAXN];
pi sp[20][MAXN];
void build(){
	FOR(i,0,n-1)sp[0][i]=pi(A[i],i);
	FOR(h,1,19){
		for(ll i=0;i+(1<<h)<=n;++i){
			sp[h][i]=max(sp[h-1][i],sp[h-1][i+(1<<(h-1))]);
		}
	}
}
ll qry(ll x,ll y){
	ll h=__builtin_clz(y-x+1)^31;
	return max(sp[h][x],sp[h][y-(1<<h)+1]).s;
}
struct node {
	int s,e,m;
	pi v;
	ll add;
	node*l,*r;
	node(int S,int E){
		s=S,e=E,m=(s+e)>>1;
		v = pi(0, LLINF);
		add=0;
		if(s^e)l=new node(s,m),r=new node(m+1,e);
	}
	void update(int x,int y,ll nval){
		value();
		if(s==x&&e==y){
			add+=nval;
			return;
		}
		if(x>m)r->update(x,y,nval);
		else if(y<=m)l->update(x,y,nval);
		else l->update(x,m,nval),r->update(m+1,y,nval);
	}
	ll f(ll x,pi line) { return line.f*x+line.s; }
	void ins(int x,int y,pi line){
		value();
		if(s==x&&e==y){
			bool one=f(s,v) > f(s,line);
			bool two=f(m,v) > f(m,line);
			if(two) swap(v,line);
			if(s==e) return;
			if(f(s,v) <= f(s,line) && f(e,v) <= f(e,line))return;
			if(one==two){//right
				r->ins(m+1,y,line);
			}else{
				l->ins(x,m,line);
			}
			return;
		}
		if(x>m) r->ins(x,y,line);
		else if(y<=m) l->ins(x,y,line);
		else l->ins(x,m,line), r->ins(m+1,y,line);
	}
	ll rmq(int x){
		value();
		if(s==e) return f(x,v);
		if(x>m) return min(f(x,v),r->rmq(x));
		else return min(f(x,v),l->rmq(x));
	}
	void value(){
		v.s += add;
		if(s^e)l->add+=add,r->add+=add;
		add=0;
	}
} *l, *r;
vector<ll> ans;
vector<spi> v[MAXN];
void dnc(int s,int e){
	if(s>e) return;
	int m=qry(s,e);
	dnc(s,m-1), dnc(m+1,e);
	l->insert(m,m,pi(0,0)), r->insert(m,m,pi(0,0));
	for(auto i:v[m]){
		ans[i.f] = min(l->rmq(i.s.f)+(i.s.s-m+1)*A[m],r->rmq(i.s.s)+(m-i.s.f+1)*A[m]);
	}
	l->update(s,m,(e-m+1)*A[m]);
	l->ins(s,m,pi(-A[m],(m == e ? 0 : l->rmq(m+1))+(m+1)*A[m]));
	r->update(m,e,(m-s+1)*A[m]);
	r->ins(m,e,pi(A[m],(s == m ? 0 : r->rmq(m-1))-(m-1)*A[m]));
}
vector<ll> minimum_costs(std::vector<int> H, std::vector<int> L, std::vector<int> R) {
	n = H.size();
	FOR(i,0,n-1)A[i]=H[i];
	build();
	FOR(i,0,siz(L)-1)v[qry(L[i],R[i])].eb(i, pi(L[i],R[i]));		
	l=new node(0,n-1), r=new node(0,n-1);
	ans.resize(siz(L));
	dnc(0, n-1);
	return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...