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

崩溃啊。。调试了3个小时的splay竟然TLE。。。。大牛们帮忙看看吧,哪里写搓掉了?

Posted by yzhw at 2009-09-26 02:59:09 on Problem 2761
# include <iostream>
# include <cstdlib>
# include <cstdio>
using namespace std;
class node
{
public:
	node *left;
	node *right;
	int key;
	int num;
	int sum;
	node():left(NULL),right(NULL),num(0),sum(0){}
};
class tree
{
public:
	node *head;
	tree():head(NULL){}
	void zig(node* &root)
	{
		node *p1=root->left;
		node *p2=root->right;
		node *p3=(p1==NULL?NULL:p1->left);
		node *p4=(p1==NULL?NULL:p1->right);
		node *p5=(p2==NULL?NULL:p2->left);
		node *p6=(p2==NULL?NULL:p2->right);
		root->left=p1;
		root->right=p5;
		p2->left=root;
		p2->right=p6;
		//updata
		root->sum=(p1==NULL?0:p1->sum+1)+(p5==NULL?0:p5->sum+1);
		root->num=(p1==NULL?0:p1->sum+1);
		if(p2!=NULL)
		{
			p2->sum=root->sum+(p6==NULL?0:p6->sum+1)+1;
			p2->num=root->sum+1;
		}
		//end updata
		root=p2;
	}
	void zag(node* &root)
	{
		node *p1=root->left;
		node *p2=root->right;
		node *p3=(p1==NULL?NULL:p1->left);
		node *p4=(p1==NULL?NULL:p1->right);
		node *p5=(p2==NULL?NULL:p2->left);
		node *p6=(p2==NULL?NULL:p2->right);
		p1->left=p3;
		p1->right=root;
		root->left=p4;
		root->right=p2;
		//updata
		root->sum=(p4==NULL?0:p4->sum+1)+(p2==NULL?0:p2->sum+1);
		root->num=(p4==NULL?0:p4->sum+1);
		if(p1!=NULL)
		{
			p1->sum=(p3==NULL?0:p3->sum+1)+root->sum+1;
			p1->num=(p3==NULL?0:p3->sum+1);
		}
		//end updata
		root=p1;
	}
	void insert(int key,node* &pos)
	{
		if(pos==NULL)
		{
			pos=new node();
			pos->key=key;
		}
		else if(key<pos->key)
		{
			insert(key,pos->left);
			(pos->num)++;
			(pos->sum)++;
			zag(pos);
		}
		else 
		{
			insert(key,pos->right);
			(pos->sum)++;
			zig(pos);
		}
	}
	void del(int key,node* &pos)
	{
		if(key==pos->key)
		{
		  if(pos->left!=NULL)
			{
				zag(pos);
				del(key,pos->right);
			}
			else
			{
				node *temp=pos;
				pos=pos->right;
				delete temp;
			}
		}
		else if(key<(pos->key))
		{
			del(key,pos->left);
			(pos->num)--;
			(pos->sum)--;
		}
		else
		{
			del(key,pos->right);
			(pos->sum)--;
		}
	}
	node findk(int k,node* &pos)
	{
		if(k==(pos->num)+1) return *pos;
		else if(k<=(pos->num)) return findk(k,pos->left);
		else return findk(k-pos->num-1,pos->right);
	}
};
struct ques
{
	int s,e;
	int id;
	int q;
	int ans;
}list[50001];
int cmp(const void *a,const void *b)
{
	ques *aa=(ques *)a;
	ques *bb=(ques *)b;
	if(aa->s!=bb->s) return aa->s-bb->s;
	else return aa->e-bb->e;
}
int cmp1(const void *a,const void *b)
{
	ques *aa=(ques *)a;
	ques *bb=(ques *)b;
	return aa->id-bb->id;
}
int main()
{
	tree data;
	int num[100001];
	int n,m;
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++)
	{
	    scanf("%d",num+i);
	
	}
	for(int i=1;i<=m;i++)
	{
		scanf("%d %d %d",&list[i].s,&list[i].e,&list[i].q);
		list[i].id=i;
	}
	qsort(list+1,m,sizeof(ques),cmp);
	for(int j=list[1].s;j<=list[1].e;j++)
		data.insert(num[j],data.head);
	list[1].ans=data.findk(list[1].q,data.head).key;
	for(int i=2;i<=m;i++)
	{
		if(list[i].s>=list[i-1].e)//区间错开
		{
			for(int j=list[i-1].s;j<=list[i-1].e;j++)
				data.del(num[j],data.head);
			for(int j=list[i].s;j<=list[i].e;j++)
				data.insert(num[j],data.head);
		}
		else if(list[i].s<=list[i-1].e&&list[i].e>list[i-1].e)//区间部分包含
		{
			for(int j=list[i-1].e+1;j<=list[i].e;j++)
				data.insert(num[j],data.head);
			for(int j=list[i-1].s;j<list[i].s;j++)
				data.del(num[j],data.head);
		}
		else //子集
		{
			for(int j=list[i-1].s;j<list[i].s;j++)
				data.del(num[j],data.head);
			for(int j=list[i].e+1;j<=list[i-1].e;j++)
				data.del(num[j],data.head);
		}
		list[i].ans=data.findk(list[i].q,data.head).key;
	}
	qsort(list+1,m,sizeof(ques),cmp1);
	for(int i=1;i<=m;i++)
		printf("%d\n",list[i].ans);
	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