Cod sursa(job #323514)

Utilizator Magnuscont cu nume gresit sau fals Magnus Data 12 iunie 2009 15:07:39
Problema Reconst Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.65 kb
#include <stdio.h>   
#include <vector>   
#include <algorithm>   
#include <assert.h>   
  
#define PB push_back   
#define ALL(x) x.begin(),x.end()   
  
using namespace std;   
  
struct xyz   
{   
    int x,y,z;   
};   
  
int n,m,ind;   
vector <int> sol;   
vector <xyz> a;   
  
bool operator< (const xyz &a, const xyz &b)   
{   
    return a.x<b.x || a.x==b.x && a.y<b.y;   
}   
  
void solve()   
{   
    sol.resize(n+1);   
    for (int q=m-1;q>=0;--q)   
    {   
        sol[a[q].x]=a[q].z;   
        for (int w=a[q].x+1;w<=a[q].y;++w) sol[a[q].x]-=sol[w];   
    }   
}   
  
void radix()   
{   
    vector <int> count(n+1);   
    vector <xyz> b(m);   
    for (int q=0;q<m;++q) ++count[a[q].y];   
    for (int q=1;q<=n;++q) count[q]+=count[q-1];   
    for (int q=0;q<m;++q)   
    {   
        b[count[a[q].y-1]]=a[q];   
        ++count[a[q].y-1];   
    }   
    a=b;   
    fill(ALL(count),0);   
    for (int q=0;q<m;++q) ++count[a[q].x];   
    for (int q=1;q<=n;++q) count[q]+=count[q-1];   
    for (int q=0;q<m;++q)   
    {   
        b[count[a[q].x-1]]=a[q];   
        ++count[a[q].x-1];   
    }   
    a=b;   
}   
  
void change()   
{   
    //sort(ALL(a));   
    radix();   
    for (int q=1;q<m;++q)   
        while (q<m && a[q].x==a[q-1].x && a[q].y==a[q-1].y)   
        {   
            --m;   
            a.erase(a.begin()+q);   
        }   
    bool found=true;   
    while (found)   
    {   
        found=false;   
        for (int q=1;q<m;++q)   
            if (a[q].x==a[q-1].x && a[q].y>a[q-1].y)   
            {   
                found=true;   
                xyz aux=a[q];   
                aux.x=a[q-1].y+1;   
                aux.z-=a[q-1].z;   
                a[q]=aux;   
            }   
        if (found)    
        {   
            //sort(ALL(a));   
            radix();   
            for (int q=1;q<m;++q)   
                while (q<m && a[q].x==a[q-1].x && a[q].y==a[q-1].y)   
                {   
                    --m;   
                    a.erase(a.begin()+q);   
                }   
        }   
    }   
}   
  
int main()   
{   
    FILE* fin=fopen("reconst.in","r");   
    fscanf(fin,"%d%d\n",&n,&m);   
  
    for (int q=1;q<=m;++q)   
    {   
        int x,y,z;   
        fscanf(fin,"%d%d%d",&x,&y,&z);   
        xyz aux;   
        aux.x=x; aux.y=y; aux.z=z;   
        a.PB(aux);   
    }   
    fclose(fin);   
  
    change();   
    solve();   
  
    FILE* fout=fopen("reconst.out","w");   
    for (int q=1;q<=n;++q) fprintf(fout,"%d ",sol[q]);   
    fclose(fout);   
    return 0;   
}