Cod sursa(job #763401)

Utilizator danalex97Dan H Alexandru danalex97 Data 2 iulie 2012 10:23:19
Problema Gropi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.81 kb
// 100 p
# include <algorithm>
# include <cstdio>

# define x first
# define y second

# define mp std :: make_pair
# define ub std :: upper_bound

const char *FIN = "gropi.in";
const char *FOU = "gropi.out";

const int MAX = 100005 ;

std :: pair < int, int > V[MAX], X, Y ;
int dp[MAX] ;
int N, C, T ;

int main ( void ) 
{
    freopen ( FIN, "r", stdin ) ;
    freopen ( FOU, "w", stdout ) ;

    scanf ( "%d %d", &C, &N ) ;
    for ( int i = 0; i < N; ++i ) 
        scanf ( "%d %d", &V[i].y, &V[i].x ) ;
	
    V[N++] = mp ( C + 5, 0 ), std :: sort ( V, V + N ) ;
    dp[0] = V[0].y != V[1].y ;
    
	for ( int i = 1; i + 1 < N; ++i ) 
        dp[i] = ( V[i].y != V[i + 1].y ) + dp[i - 1] ;

    for ( scanf ( "%d", &T ) ; T ; --T ) 
	{
        scanf ( "%d %d %d %d", &X.y, &X.x, &Y.y, &Y.x ) ;
		if ( Y < X ) std :: swap ( X, Y ) ;
		
        int A = ub ( V, V + N, mp ( X.x, 0 ) ) - V ;
		
        if ( Y.x <= V[A].x ) 
            printf ( "%d\n", Y.x - X.x + 1 + ( X.y != Y.y ) ) ;
		else
		{
            int B = ub ( V, V + N, mp ( Y.x, 0 ) ) - V - 1 ;
            printf ( "%d\n", Y.x - X.x + 1 + dp[B - 1] - dp[A - 1] + (X.y == V[A].y) + (Y.y == V[B].y) ) ;
        }
    }
}

/* // 90p
#include <fstream>
#include <algorithm>
using namespace std;

ifstream F("gropi.in");
ofstream G("gropi.out");

typedef pair<int,int> Pair;

#define Ng 100011
#define col first 
#define lin second 
#define swap(a,b) ( a ^= b ^= a ^= b )

int N,M,Q;
Pair Gropi[Ng];
int Port[Ng];
Pair Start,End;
 
int Find_next(int Val,int St,int Dr)
{
	if ( St==Dr ) return St;
	if ( Dr-St==1 )
	{
		if ( Gropi[St].col > Val ) return St;
		return Dr;
	}
	
	int Mid=(St+Dr) /2;
	if ( Gropi[Mid].col > Val )
		return Find_next(Val,St,Mid);
	else
		return Find_next(Val,Mid+1,Dr);
}

int Find_last(int Val,int St,int Dr)
{
	if ( St==Dr ) return St;
	if ( Dr-St==1 )
	{
		if ( Gropi[Dr].col < Val ) return Dr;
		return St;
	}
	
	int Mid=(St+Dr) /2;
	if ( Gropi[Mid].col > Val )
		return Find_last(Val,St,Mid-1);
	else
		return Find_last(Val,Mid,Dr);
}

int main()
{
	F>>N>>M;
	for (int i=1;i<=M;++i)
		F>>Gropi[i].lin>>Gropi[i].col;
	sort(Gropi,Gropi+M+1);
	
	for (int i=1;i<=M;++i)
		Port[i] = Port[i-1] + (Gropi[i].lin != Gropi[i+1].lin);
	
	for (F>>Q;Q;--Q)
	{
		F>>Start.lin>>Start.col;
		F>>End.lin>>End.col;
		
		if ( Start.col > End.col )
		{
			swap(Start.col,End.col);
			swap(Start.lin,End.lin);
		}			
			
		int p1=Find_next(Start.col,1,M);
		int p2=Find_last(End.col,1,M);
			
		if ( Gropi[p1].col >= End.col )
			G<< End.col - Start.col + 1 
			+ (Start.lin != End.lin)<<'\n';
		else
			G<<  
				End.col - Start.col + 1 + 
				Port[p2-1]-Port[p1-1] + 
				( Start.lin == Gropi[p1].lin ) + 
				( End.lin == Gropi[p2].lin ) 
			<<'\n';
	}
}
*/