Cod sursa(job #659273)

Utilizator andrici_cezarAndrici Cezar andrici_cezar Data 10 ianuarie 2012 14:23:08
Problema Gropi Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <cstdio>
#include <algorithm>

using namespace std;
struct pct {long x; long y; long z;} a[100002];
struct pct2 {long x; long y;} st, fn, aux;
long N, K;
long i, j, t, nr, step;

bool compare (pct i, pct j) { return (i.y < j.y); }

int main() {
	freopen("gropi.in","r",stdin);
	freopen("gropi.out","w",stdout);

		scanf("%ld %ld", &N, &K);

		for (i = 1; i <= K; ++i)
			scanf("%ld %ld", &a[i].x, &a[i].y);
		
		sort (a, a+K+1, compare); 

		j = 1;
		for (i = 2; i <= K+1; ++i)
			if (a[j].x != a[i].x) {
				for (t = j; t < i; ++t)
					a[t].z = i - 1;
				j = i;
			}

		scanf("%ld", &t);

		while (t) {
			scanf("%ld %ld %ld %ld", &st.x, &st.y, &fn.x, &fn.y);

			if (st.y > fn.y)
				aux = st, st = fn, fn = aux;

			nr = fn.y - st.y + 1;

		    for (step = 1; step < K; step <<= 1);
		    for (j = 0; step; step >>= 1)
		        if (j + step < K && a[j + step].y <= st.y)
		           j += step;
		    ++j;
			if (a[j].x != st.x) j = a[j].z + 1;

			for (; j <= K && a[j].y <= fn.y;)
				++nr, st.x = 3 - st.x, j = a[j].z+1;
			
			if (st.x != fn.x)
				++nr;
			
			printf("%ld\n", nr);
			--t;
		}

	return 0;
}