Cod sursa(job #197649)

Utilizator Omega91Nicodei Eduard Omega91 Data 5 iulie 2008 13:14:22
Problema Gropi Scor 0
Compilator cpp Status done
Runda Junior Challenge 2008 Marime 1.33 kb
#include <cstdio>
#define DIMENS 660000
void swap(int &a, int &b)
{
	int aux;
	aux = a;
	a = b;
	b = aux;
}

int main()
{
	int N, C, M, i, x1, x2, y1, y2, rasp, cel;
	short timp[DIMENS];
	bool gropi[2][DIMENS], drum[2][DIMENS], vdir[DIMENS], dir;
	FILE *f1, *f2;
	f1 = fopen("gropi.in", "r");
	f2 = fopen("gropi.out", "w");
	fscanf(f1, "%d %d", &C, &N);
	for (i = 1; i <= N; ++i) {
		fscanf(f1, "%d %d", &x1, &y1);
		gropi[x1 - 1][y1] = true;
	}

	timp[1] = 1;
	if ( gropi[1][1] ) {
		drum[2][1] = true;
		vdir[1] = dir = 1;
	}
	else {
		drum[1][1] = true;
		vdir[1] = dir = 0;
	}
	for (cel = 2; cel <= C; ++cel) {
		if (!gropi[dir][cel]) {
			drum[dir][cel] = true;
			timp[cel] = timp[cel - 1] + 1;
		}
		else {
			dir = !dir;
			drum[dir][cel] = drum[dir][cel - 1] = true;
			timp[cel] = timp[cel - 1] + 2;
		}
		vdir[cel] = dir;
	}
	fscanf(f1, "%d", &M);
	for (i = 1; i <= M; ++i) {
		fscanf(f1, "%d %d %d %d", &x1, &y1, &x2, &y2);
		if (y1 > y2) { swap(x1, x2); swap(y1, y2); }
		else if (y1 == y2 && x1 > x2) swap(x1, x2);
		else if (y1 == y2 && x1 == x2) {
			rasp = 0;
			fprintf(f2, "%d\n", rasp);
			continue;
		}
		x1--;
		x2--;
		rasp = timp[y2] - timp[y1] + 1;
		if ( vdir[y1] != x1 ) {
			if (drum[x1]) rasp--;
			else rasp++;
		}
		if ( vdir[y2] != x2 ) {
			rasp++;
		}
		fprintf(f2, "%d\n", rasp);
	}

	fclose(f1);
	fclose(f2);
	return 0;
}