Cod sursa(job #659233)

Utilizator andrici_cezarAndrici Cezar andrici_cezar Data 10 ianuarie 2012 13:50:36
Problema Gropi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <cstdio>
#include <algorithm>

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

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

int binary_search(int val)
{
    int i, step;
    for (step = 1; step < K; step <<= 1);
    for (i = 0; step; step >>= 1)
        if (i + step < K && a[i + step].y <= val)
           i += step;
    return i;
}


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; ++i)
			if (a[j].x == a[i].x) a[j].z = i;
			else {
				for (t = j+1; 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 (i = binary_search(st.y)+1; i <= K && a[i].y <= fn.y; ++i)
					if (a[i].x == st.x)
						++nr, st.x = 3 - st.x, i = a[i].z;
			
			if (st.x != fn.x)
				++nr;
			
			printf("%ld\n", nr);
			--t;
		}

	return 0;
}