Pagini recente » Cod sursa (job #2390373) | Cod sursa (job #2356815) | Cod sursa (job #2623395) | Cod sursa (job #981622) | Cod sursa (job #239586)
Cod sursa(job #239586)
#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), gropi2;
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);
gropi2.push_back(gropi[0]);
for (int q=1;q<n;++q)
if (gropi[q].second!=gropi[q-1].second) gropi2.push_back(gropi[q]);
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,i,j;
if (y1>y2)
{
swap(x1,x2);
swap(y1,y2);
}
i=upper_bound(gropi2.begin(),gropi2.end(),make_pair(y1,x1))-gropi2.begin();
while(i<gropi2.size() && gropi2[i].first<=y1) ++i;
j=lower_bound(gropi2.begin(),gropi2.end(),make_pair(y2,x2))-gropi2.begin();
while(j==gropi2.size() || j>=0 && gropi2[j].first>=y2) --j;
if (i<=j)
{
ans+=j-i+1;
if (gropi2[i].second!=x1) --ans;
if (gropi2[j].second==x2) ++ans;
}
else
{
int aux=upper_bound(gropi.begin(),gropi.begin()+n,make_pair(y1,x1))-gropi.begin();
while(aux<n && gropi[aux].first<=y1) ++aux;
if (aux<n && gropi[aux].first<y2)
{
if (gropi[aux].second==x1) ++ans;
if (gropi[aux].second==x2) ++ans;
}
else
if (x1!=x2) ++ans;
}
fprintf(fout,"%d\n",ans);
}
fclose(fin);
fclose(fout);
return 0;
}