Cod sursa(job #253358)

Utilizator 630r63Ilinca George Mihai 630r63 Data 5 februarie 2009 18:23:21
Problema Stalpi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.25 kb
 #include<stdio.h>  
 #include<algorithm>  
   
 using namespace std;  
   
 #define lg 100005  
 #define inf 0x3f3f3f3f  
   
 int n, i, ind, poz;  
 long long cnt[lg], d[lg], val;  
 struct ches{  
     int x, s, d, c;  
 };  
 ches v[lg], w[lg];  
   
 inline int cmp(ches a, ches b){  
     return a.x < b.x;  
 }  
 inline int cmp2(ches a, ches b){  
     if (a.s != b.s)  
         return a.s < b.s;  
     return a.d < b.d;  
 }  
 int caut(int val){  
     int li = 2, ls = n+1, x, gs = -1;  
   
     while (li <= ls){  
         x = (li+ls) / 2;  
   
         if (v[x].x >= val){  
             gs = x;  
             ls = x-1;  
         }  
         else  
             li = x+1;  
     }  
   
     return gs;  
 }  
 int caut2(int val){  
     int li = 2, ls = n+1, x, gs = -1;  
   
     while (li <= ls){  
         x = (li+ls) / 2;  
   
         if (v[x].x <= val){  
             gs = x;  
             li = x+1;  
         }  
         else  
             ls = x-1;  
     }  
   
     return gs;  
 }  
 void update(int poz, long long val){  
     while (poz <= n+1){  
         cnt[poz] = min(cnt[poz], val);  
   
         poz += (poz^(poz-1)) & poz;  
     }  
 }  
 int find(int poz){  
     long long gs = 20000000000LL;  
     while (poz > 0){  
         gs = min(gs, cnt[poz]);  
   
        poz -= (poz^(poz-1)) & poz;  
     }  
   
     return gs;  
 }  
   
 int main()  
 {  
     freopen("stalpi.in", "rt", stdin);  
     freopen("stalpi.out", "wt", stdout);  
   
     scanf("%d", &n);  
     for (i = 2; i <= n+1; i ++){  
         scanf("%d%d%d%d", &v[i].x, &v[i].c, &v[i].s, &v[i].d);  
   
         w[i].s = v[i].x-v[i].s, w[i].d = v[i].x+v[i].d, w[i].x = v[i].c;  
     }  
   
     sort(v+2, v+n+2, cmp);  
     sort(w+2, w+n+2, cmp2);  
  

     for (i = 1; i <= n; i ++)  
         cnt[i] = d[i] = 20000000000LL;  
     for (i = 2; i <= n+1; i ++){  
         poz = n+1 - caut(w[i].s)+2;  
         ind = n+1 - caut2(w[i].d)+1;  
   
         val = find(poz) + w[i].x;  
   

         if (val < d[ind]){  
             d[ind] = val;  

             update(ind, val);  
         }  
     }  
   
     printf("%lld\n", d[1]);  
   
     return 0;  
}