Cod sursa(job #239658)

Utilizator SleepyOverlordPatcas Csaba SleepyOverlord Data 5 ianuarie 2009 10:58:48
Problema Reconst Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.74 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 change()
{
	//sort(ALL(a));
	//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;
	//	int limit=m;
	//	for (int q=1;q<limit;++q)
	//		if (q<limit && a[q].x==a[q-1].x)
	//		{
	//			found=true;
	//			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);*/
	//			a[q]=aux;
	//			//--limit;
	//			break;
	//		}
	//	if (found) sort(ALL(a));
	//}
		bool found=true;  
		while (found)  
		{  
			found=false;  
			sort(ALL(a));  
			for (int q=1;q<m;++q)  
				if (a[q].x==a[q-1].x)  
				{  
					found=true;  
					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;
}