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

有没有好心人帮我看看 我第一次写线段树 但是是WA

Posted by 20055106 at 2007-09-05 17:19:15 on Problem 2352
#include<stdio.h>
#include<stdlib.h>
struct mytree
{
	int l,r,num;
	mytree *lc,*rc;
};
mytree *root;
int ans[15005];
void build(int l,int r,mytree *rt)
{
	rt->l=l;rt->r=r;
	rt->num=0;
	int mid=(l+r)/2;
	if(r-l>1)
	{
		mytree *t=new mytree;
		rt->lc=t;
		mytree *tt=new mytree;
		rt->rc=tt;
		build(l,mid,rt->lc);
		build(mid+1,r,rt->rc);
	}
	else
		rt->lc=rt->rc=NULL;
}
int get(int i,int j,mytree *t)
{
	int l=t->l,r=t->r;
	int ans1=0,ans2=0;
	int mid=(l+r)/2;
	if(i==l&&j==r)
		return t->num;
	if(j<=mid&&t->lc!=NULL)
		return get(i,j,t->lc);
	if(i>mid&&t->rc!=NULL)
		return get(i,j,t->rc);
	if(t->lc!=NULL)
		ans1=get(i,mid,t->lc);
	if(t->rc!=NULL)
		ans2=get(mid+1,j,t->rc);
	return ans1+ans2;
}
void add(int a,mytree *t)
{
	int l=t->l,r=t->r;
	int mid=(l+r)/2;
//	if(l-r<=1)
//		return ;
	if(a>=l&&a<=r)
		t->num++;
	if(l==r)
		return ;
	if(a<=mid&&t->lc!=NULL)
		add(a,t->lc);
	if(a>mid&&t->rc!=NULL)
		add(a,t->rc);
}
int main()
{
	int n,i;
	root=new mytree;
	build(0,32000,root);
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		ans[get(0,x,root)]++;
		add(x,root);
	}
	for(i=0;i<n;i++)
		printf("%d\n",ans[i]);
	return 0;
}

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