Pagini recente » Cod sursa (job #2845924) | Cod sursa (job #168323) | Cod sursa (job #221926) | Cod sursa (job #1714415) | Cod sursa (job #285229)
Cod sursa(job #285229)
#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);
}