Pagini recente » Cod sursa (job #1774046) | Monitorul de evaluare | Cod sursa (job #2671853) | Istoria paginii info-oltenia-2018/echipe/9-10 | Cod sursa (job #239546)
Cod sursa(job #239546)
#include <stdio.h>
#include <ctype.h>
#include <vector>
#include <algorithm>
using namespace std;
int c,n,m,ind;
char buf[50];
vector <pair<int,int> > gropi(100000);
int getn()
{
int ans=0;
while (isdigit(buf[ind]))
{
ans=ans*10+buf[ind]-'0';
++ind;
}
++ind;
return ans;
}
int main()
{
FILE* fin=fopen("gropi.in","r");
FILE* fout=fopen("gropi.out","w");
fscanf(fin,"%d%d\n",&c,&n);
for (int q=0;q<n;++q)
{
fgets(buf,50,fin);
ind=0;
int x=getn(), y=getn();
gropi[q]=make_pair(y,x);
}
sort(gropi.begin(),gropi.begin()+n);
fscanf(fin,"%d\n",&m);
for (int q=0;q<m;++q)
{
fgets(buf,50,fin);
ind=0;
int x1=getn(), y1=getn(), x2=getn(), y2=getn(), ans=abs(y2-y1)+1;
int i, akt=x1;
if (y1<y2)
{
i=upper_bound(gropi.begin(),gropi.begin()+n,make_pair(y1,x1))-gropi.begin();
while (i<n && gropi[i].first<=y1) ++i;
if (i<n && gropi[i].first>y1 && gropi[i].first<y2)
while (i<n && gropi[i].first<y2)
{
if (gropi[i].second==akt)
{
++ans;
akt=3-akt;
}
++i;
}
}
else
{
i=lower_bound(gropi.begin(),gropi.begin()+n,make_pair(y1,x1))-gropi.begin();
while (i==n || i>=0 && gropi[i].first>=y1) --i;
if (i>=0 && gropi[i].first>y2 && gropi[i].first<y1)
while (i>=0 && gropi[i].first>y2)
{
if (gropi[i].second==akt)
{
++ans;
akt=3-akt;
}
--i;
}
}
if (akt!=x2) ++ans;
fprintf(fout,"%d\n",ans);
}
fclose(fin);
fclose(fout);
return 0;
}