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

Re:请大家帮忙看看2761,我用的2分插入和删除,为什么老是WA呢?(内附代码)

Posted by elf788544 at 2006-02-28 19:36:07 on Problem 2761
In Reply To:请大家帮忙看看2761,我用的2分插入和删除,为什么老是WA呢?(内附代码) Posted by:elf788544 at 2006-02-28 18:17:49
请大家帮帮我啊,算法复杂度是O(nlog(n)+m*log(n))的
为什么老是WA呢?
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
#define MAXN 100001
vector<int>sp;
int a[MAXN];
int res[50001];
struct seg
{
	int s,t;
	int k;
	int ind;
}se[50001];
bool cmp(seg aa,seg bb)
{
	return aa.s<bb.s || aa.s==bb.s && aa.t<bb.t;
}
void vpop(int x)
{
	int sta=0,end=sp.size()-1,mid=0;
	while(sta<=end)
	{
		mid=(sta+end)/2;
		if(x>sp[mid]) sta=mid+1;
		else if(x<sp[mid]) end=mid-1;
		else 
		{
			sp.erase(sp.begin()+mid);
			return;
		}
	}
}
void vpush(int x)
{
	int sta=0,end=sp.size()-1,mid=-1;
	while(sta<=end)
	{
		mid=(sta+end)/2;
		if(x>sp[mid]) sta=mid+1;
		else if(x<sp[mid]) end=mid-1;
		else 
		{
			sp.insert(sp.begin()+mid+1,x);
			return;
		}
	}
	sp.insert(sp.begin()+mid+1,x);
}
void out(void)
{
	register int i;
	for(i=0;i<sp.size();i++)
		printf("%2d",sp[i]);
	printf("\n");
}
int main()
{
	register int i,j;
	int n,m;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++)
		scanf("%d",a+i);
	for(i=0;i<m;i++)
	{
		scanf("%d%d%d",&se[i].s,&se[i].t,&se[i].k);
		if(se[i].s>se[i].t) se[i].s^=se[i].t,se[i].t^=se[i].s,se[i].s^=se[i].t;
		se[i].ind=i;
	}
	sort(se,se+m,cmp);
	for(i=se[0].s;i<=se[0].t;i++)
		sp.push_back(a[i]);
	sort(sp.begin(),sp.end());
	res[se[0].ind]=sp[se[0].k-1];
	//out();
	for(i=1;i<m;i++)
	{
		if(se[i-1].t<se[i].s)
		{
			for(j=se[i-1].s;j<=se[i-1].t;j++)
			{
				vpop(a[j]);
				//out();
			}
			for(j=se[i].s;j<=se[i].t;j++)
			{
				vpush(a[j]);
				//out();
			}
		}
		else
		{
			for(j=se[i-1].s;j<se[i].s;j++)
			{
				vpop(a[j]);
				//out();
			}
			for(j=se[i-1].t+1;j<=se[i].t;j++)
			{
				vpush(a[j]);
				//out();
			}
		}
		res[se[i].ind]=sp[se[i].k-1];	
	}
	for(i=0;i<m;i++)
		printf("%d\n",res[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