Cod sursa(job #866770)

Utilizator DaNutZ2UuUUBB Bora Dan DaNutZ2UuU Data 28 ianuarie 2013 19:00:52
Problema Stalpi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.23 kb
#include <fstream>
#include <algorithm>
#include <cstring>

using namespace std;

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

struct stalp
{
    int s, d, c;
}
t[N];

const int N = 100005;
long long inf = 10000000000000LL;
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;
}