Pagini recente » Cod sursa (job #2636472) | Cod sursa (job #2903145) | Cod sursa (job #1608121) | Cod sursa (job #2152317) | Cod sursa (job #197812)
Cod sursa(job #197812)
#include<stdio.h>
#include<algorithm>
using namespace std;
#define N 100010
int c,n,cate=-1,m;
struct abc
{
int x,y;
};
abc total[N],t[N];
bool compar(const abc &x,const abc &y)
{
if(x.y<y.y)
return true;
return false;
}
void preluc()
{
int i,ant=0;
for(i=1; i<=n; i++)
{
if(t[i].x!=ant)
{
total[++cate].x=ant;
total[cate].y=t[i].y-1;
ant=t[i].x;
}
}
total[++cate].x=ant;
total[cate].y=c;
total[0].x=total[0].y=total[++cate].x=0;
total[cate].y=c+1;
cate--;
}
int caut(int y)
{
int p=1,u=cate,mij;
while(p<u)
{
mij=(p+u)>>1;
if(y<=total[mij].y)
u=mij;
else
p=mij+1;
}
if(p>cate)
p=cate;
if(total[p].y<y)
p++;
return p;
}
int caut1(int y)
{
int p=1,u=n,mij;
while(p<u)
{
mij=(p+u)>>1;
if(y<=t[mij].y)
u=mij;
else
p=mij+1;
}
if(p>n)
p=n;
return p;
}
void rezolvas(int x1,int y1,int x2,int y2,int poz)
{
int pasi=0,aux;
bool exist;
if((x1!=total[poz].x)&&(x2!=total[poz].x))
{
pasi=y2-y1;
if(pasi<0)
pasi=-pasi;
pasi++;
}
else
{
if(y1<y2)
{
aux=caut(y2);
while((t[aux].y>y2)&&(aux>1))
aux--;
if((t[aux].y>y1)&&(t[aux].y<y2))
exist=true;
else
exist=false;
pasi=y2-y1+1;
if(exist)
{
if(x1==total[poz].x)
pasi++;
if(x2==total[poz].x)
pasi++;
}
}
else
{
aux=caut(y1);
while((t[aux].y>y1)&&(aux>1))
aux--;
if((t[aux].y>y2)&&(t[aux].y<y1))
exist=true;
else
exist=false;
pasi=y1-y2+1;
if(exist)
{
if(x1==total[poz].x)
pasi++;
if(x2==total[poz].x)
pasi++;
}
}
}
printf("%d\n",pasi);
}
void rezolva()
{
int x1,y1,x2,y2,poz1,poz2,pasi=0,aux;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(y1==y2)
{
if(x1==x2)
{
printf("0\n");
return;
}
printf("2\n");
return;
}
poz1=caut(y1);
if(poz1==0)
poz1=1;
if(poz1>cate)
poz1=cate;
poz2=caut(y2);
if(poz2==0)
poz2=1;
if(poz2>cate)
poz2=cate;
if(poz1==poz2)
{
rezolvas(x1,y1,x2,y2,poz1);
return;
}
if(x1==total[poz1].x)
pasi++;
if(x2==total[poz2].x)
pasi++;
aux=y2-y1;
if(aux<0)
aux=-aux;
pasi+=aux+1;
aux=poz1-poz2;
if(aux<0)
aux=-aux;
pasi+=aux;
printf("%d\n",pasi);
}
void citire()
{
int i;
scanf("%d%d",&c,&n);
for(i=1; i<=n; i++)
scanf("%d%d",&t[i].x,&t[i].y);
sort(t+1,t+n+1,compar);
preluc();
scanf("%d",&m);
for(i=0; i<m; i++)
rezolva();
}
int main()
{
freopen("gropi.in","r",stdin);
freopen("gropi.out","w",stdout);
citire();
return 0;
}