Cod sursa(job #197984)

Utilizator andrei-alphaAndrei-Bogdan Antonescu andrei-alpha Data 7 iulie 2008 16:23:28
Problema Gropi Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <cstdio>
#include <algorithm>
#include <vector>
#define IN "gropi.in"
#define OUT "gropi.out"
#define N_MAX 100001
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define FOR(i,a,b) for(int i=a;i<=b;++i)

using namespace std;

vector <pair <int,int> > v(N_MAX);
int N,M,C;
int x1,x2,y1,y2;

void scan()
{
	freopen(IN, "r",stdin);
	freopen(OUT, "w",stdout);
	scanf("%d%d\n", &C,&N);
	FOR(i,1,N)
		scanf("%d%d", &v[i].s,&v[i].f);
	v.resize(N);
	sort(v.begin()+1, v.end()+1 );
	scanf("%d", &M);
}

inline int caut_bin()
{
	FOR(m,1,N)
		if(v[m].f > y1 && v[m].f < y2)
			return m;
	return N+1;	
}

void solve()
{
	scanf("%d%d%d%d", &x1,&y1,&x2,&y2);
	if( y1 > y2 )
		swap(y1,y2),swap(x1,x2);
	int rez = 0;
	int poz = caut_bin();
	int line = x1;
	
	FOR(i,poz,N)
	{
		if(v[i].s == line)
			++line,++rez;
		if(line == 3)
			line = 1;
		
		if(v[i+1].f > y2)
			break;
	}
	if(line != x2)
		++rez;
	rez += y2-y1+1;
	
	printf("%d\n",rez);
}

int main()
{
	scan();
	FOR(ti,1,M)
		solve();
	return 0;
}