Pagini recente » Cod sursa (job #493722) | Cod sursa (job #2691825) | Cod sursa (job #2822215) | Cod sursa (job #2838025) | Cod sursa (job #2441873)
#include <fstream>
#include <algorithm>
#include <cstring>
using namespace std;
ifstream fin("stalpi.in");
ofstream fout("stalpi.out");
const int N = 100005;
long long inf = 10000000000000LL;
struct stalp
{
int s, d, c;
}
t[N];
int x[N], n;
long long a[N];
int cmp(stalp a,stalp b)
{
return a.d < b.d;
}
int querry(int p)
{
long long r = inf;
for( ; p <= n; p+=((p ^ (p - 1)) & p))
r = min(r, a[p]);
return r;
}
void update(int p,long long c)
{
for( ;p ;p -= ((p ^ (p - 1)) & p))
a[p] = min(a[p], c);
}
int main()
{
fin >> n;
int i, p;
long long c;
for(i = 1; i <= n; i++)
{
fin >> x[i] >> t[i].c >> t[i].s >> t[i].d;
a[i] = inf;
t[i].s = x[i] - t[i].s;
t[i].d = x[i] + t[i].d;
}
sort(x + 1,x + n + 1);
sort(t + 1,t + n + 1, cmp);
for(i = 1; i <= n; i++)
{
p = lower_bound(x + 1, x + n + 1, t[i].s) - x - 1;
if(p == 0)
c = 0;
else
c = querry(p);
c += t[i].c;
p = upper_bound(x + 1, x + n + 1, t[i].d) - x - 1;
update(p,c);
}
fout << a[n] << '\n';
fin.close();
fout.close();
return 0;
}