Pagini recente » Cod sursa (job #930180) | Cod sursa (job #741539) | Diferente pentru autumn-warmup-2007/solutii/runda-2 intre reviziile 56 si 16 | Istoria paginii jc2021/solutii/perrynator | Cod sursa (job #1716106)
#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!