Pagini recente » Statistici Tudor Buhnia (tudorbuhnia) | Cod sursa (job #1537271) | Cod sursa (job #1498359) | Cod sursa (job #2671226) | Cod sursa (job #285489)
Cod sursa(job #285489)
#include<fstream>
#include<algorithm>
using namespace std;
struct sir {int x,y;} gr[100010],pozi,pozf;
int gropi,n,i,t,pasi,drumuri,col,c,l,s,d,m,poz;
int cmp(sir a,sir b)
{ return a.y<b.y;}
int main()
{
ifstream f("gropi.in");
ofstream g("gropi.out");
f>>col>>gropi;
for(i=1;i<=gropi;i++) f>>gr[i].x>>gr[i].y;
sort(gr+1,gr+gropi+1,cmp);
f>>drumuri;
for(t=1;t<=drumuri;t++)
{ f>>pozi.x>>pozi.y>>pozf.x>>pozf.y;
s=1; d=gropi; poz=0;
if(pozi.y==pozf.y) { if(pozi.x==pozf.x) g<<"1"<<'\n';
else g<<"2"<<'\n';
}
else if(pozi.y<pozf.y) {
while(s<=d)
{ m=(s+d)>>1;
if(gr[m].y>=pozi.y) { poz=m; d=m-1;}
else s=m+1;
}
if(poz==0) { pasi=pozf.y-pozi.y+1; if(pozi.x!=pozf.x) pasi++; g<<pasi<<'\n';}
else{
l=pozi.x; c=pozi.y; pasi=1;
while(l!=pozf.x||c!=pozf.y)
{
if(gr[poz].x==l&&gr[poz].y<=pozf.y)
{ pasi+=gr[poz].y-c+1; c=gr[poz].y;
if(l==1) l=2; else l=1;
}
else if(gr[poz].y>pozf.y)
{ pasi+=pozf.y-c; c=pozf.y;
if(l!=pozf.x) pasi++;
l=pozf.x;
}
else { pasi+=gr[poz].y-c; c=gr[poz].y;}
poz++; if(poz>gropi)
{ pasi+=pozf.y-c;
c=pozf.y;
if(l!=pozf.x) pasi++;
l=pozf.x;
}
}
}
}
else
{
while(s<=d)
{ m=(s+d)>>1;
if(gr[m].y<=pozi.y) { poz=m; s=m+1;}
else d=m-1;
}
if(poz==0) {pasi=pozi.y-pozf.y+1; if(pozi.x!=pozf.x) pasi++; g<<pasi<<'\n';}
else {
pasi=1; l=pozi.x; c=pozi.y;
while(l!=pozf.x||c!=pozf.y)
{ if(gr[poz].x==l&&gr[poz].y>=pozf.y)
{ pasi+=c-gr[poz].y+1; c=gr[poz].y;
if(l==1) l=2; else l=1;
}
else if(gr[poz].y<pozf.y)
{ pasi+=c-pozf.y; c=pozf.y;
if(l!=pozf.x) pasi++;
l=pozf.x;
}
else {pasi+=c-gr[poz].y; c=gr[poz].y;}
poz--; if(poz<1)
{ pasi+=c-pozf.y; c=pozf.y;
if(l!=pozf.x) pasi++;
l=pozf.x;
}
}
}
}
g<<pasi<<'\n';
}
f.close();
g.close();
return 0;
}