Cod sursa(job #80001)

Utilizator gigi_becaliGigi Becali gigi_becali Data 25 august 2007 01:50:55
Problema Cutii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.34 kb
using namespace std;
#include <map>
#include <hash_map.h>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#define maxn 16001
#define maxv 1000000
#include <set>
#include <list>
#define maxh 666013
#define ui unsigned int
#define prime 17
inline int bit(int x){ return (x&((x-1)^x));}
struct nod { int x, y; unsigned int x1, y1;};

map<ui, map<ui, ui> > BIT;
nod a[maxn];
int n;

struct nd { ui x, y; ui v;nd(){}; nd(ui a, ui b, ui c){x=a; y=b; v=c;};};



struct cmp {
	bool operator()(const nd &a, const nd &b)const
	{
		if(a.x<b.x)return 1;
		if(a.x==b.x)if(a.y<b.y)return 1;
		if(a.x==b.x)if(a.y==b.y)if(a.v<b.v)return 1;
		
		return 0;
	}
};
set<nd,cmp>H[maxh];

struct Nod{ ui x, y, v; Nod*next;};

Nod *Has[maxh];
int len[maxh];
inline ui hashf(ui i, ui j)
{
	i=i%maxh;
	j=j%maxh;
	return (i*prime+maxh+j)%maxh;
}


void read()
{
	freopen("zoo.in","r",stdin);
	scanf("%d\n", &n);
	int i;
	for(i=1;i<=n;++i) scanf("%d %d\n", &a[i].x, &a[i].y);
}

void update(ui x, ui y)
{
	ui i, j, p, found;
	nd ax, tx;
	set<nd,cmp>::iterator it;
	Nod *r;
	for(i=x;i<=(maxv<<1)+1; i+=bit(i))
		for(j=y;j<=(maxv<<1)+1 ;j+=bit(j))
		{
			p=hashf(i, j);
			found=0;
			for(r=Has[p];r;r=r->next)
				if(r->x==i && r->y==j){found=1; ++r->v;}
			if(!found)
			{
				++len[p];
				r=new Nod;
				r->x=i;
				r->y=j;
				r->v=1;
				r->next=Has[p];
				Has[p]=r;
			}
			
	
		}
		
			
			
			//++BIT[i][j];
/*
		{
			p=hashf(i, j);
			found=0;
			
			ax.x=i;
			ax.y=j;
			if(H[p].find(ax)!=H[p].end()){ax.v=1;H[p].insert(ax);}
			else
			{
				tx=*H[p].find(ax);
				ax.v=tx.v;
				H[p].erase(tx);
				++ax.v;
				H[p].insert(ax);
			}
			
		}
			*/
		/*
		{
			p=hashf(i, j);
			found=0;
			for(it=H[p].begin();it!=H[p].end();++it)
				if(it->x==i && it->y==j) {found=1;break;}
			if(found)it->v++;
				else H[p].push_back(nd(i, j));
			//	printf("%d %d\n", i, j);
			//++BIT[i][j];
		}*/
}

ui query(ui x, ui y)
{
	ui s=0, i, j, p;
	set<nd, cmp>::iterator it;
	Nod *r;
	for(i=x;i>0;i-=bit(i))
		for(j=y;j>0;j-=bit(j))
		{
			p=hashf(i, j);
			//if(len[p]>2)continue;
			for(r=Has[p];r;r=r->next)
				if(r->x==i && r->y==j)
				{
					s+=r->v;
					break;
				}
			
		}
		/*
		{
			p=hashf(i, j);
			for(it=H[p].begin();it!=H[p].end();++it)
				if(it->x==i && it->y==j) { s+=it->v; break;}
	//		s+=BIT[i][j];
		}
			*/
	return s;
}

char ax[100000*45];
int main()
{
//	freopen("zoo.out","w",stdout);
//	read();
	double start=clock();
	srand(time(0));
	int i, m,x1, x2, y1, y2;
	n=16000;
	for(i=1;i<=n;++i) a[i].x=rand()%1000000234, a[i].y=rand()%1000000234;
	
	for(i=1;i<=n;++i) a[i].x1=(ui)a[i].x+maxv, a[i].y1=(ui)a[i].y+maxv;
	for(i=1;i<=n;++i)update(a[i].x1, a[i].y1);
	
	m=100000;
	//scanf("%d\n", &m);
//	fread(ax, sizeof(char), m*44, stdin);
	char *p;
	
	unsigned int X1, Y1, X2, Y2;
	
	m--;
	/*
	p=strtok(ax, " ");
	x1=atoi(p);
	p=strtok(0, " ");
	y1=atoi(p);
	p=strtok(0, " ");
	x2=atoi(p);
	p=strtok(0, " \n");
	y2=atoi(p);
	*/
	x1=rand()%1000000234;
	x2=rand()%1000000234;
	y1=rand()%1000000234;
	y2=rand()%1000000234;

	X1=(ui)x1+maxv, Y1=(ui)y1+maxv, X2=(ui)x2+maxv, Y2=(ui)y2+maxv;
//printf("__%d %d %d %d\n", x1, x2, y1, y2);
//printf("(%d %d %d %d\n", query(x2, y2), query(x1-1 ,y1-1), query(x2, y1-1), query(x1-1, y2));
	ui s=query(X2, Y2)+query(X1-1, Y1-1)-query(X2, Y1-1)-query(X1-1, Y2);
	//printf("%d\n", s);

	
	while(m--)
	{
		/*
		p=strtok(0, " ");
		x1=atoi(p);
		p=strtok(0, " ");
		y1=atoi(p);
		p=strtok(0, " ");
		x2=atoi(p);
		p=strtok(0, " \n");
		y2=atoi(p);
		*/
		x1=rand()%1000000234;
		x2=rand()%1000000234;
		y1=rand()%1000000234;
		y2=rand()%1000000234;

	//	scanf("%d %d %d %d\n", &x1, &y1, &x2, &y2);
		X1=(ui)x1+maxv, Y1=(ui)y1+maxv, X2=(ui)x2+maxv, Y2=(ui)y2+maxv;
		//printf("__%d %d %d %d\n", x1, x2, y1, y2);
		//printf("(%d %d %d %d\n", query(x2, y2), query(x1-1 ,y1-1), query(x2, y1-1), query(x1-1, y2));
		ui s=query(X2, Y2)+query(X1-1, Y1-1)-query(X2, Y1-1)-query(X1-1, Y2);
		//printf("%d\n", s);
		
	}

map<int, int>mx;	
	for(i=0;i<maxh;++i)mx[len[i]]++;
	for(map<int,int>::iterator it=mx.begin();it!=mx.end();++it)printf("(%d %d)", it->first, it->second);
	printf("\n");
	
//	printf("%d\n", max);
	printf("%lf\n", (clock()-start)/(double)CLOCKS_PER_SEC);
	return 0;
}