Cod sursa(job #877699)

Utilizator Marius96Marius Gavrilescu Marius96 Data 13 februarie 2013 02:11:35
Problema Ograzi Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.22 kb
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
using std::vector;

static int x[50005];
static int y[50005];
static int w,h;

struct str
{
	int first,second;
};

inline str make_pair (int a,int b)
{
	str x;
	x.first=a;
	x.second=b;
	return x;
}

#define LEN 404521
#define PAIR str
#define inside(s,a,b) a<=s.first && a+w>=s.first && b<=s.second && b+h>=s.second
#define pack(s) ((s.first*130513+s.second)%LEN)
static vector<PAIR> hash[LEN];

int main (void)
{
	freopen ("ograzi.in","r",stdin);
#ifdef INFOARENA
	freopen ("ograzi.out","w",stdout);
#endif

	int n,m;
	scanf ("%d%d%d%d\n",&n,&m,&w,&h);

	char s[32];
	for(int i=0;i<n;i++){
		gets (s);
		x[i]=atoi (s);
		y[i]=atoi (strchr (s, ' ')+1);
	}

	for(int j=0;j<m;j++){
		int x,y;
		gets (s);
		x=atoi (s);
		y=atoi (strchr (s, ' ')+1);

		PAIR key=make_pair (x/w, y/h);
		hash[pack (key)].push_back (make_pair (x,y));
	}

	int ans=0;
	for(int i=0;i<n;i++){
		const int sa=x[i]/w;
		const int sb=y[i]/h;

		for(int a=sa;a<sa+2;a++)
			for(int b=sb;b<sb+2;b++){
				int key=pack (make_pair (a,b));
				for(std::vector<PAIR>::iterator it=hash[key].begin();it!=hash[key].end();it++)
					if(inside ((*it), x[i],y[i]))
						ans++;
			}
	}

	printf ("%d",ans);
	return 0;
}