Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

线段树的话排序的时候把入边排在出边的前面,重边就过了

Posted by PoPoQQQ at 2014-06-04 12:19:14 on Problem 1177 and last updated at 2014-06-04 12:22:35
RT
能处理DISCUSS里的所有重边数据
另外HDU那道题依旧没有重边。。我改错了代码,把不判重的数据交上去发现AC了

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define c(a) memset(a,0,sizeof a)
#define ls tree[p].lson
#define rs tree[p].rson
struct abcd{int x,y,f,lson,rson;}tree[40100];int tot;
struct abcde{int no,l,r,f;}linex[10100],liney[10100],toadd;
int n,ans;
int cmp(const void *x,const void *y)
{
	struct abcde *xx=(struct abcde*)x;
	struct abcde *yy=(struct abcde*)y;
	if(xx->no==yy->no)return yy->f-xx->f;
	return xx->no-yy->no;
}
void Build_tree(int p,int x,int y)
{
	int mid=x+y>>1;
	tree[p].x=x;tree[p].y=y;
	if(x+1==y)return ;
	ls=++tot;
	rs=++tot;
	Build_tree(ls,x,mid);
	Build_tree(rs,mid,y);
}
void add(int p)
{
	int mid=tree[p].x+tree[p].y>>1;
	if(tree[p].x>=toadd.l&&tree[p].y<=toadd.r&&tree[p].f>=0)
	{
		if(!tree[p].f)ans+=tree[p].y-tree[p].x;
		tree[p].f+=toadd.f;
		if(!tree[p].f)ans+=tree[p].y-tree[p].x;
		return ;
	}
	if(tree[p].f>=0)tree[ls].f+=tree[p].f,tree[rs].f+=tree[p].f,tree[p].f=-1;
	if(toadd.r<=mid)add(ls);
	else if(toadd.l>=mid)add(rs);
	else add(ls),add(rs);
	if(tree[ls].f==tree[rs].f&&tree[ls].f>=0)tree[p].f=tree[ls].f,tree[ls].f=tree[rs].f=0;
}
int main()
{
	int i,x1,x2,y1,y2;
	while(~scanf("%d",&n))
	{
		c(tree);tot=0;ans=0;
		for(i=1;i<=n;i++)
		{
			scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
			x1+=10000;x2+=10000;y1+=10000;y2+=10000;
			linex[i+i-1].no=y1;
			linex[i+i-1].l=x1;
			linex[i+i-1].r=x2;
			linex[i+i-1].f=1;
			linex[i<<1].no=y2;
			linex[i<<1].l=x1;
			linex[i<<1].r=x2;
			linex[i<<1].f=-1;
			liney[i+i-1].no=x1;
			liney[i+i-1].l=y1;
			liney[i+i-1].r=y2;
			liney[i+i-1].f=1;
			liney[i<<1].no=x2;
			liney[i<<1].l=y1;
			liney[i<<1].r=y2;
			liney[i<<1].f=-1;
		}
		n<<=1;
		Build_tree(0,0,20000);
		qsort(linex+1,n,sizeof linex[0],cmp);
		qsort(liney+1,n,sizeof liney[0],cmp);
		for(i=1;i<=n;i++)
		toadd=linex[i],
		add(0);
		memset(tree,0,sizeof tree);tot=0;
		Build_tree(0,0,20000);
		for(i=1;i<=n;i++)
		toadd=liney[i],
		add(0);
		printf("%d\n",ans);
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator