Cod sursa(job #239524)

Utilizator SleepyOverlordPatcas Csaba SleepyOverlord Data 4 ianuarie 2009 22:21:56
Problema Gropi Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
#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();
			if (gropi[i].first==y1) ++i;
			if (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();
			if (i==n || 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;
}