Cod sursa(job #201066)

Utilizator andrei.12Andrei Parvu andrei.12 Data 28 iulie 2008 23:03:40
Problema Stalpi Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.54 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];

inline int cmp(ches a, ches b){
	return a.x < b.x;
}
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);

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

//	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(v[i].x - v[i].s)+2;
		ind = n+1 - caut2(v[i].x + v[i].d)+1;

		val = find(poz) + v[i].c;

		//printf("la %d cu %d:\n", i-1, v[i].c);
		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;
}