Cod sursa(job #1988758)

Utilizator mariusn01Marius Nicoli mariusn01 Data 4 iunie 2017 16:42:20
Problema Reconst Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.32 kb
#include <fstream>
#include <iostream>
#include <algorithm>
#include <set>
#define st first.first
#define dr first.second
#define cost second
#define DIM 4010

using namespace std;

typedef pair< pair<int, int>, int > nod;
/*
struct nod {
    int st;
    int dr;
    int cost;
};
*/

nod v[DIM], crt, aux;
int a[DIM];
int n, m, i, j, ST, DR, COST, nextST, nextDR, nextCOST;

set<nod> s;

void afTri(int a, int b, int c) {
    cout<<"("<<a<<" "<<b<<" "<<c<<")\n";
}

void afSet(set<nod> s) {
    for(set<nod>::iterator it = s.begin(); it != s.end(); it++){
        cout<<"("<<it->st<<" "<<it->dr<<" "<<it->cost<<") ";
    }
    cout<<"\n\n";
}

/*
int cmp(nod a, nod b) {
    if (a.st == b.st)
        return a.dr <= b.dr;
    else
        return a.st <= b.st;
}
*/
int main () {
    ifstream fin ("reconst.in");
    ofstream fout("reconst.out");

    fin>>n>>m;
    for (i=1;i<=m;i++) {
        fin>>v[i].st>>v[i].dr>>v[i].cost;
        if (v[i].st > v[i].dr) {
            swap(v[i].st, v[i].dr);
        }
    }

    sort(v+1, v+m+1);

    s.insert(v[m]);
    for (int i=m-1;i>=1;i--) {
        crt = v[i];
        //afTri(crt.st, crt.dr, crt.cost);

        for (;;) {
            crt.st--;
            set<nod>::iterator it = s.upper_bound(crt);
            crt.st++;

            set<nod>::iterator it1 = it;
            it1 ++;
            if (it1 != s.end() && it1->st == crt.st)
                it++;

            if (it == s.end()) {
                s.insert(crt);
                //afSet(s);
                break;
            } else {
                if (it->st == crt.st && it->dr == crt.dr)
                    break;
                else
                    if (it->st != crt.st) {
                        s.insert(crt);
                        //afSet(s);
                        break;
                    }else
                        if (crt.dr < it->dr) {

                            nextST = crt.dr+1;
                            nextDR = it->dr;
                            nextCOST = it->cost - crt.cost;

                            aux.st = it->st;
                            aux.dr = crt.dr;
                            aux.cost = crt.cost;

                            //it->dr = crt.dr;
                            //it->cost = crt.cost;
                            s.erase(it);
                            s.insert(aux);

                            crt.st = nextST;
                            crt.dr = nextDR;
                            crt.cost = nextCOST;

                            if (crt.st > n)
                                break;


                        } else {

                            crt.st = it->dr+1;
                            crt.cost = crt.cost - it->cost;
                            if (crt.st > n)
                                break;

                        }
            }

        }

    }

    for(set<nod>::iterator it = s.begin(); it != s.end(); it++){
        m++;
        v[m].st = it->st;
        v[m].dr = it->dr;
        v[m].cost = it->cost;
    }

    for (i=m;i>=1;i--) {
        int suma = 0;
        for (j=v[i].st;j<=v[i].dr;j++)
            suma += a[j];
        a[ v[i].st ] += (v[i].cost - suma);
    }

    for (i=1;i<=n;i++)
        fout<<a[i]<<" ";

    return 0;
}