Cod sursa(job #216722)

Utilizator FllorynMitu Florin Danut Flloryn Data 25 octombrie 2008 14:46:28
Problema Reconst Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.88 kb
    var a,b,s : array[1..100000] of longint;  
       use : array[1..2000] of longint;  
       i,j,n,m,ii,jj,nr,caz : longint;  
       f,g : text;  
       ok : boolean;  
   procedure work1(i : longint);  
   var j,mm : longint;  
   Begin  
     j:=i; mm:=m; ok:=false;  
     repeat  
         while a[j]=a[j+1] do inc(j);  
         ii:=i;  
             for jj:=ii+1 to j do  
               if b[ii]<>b[jj] then  
             begin  
               a[jj]:=b[ii]+1; ok:=true;  
               s[jj]:=s[jj]-s[ii];  
             end;  
       inc(j); i:=j;  
     until j>=mm;  
   End;  
     
   procedure sort1(l,r : longint);  
   var i,j,x,y,aux : longint;  
   begin  
        i:=l; j:=r; x:=a[(l+r)div 2];  
        y:=b[(l+r)div 2];  
        repeat  
              while (a[i]<x)or((a[i]=x)and(b[i]<y))  
               do inc(i);  
              while (x<a[j])or((x=a[j])and(y<b[j]))  
               do dec(j);  
              if i<=j then  
              begin  
                   aux:=a[i];a[i]:=a[j];a[j]:=aux;  
                   aux:=b[i];b[i]:=b[j];b[j]:=aux;  
                   aux:=s[i];s[i]:=s[j];s[j]:=aux;  
                   inc(i); dec(j);  
              end;  
        until i>j;  
        if l<j then sort1(l,j);  
        if i<r then sort1(i,r);  
   end;  
     
     
   begin  
     assign(f,'reconst.in');reset(f);  
     assign(g,'reconst.out');rewrite(g);  
     read(f,n,m);  
       for i:=1 to m do  
         read(f,a[i],b[i],s[i]);  
     i:=1; caz:=1;  
       repeat  
           sort1(1,m);  
           work1(i);  
       until not ok;  
       for i:=m downto 1 do  
       begin  
         use[a[i]]:=s[i];  
           for j:=a[i]+1 to b[i] do  
         use[a[i]]:=use[a[i]]-use[j];  
       end;  
     
     write(g,use[1]);  
       for i:=2 to n do write(g,' ',use[i]);  
     close(g);  
   end.