Cod sursa(job #285229)

Utilizator raduzerRadu Zernoveanu raduzer Data 22 martie 2009 14:07:41
Problema Stalpi Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

const int MAX_N = 100010;
const int INF = 0x3f3f3f3f;

struct stalp {
	int x, cost, st, dr;
};

int n;
stalp a[MAX_N];
int aib[MAX_N], d[MAX_N];

int cmp(stalp a, stalp b)
{
	return a.x < b.x;
}

inline void checkMin(int &a, int b)
{
	if (a > b) a = b;
}

int binsearch(int x)
{
	int mij, st = 1, dr = n;
	
	while (st < dr)
	{
		mij = st + ((dr - st) >> 1);
		
		if (x < a[mij].x) dr = mij;
		else st = mij + 1;
	}
	
	if (x >= a[st].x) return 0;
	return st;
}

int binsearch2(int x)
{
	int mij, st = 1, dr = n;
	
	while (st < dr)
	{
		mij = st + ((dr - st) >> 1);
		
		if (x <= a[mij].x) dr = mij;
		else st = mij + 1;
	}
	
	return st;
}

void update(int poz, int val)
{
	while (poz <= n)
	{
		checkMin(aib[poz], val);
		poz += (poz ^ (poz - 1)) & poz;
	}
}

int query(int val)
{
	int ret = INF;
	
	while (val)
	{
		checkMin(ret, aib[val]);
		val &= val - 1;
	}
	
	return ret;
}

int main()
{
	int i;
	freopen("stalpi.in", "r", stdin);
	freopen("stalpi.out", "w", stdout);
	
	scanf("%d", &n);
	for (i = 1; i <= n; ++i)
		scanf("%d %d %d %d", &a[i].x, &a[i].cost, &a[i].st, &a[i].dr);
		
	sort(a + 1, a + n + 1, cmp);
	
	memset(aib, INF, sizeof(aib));
	
	for (i = n; i; --i)
	{
		int poz = binsearch(a[i].x + a[i].dr);
		
		if (!poz) d[i] = a[i].cost;
		else d[i] = query(poz) + a[i].cost;
		
		update(binsearch2(a[i].x - a[i].st), d[i]);
	}
	
	int mn = INF;
	
	for (i = 1; i <= n; ++i)
		if (a[i].x - a[i].st <= a[1].x) checkMin(mn, d[i]);
		
	printf("%d\n", mn);
}