Pagini recente » Cod sursa (job #2570889) | Cod sursa (job #2448377) | Cod sursa (job #66529) | Cod sursa (job #1999523) | Cod sursa (job #285942)
Cod sursa(job #285942)
#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; pasi=1;
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; if(pozi.x!=pozf.x) pasi++; pozi=pozf;}
l=pozi.x; c=pozi.y;
while(c!=pozf.y)
{ if(gr[poz].y<=pozf.y&&l==gr[poz].x)
{ 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;}
else {pasi+=gr[poz].y-c; c=gr[poz].y;}
poz++; if(poz>gropi&&c!=pozf.y)
{ pasi+=pozf.y-c;c=pozf.y;}
}
if(l!=pozf.x) pasi++;
}
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; if(pozi.x!=pozf.x) pasi++; pozi=pozf;}
l=pozi.x; c=pozi.y;
while(c!=pozf.y)
{ if(gr[poz].y>=pozf.y&&l==gr[poz].x)
{ 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;}
else {pasi+=c-gr[poz].y; c=gr[poz].y;}
poz--; if(poz<1&&c!=pozf.y) {pasi+=c-pozf.y; c=pozf.y;}
}
if(l!=pozf.x) pasi++;
}
g<<pasi<<'\n';
}
f.close();
g.close();
return 0;
}