Cod sursa(job #1716106)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 11 iunie 2016 22:59:48
Problema Stalpi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>

using namespace std;

ifstream fin("stalpi.in");
ofstream fout("stalpi.out");

const int dim = 100005;
const int inf = 0x3f3f3f3f;

int n;

struct Stalp {
	int cost, l, r;
} v[dim];

int x[dim], aib[dim];

constexpr int lsb(int x) {

	return (x & (-x));

}

void init(void) {

	memset(aib, 0x3f, sizeof aib);

}

void update(int pos, int val) {

	for (int i = pos; i; i -= lsb(i))
		aib[i] = min(aib[i], val);

}

int query(int pos) {

	int ret = inf;
	for (int i = pos; i <= n; i += lsb(i))
		ret = min(ret, aib[i]);

	return ret;

}

int main() {
		
	fin >> n;
	for (int i = 1; i <= n; ++i) {

		fin >> x[i] >> v[i].cost >> v[i].l >> v[i].r;

		v[i].l = x[i] - v[i].l;
		v[i].r = x[i] + v[i].r;
		
	}
	
	init();
	sort(x + 1, x + n + 1);

	sort(v + 1, v + n + 1,
	[](const Stalp& a, const Stalp& b) {
		return a.r < b.r;
	});

	for (int i = 1; i <= n; ++i) {

		int pos = lower_bound(x + 1, x + n + 1, v[i].l) - x - 1;
		
		int temp = 0;
		if (pos)
			temp = query(pos);

		temp += v[i].cost;
		
		pos = upper_bound(x + 1, x + n + 1, v[i].r) - x - 1;
		update(pos, temp);

	}

	fout << aib[n];

	return 0;

}

//Trust me, I'm the Doctor!