Cod sursa(job #488075)

Utilizator tamas_iuliaTamas Iulia tamas_iulia Data 27 septembrie 2010 17:17:10
Problema Stalpi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.74 kb
#include <stdio.h>
#include <vector>
#include <algorithm>
#define Nmax 100002
#define INF 1000000000
#define BIG 10000000000000LL
#define Amax (1<<18)+1
#define LL long long
#define pb push_back
#define mp make_pair
#define x first
#define y second

using namespace std;

pair< pair< int,int > , int > A[Nmax];
int X[Nmax];
LL din[Nmax];
LL Arb[Amax];
int N,wh,mn,Pas;

inline LL Minim(LL x,LL y){ return x<y ? x:y; }
inline LL Maxim(LL x,LL y){ return x>y ? x:y; }

inline void Update(int nod,int l,int r,int poz,int val){
	if( l==r ){
		Arb[nod]=Minim(Arb[nod],val);
		return;
	}
	int m=(l+r)>>1;
	if( poz<=m ) Update(nod<<1,l,m,poz,val); 
	else Update((nod<<1)+1,m+1,r,poz,val);
	Arb[nod]=Minim(Arb[nod],Minim(Arb[2*nod],Arb[2*nod+1]));
}
inline void Query(int nod,int l,int r,int x,int y){
	if( x<=l && r<=y ){
		mn=Minim(mn,Arb[nod]);
		return;
	}
	int m=(l+r)>>1;
	if( x<=m ) Query(nod<<1,l,m,x,y);
	if( m <y ) Query((nod<<1)+1,m+1,r,x,y);
}
	
inline int cmp( pair< pair< int,int > , int > A, pair< pair< int,int > , int > B ){
	return A.x.x < B.x.x;
}

int main(){
	int i,L,R,c,s,d;
	freopen("stalpi.in","r",stdin);
	freopen("stalpi.out","w",stdout);
	scanf("%d",&N);
	for(i=1;i<=N;++i){
		scanf("%d%d%d%d",&X[i],&c,&s,&d);
		s=Maxim(X[i]-s,1), d=Minim(d+X[i],INF);
		A[i]=mp(mp(s,d),c);
	}
	
	sort(X+1,X+N+1);
	sort(A+1,A+N+1);
	
	for(i=1;i<=N;++i) din[i]=BIG;
	for(i=1;i<Amax;++i) Arb[i]=BIG;
	
	for(i=1;i<=N;++i){
		L=lower_bound(X+1,X+N+1,A[i].x.x)-X-1;
		R=upper_bound(X+1,X+N+1,A[i].x.y)-X-1;
		
		mn=BIG;
		if(L==0) mn=0; 
		else Query(1,1,N,L,R);
		
		din[R]=Minim(din[R],mn+A[i].y);
		
		Update(1,1,N,R,din[R]);
	}
	
	printf("%lld\n",din[N]);
	fclose(stdin); fclose(stdout);
	return 0;
}