Cod sursa(job #877673)

Utilizator Marius96Marius Gavrilescu Marius96 Data 13 februarie 2013 01:32:49
Problema Ograzi Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<tr1/unordered_map>

static unsigned short x[50005];
static unsigned short y[50005];
static unsigned short w,h;

#define MAP std::tr1::unordered_multimap<long long, long long>
#define IT MAP::iterator
#define first(x) ((x&0xFFFFF00000LL)>>20)
#define second(x) (x&0xFFFFF)
#define inside(s,a,b) a<=first (s) && a+w>=first (s) && b<=second (s) && b+h>=second (s)
#define pack(a,b) (((a)<<20)+(b))

static MAP hash;

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

	int n,m;
	scanf ("%d%d%hu%hu\n",&n,&m,&w,&h);
	hash.max_load_factor (0.7);
	hash.rehash (m/0.7+5);

	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++){
		short x,y;
		gets (s);
		x=atoi (s);
		y=atoi (strchr (s, ' ')+1);

		long long key=pack (x/w,y/h);
		long long value=pack (x,y);
		hash.insert ( std::make_pair (key,value));
	}

	int ans=0;
	for(int i=0;i<n;i++){
		const int sa=x[i]/w;
		const int sb=y[i]/h;
		std::pair<IT,IT> p;

		for(int a=sa;a<sa+2;a++)
			for(int b=sb;b<sb+2;b++){
				p=hash.equal_range (pack (a,b));
				for(IT it=p.first;it!=p.second;it++)
					if(inside (it->second,x[i],y[i]))
						ans++;
			}
	}

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