Cod sursa(job #589030)

Utilizator mihaipopa12Popa Mihai mihaipopa12 Data 10 mai 2011 17:26:01
Problema Gropi Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.89 kb
#include<stdio.h>
#include<algorithm>

#define maxG 100005
#define same (x1 == x2 && y1 == y2)

using namespace std;

FILE*f=fopen("gropi.in","r");
FILE*g=fopen("gropi.out","w");

int n,m,i,V[maxG],z,rez,minim,T[maxG],Rez,A[3][maxG],a,b;
int Q,x1,y1,x2,y2,z1,z2;

struct groapa{
	int x;
	int y;
}G[maxG];

struct cmp{
	
	inline bool operator () ( const groapa &a , const groapa &b ){
		
		return a.y < b.y;
		
	}
	
};

inline int cb ( int y ){
	
	int p = 1, u = z; int m;
	
	while ( p <= u ){
		m = ( p + u ) >> 1;
		if ( V[m] == y ) return m;
		
		if ( V[m] < y )
			p = m + 1;
		else
			u = m - 1;
		
	}
	return u;	
}

inline bool obstacol ( int y1 , int y2 , int A[maxG] ){
	int p = 1 ; int u = A[0]; int mij;
	
	while ( p <= u ){
		mij = ( p + u ) >> 1;
		if ( A[mij] > y1 && A[mij] < y2 ) return 1;
		if ( A[mij] < y1 ) p = mij + 1;
		else
			u = mij - 1;
	}
	
	return 0;
}

int main () {
	
	fscanf(f,"%d %d",&n,&m);
	
	for ( i = 1 ; i <= m ; ++i ){
		fscanf(f,"%d %d",&G[i].x,&G[i].y);
		A[G[i].x][++A[G[i].x][0]] = G[i].y;
		if ( G[i].y < G[minim].y || !minim )
			minim = i;
	}
	
	++m; G[m].x = G[minim].x; G[m].y = 0;
	
	sort( G+1,G+m+1,cmp() ); sort(A[1]+1,A[1] + A[1][0] + 1); sort(A[2]+1,A[2] + A[2][0] + 1);
	
	for ( i = 1 ; i <= m ; ++i ){
		if ( G[i].x != G[i-1].x )
			V[++z] = G[i].y,T[z] = G[i].x;
	}
	
	
	fscanf(f,"%d",&Q);
	
	for ( i = 1 ; i <= Q ; ++i ){
		
		fscanf(f,"%d %d %d %d",&x1,&y1,&x2,&y2);
		
		if ( y1 > y2 ){
			int aux; 
			aux = x1; x1 = x2; x2 = aux;
			aux = y1; y1 = y2; y2 = aux;
		}
		
		z1 = cb(y1); z2 = cb(y2);
		
		Rez = z2 - z1 + y2 - y1 + 1;
		
		if ( !(z1 == z2 && !obstacol(y1,y2,A[T[z1]])) ){
			if ( x1 == T[z1] ) ++Rez;
			if ( x2 == T[z2] ) ++Rez;
		}
		else{
			if ( x1 != x2 ) ++Rez;
		}
		
		fprintf(g,"%d\n",Rez);
		
	}
	
	fclose(f);
	fclose(g);
	
	return 0;
}