Pagini recente » Cod sursa (job #2873881) | Cod sursa (job #345993) | Cod sursa (job #82311) | Cod sursa (job #1779319) | Cod sursa (job #565708)
Cod sursa(job #565708)
# include <fstream>
# include <iostream>
# include <algorithm>
# include <set>
# include <vector>
# define DIM 100003
using namespace std;
struct nod{
int x, y;
friend bool operator < (const nod &a, const nod &b){
if (a.y<b.y)return 1;
return 0;
}
}v[DIM];
int c, n, m, d[DIM];
void calc()
{
sort(v+1,v+n+1);
d[1]=0;
for(int i=2;i<=n;++i)
{
d[i]=d[i-1]+v[i].y-v[i-1].y;
if (v[i-1].x!=v[i].x)
++d[i];
}
}
int cauta (int st, int dr, int p)
{
if (st==dr)
return st;
int mij=(st+dr)/2;
if (v[mij].y>=p)return cauta(st, mij, p);
else return cauta(mij+1, dr, p);
}
int dist (int a, int b, int x, int y)
{
int dst, i, j;
i=cauta(1, n+1, b);
j=cauta(1, n, y);
if (v[i].y==b)++i;
if (v[j].y>=y)--j;
if (y==b)
dst=1;
else if (j==0 || i==n+1)
{
dst=y-b+1;
if (a!=x)++dst;
}
else
{
dst=v[i].y-b+1;
if (v[i].x==a)++dst;
dst+=y-v[j].y;
if (v[j].x==x)++dst;
dst+=d[j]-d[i];
}
return dst;
}
int main()
{
ifstream fin ("gropi.in");
freopen("gropi.out", "w", stdout);
fin>>c>>n;
int a, b, x, y;
for(int i=1;i<=n;++i)
fin>>v[i].x>>v[i].y;
calc();
fin>>m;
for(;m--;)
{
fin>>a>>b>>x>>y;
if (b<y)
printf("%d\n", dist(a, b, x, y));
else
printf("%d\n", dist(x, y, a, b));
}
return 0;
}