Cod sursa(job #273636)

Utilizator andrei-alphaAndrei-Bogdan Antonescu andrei-alpha Data 8 martie 2009 20:11:38
Problema Gropi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
using namespace std;

#include <vector>
#include <bitset>
#define f first
#define s second
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define mp make_pair

int N,Q,S[1<<17];
vector< pair<int,int> > V(1<<17);

void scan()
{
	freopen("gropi.in","r",stdin);
	freopen("gropi.out","w",stdout);
	scanf("%d%d",&N,&N);
	
	V.resize(N);
	FOR(i,0,N-1)
		scanf("%d%d",&V[i].s,&V[i].f);
	sort(V.begin(),V.end());
}

void solve()
{
	FOR(i,1,N-1)
		S[i] = S[i-1] + (V[i].s != V[i-1].s);
	FOR(i,0,N-1)
		printf("%d %d\n",V[i].f,V[i].s);
		
	int rez,x1,y1,x2,y2;	
	scanf("%d",&Q);
	FOR(i,1,Q)
	{
		scanf("%d%d%d%d\n",&y1,&x1,&y2,&x2);
		if(x2 < x1) swap(x1,x2),swap(y1,y2);
		rez = x2 - x1 + 1;
		x1 = upper_bound(V.begin(),V.end(),mp(x1,0)) - V.begin();
		x2 = upper_bound(V.begin(),V.end(),mp(x2,0)) - V.begin();
		
		if(--x2>=x1)
		{
			if(y1 == V[x1].s) ++rez,y1 = V[x1].s;
			if(y2 == V[x2].s) ++rez,y2 = V[x2].s;
			rez += S[x2] - S[x1];
		}
		else
			if(y1!=y2) ++rez;
		printf("%d\n",rez);
	}
}

int main()
{
	scan();
	solve();
	return 0;
}