Cod sursa(job #239640)

Utilizator SleepyOverlordPatcas Csaba SleepyOverlord Data 5 ianuarie 2009 09:51:56
Problema Reconst Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.23 kb
#include <stdio.h>
#include <vector>
#include <algorithm>

#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 change()
{
	bool found=true;
	while (found)
	{
		found=false;
		sort(ALL(a));
		for (int q=1;q<n;++q)
			if (a[q].x==a[q-1].x)
			{
				found=true;
				if (a[q].y==a[q-1].y)
				{
					--n;
					a.erase(a.begin()+q);
					break;
				}
				xyz aux=a[q];
				aux.x=a[q-1].y+1;
				aux.z-=a[q-1].z;
				a.erase(a.begin()+q);
				a.PB(aux);
				break;
			}
	}
}

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;
}