Pagini recente » Cod sursa (job #1013402) | Cod sursa (job #600959) | Cod sursa (job #3177755) | Cod sursa (job #546178) | Cod sursa (job #763395)
Cod sursa(job #763395)
#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==End )
{
G<<"1\n";
continue;
}
if ( Start.col == End.col )
{
G<<"2\n";
continue;
}
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);
G<<End.col-Start.col+1+Port[p2-1]-Port[p1-1] + ( Start.lin == Gropi[p1].lin ) + ( End.lin == Gropi[p2].lin ) <<'\n';
}
}