Cod sursa(job #201068)

Utilizator andrei.12Andrei Parvu andrei.12 Data 28 iulie 2008 23:24:23
Problema Stalpi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.72 kb
#include<stdio.h>
#include<algorithm>

using namespace std;

#define lg 100005
#define inf 0x3f3f3f3f

int n, i, ind, poz;
long long cnt[lg], d[lg], val;
struct ches{
	int x, s, d, c;
};
ches v[lg], w[lg];

inline int cmp(ches a, ches b){
	return a.x < b.x;
}
inline int cmp2(ches a, ches b){
	if (a.s != b.s)
		return a.s < b.s;
	return a.d < b.d;
}
int caut(int val){
	int li = 2, ls = n+1, x, gs = -1;

	while (li <= ls){
		x = (li+ls) / 2;

		if (v[x].x >= val){
			gs = x;
			ls = x-1;
		}
		else
			li = x+1;
	}

	return gs;
}
int caut2(int val){
	int li = 2, ls = n+1, x, gs = -1;

	while (li <= ls){
		x = (li+ls) / 2;

		if (v[x].x <= val){
			gs = x;
			li = x+1;
		}
		else
			ls = x-1;
	}

	return gs;
}
void update(int poz, long long val){
	while (poz <= n+1){
		cnt[poz] = min(cnt[poz], val);

		poz += (poz^(poz-1)) & poz;
	}
}
int find(int poz){
	long long gs = 20000000000LL;
	while (poz > 0){
		gs = min(gs, cnt[poz]);

		poz -= (poz^(poz-1)) & poz;
	}

	return gs;
}

int main()
{
	freopen("stalpi.in", "rt", stdin);
	freopen("stalpi.out", "wt", stdout);

	scanf("%d", &n);
	for (i = 2; i <= n+1; i ++){
		scanf("%d%d%d%d", &v[i].x, &v[i].c, &v[i].s, &v[i].d);

		w[i].s = v[i].x-v[i].s, w[i].d = v[i].x+v[i].d, w[i].x = v[i].c;
	}

	sort(v+2, v+n+2, cmp);
	sort(w+2, w+n+2, cmp2);

//	memset(cnt, 0x3f, sizeof(cnt)); memset(d, 0x3f, sizeof(d));
	for (i = 1; i <= n; i ++)
		cnt[i] = d[i] = 20000000000LL;
	for (i = 2; i <= n+1; i ++){
		poz = n+1 - caut(w[i].s)+2;
		ind = n+1 - caut2(w[i].d)+1;

		val = find(poz) + w[i].x;

		//printf("la %d cu %d:\n", w[i].x-1, w[i].x);
		if (val < d[ind]){
			d[ind] = val;
			//printf("costul pentru %d este %d\n", n+1-ind+1-1, val);
			update(ind, val);
		}
	}

	printf("%lld\n", d[1]);

	return 0;
}