Cod sursa(job #577201)

Utilizator andrici_cezarAndrici Cezar andrici_cezar Data 9 aprilie 2011 20:42:56
Problema Gropi Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#include<stdio.h>

struct pct{long x; long y;} p1, p2, aux;
long i, j, col, gr, xx, n,yy, nr, a[5][200001];

int main()
{
	freopen("gropi.in","r",stdin);
	freopen("gropi.out","w",stdout);
	
		scanf("%ld %ld", &col, &gr);
		
		for (i = 1; i <= gr; i ++)
		{
			scanf("%ld %ld", &xx, &yy);
			a[xx][yy] = 1;
		}
		
		scanf("%ld", &n);
		
		for (i = 1; i <= n; i ++)
		{
			scanf("%ld %ld %ld %ld", &p1.x, &p1.y, &p2.x, &p2.y);
			if (p2.y < p1.y) aux = p1, p1 = p2, p2 = aux;
			
			nr = 1;
			
			while (p1.y < p2.y)
			{
				while (a[p1.x][p1.y] == 0 && p1.y < p2.y) {p1.y ++; nr ++;}
				if (a[p1.x][p1.y] == 1)
				{
					nr ++;
					p1.x = 3 - p1.x;
				}
			}
			if (p1.x != p2.x) nr++;
			
			printf("%ld\n", nr);
		}
			
	return 0;
}