Cod sursa(job #197201)

Utilizator wefgefAndrei Grigorean wefgef Data 2 iulie 2008 19:40:33
Problema Reconst Scor Ascuns
Compilator cpp Status done
Runda Marime 3.16 kb
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;

#define all(c) (c).begin(), (c).end()
#define pb push_back
#define sz(c) int((c).size())
#define mp make_pair
#define x first
#define y second

const char FILEIN[] = "reconst.in";
const char FILEOUT[] = "reconst.out";
const int MAX_N = 2048;
const int MAX_M = 2048;

int N, M;
vector< pair<int, int> > Interv[MAX_N], Interv2[MAX_N];
int V[MAX_N];
bool viz[MAX_N];

void ReadData() {
	ifstream fin(FILEIN);

	fin >> N >> M;
	for (int i = 0; i < M; ++i) {
		int A, B, S;
		fin >> A >> B >> S;
		Interv[A].pb(mp(B, S));
	}
}

inline int cmp(pair<int, int> a, pair<int, int> b) {
	return a.x > b.x;
}

void Solve() {
	for (int i = 0; i < N; ++i) {
		sort(all(Interv[i]));
		for (int j = 1; j < sz(Interv[i]); ++j)
			if (Interv[i][j] != Interv[i][j-1])
				Interv[ Interv[i][j-1].x+1 ].pb(mp(Interv[i][j].x, Interv[i][j].y - Interv[i][j-1].y));
	}

	for (int i = 0; i < N; ++i)
		if (!Interv[i].empty())
			Interv2[ Interv[i][0].x ].pb(mp(i, Interv[i][0].y));

	for (int i = N-1; i >= 0; --i) {
		sort(all(Interv2[i]), cmp);
		for (int j = 1; j < sz(Interv2[i]); ++j)
			if (Interv2[i][j] != Interv2[i][j-1])
				Interv2[ Interv2[i][j-1].x-1 ].pb(mp(Interv2[i][j].x, Interv2[i][j].y - Interv2[i][j-1].y));
	}

	vector< pair<int, pair<int, int> > > NewInterv;
	for (int i = 0; i < N; ++i)
		if (!Interv2[i].empty())
			NewInterv.pb(mp(i-Interv2[i][0].x, mp(Interv2[i][0].x, Interv2[i][0].y)));

	sort(all(NewInterv));

	for (int i = 0; i < sz(NewInterv); ++i) {
		int A = NewInterv[i].y.x;
		int B = A + NewInterv[i].x;
		int S = NewInterv[i].y.y;

		for (int j = A; j <= B; ++j)
			if (viz[j])
				S -= V[j];
		for (int j = A; j <= B; ++j)
			if (!viz[j]) {
				V[j] = S;
				break;
			}
		for (int j = A; j <= B; ++j)
			viz[j] = 1;
	}
}

void WriteData() {
	ofstream fout(FILEOUT);

	for (int i = 0; i < N; ++i)
		fout << V[i] << " ";
}

int main() {
	ReadData();
	Solve();
	WriteData();
}

/*
Steve walks warily down the street,
With the brim pulled way down low
Aint no sound but the sound of his feet,
Machine guns ready to go
Are you ready, are you ready for this
Are you hanging on the edge of your seat
Out of the doorway the bullets rip
To the sound of the beat

Another one bites the dust
Another one bites the dust
And another one gone, and another one gone
Another one bites the dust
Hey, Im gonna get you too
Another one bites the dust

How do you think Im going to get along,
Without you, when youre gone
You took me for everything that I had,
And kicked me out on my own

Are you happy, are you satisfied
How long can you stand the heat
Out of the doorway the bullets rip
To the sound of the beat

Chorus

Another one bites the dust
Another one bites the dust
Another one bites the dust
Another one bites the dust
There are plenty of ways you can hurt a man
And bring him to the ground
You can beat him
You can cheat him
You can treat him bad and leave him
When hes down
But Im ready, yes Im ready for you
Im standing on my own two feet
Out of the doorway the bullets rip
Repeating the sound of the beat
*/