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 aking at 2008-09-06 14:13:59 on Problem 2761
刚学,很不懂,一直wa,求大牛给数据测试

#pragma warning(disable:4786)
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;

const int N=400010;
const int MAX=99999999;

vector<int>vet,myset[N];
vector<int>::iterator it[N];

struct seg
{
	int lc,rc;
	int data;
}S[N];

void init(int l,int r,int k)
{
	S[k].lc=l;
	S[k].rc=r;
	if(l==r)
		return;
	int mid=(l+r)>>1;
	init(l,mid,k<<1);
	init(mid+1,r,(k<<1)+1);
	return;
}

void add(int a,int b,int c,int k)
{
	if(a<S[k].lc)
		a=S[k].lc;
	if(b>S[k].rc)
		b=S[k].rc;
	if(a==S[k].lc&&b==S[k].rc)	
	{
		myset[k].clear();
		myset[k].push_back(c);
		return;
	}
	int mid=(S[k].lc+S[k].rc)>>1;
	if(a<=mid)
		add(a,b,c,k<<1);
	if(b>mid)
		add(a,b,c,(k<<1)+1);
	return;
}

void update(int a,int b,int k)
{
	if(S[k].lc==S[k].rc)
		return;
	int mid=(a+b)>>1;
	update(a,mid,k<<1);
	update(mid+1,b,(k<<1)+1);
	int i=0,j=0;
	myset[k].clear();
	while(i<myset[k<<1].size()&&j<myset[(k<<1)+1].size())
	{
		if(myset[k<<1][i]<=myset[(k<<1)+1][j])
		{
			myset[k].push_back(myset[k<<1][i]);
			++i;
		}
		else
		{
			myset[k].push_back(myset[(k<<1)+1][j]);
			++j;
		}
	}
	while(i<myset[k<<1].size())
	{
		myset[k].push_back(myset[k<<1][i]);
		++i;
	}
	while(j<myset[(k<<1)+1].size())
	{
		myset[k].push_back(myset[(k<<1)+1][j]);
		++j;
	}
	return;
}
		
void query(int a,int b,int k)
{
	if(a<S[k].lc)
		a=S[k].lc;
	if(b>S[k].rc)
		b=S[k].rc;
	if(S[k].lc==a&&S[k].rc==b)
	{
		vet.push_back(k);
		return;
	}
	int mid=(S[k].lc+S[k].rc)>>1;
	if(a<=mid)
		query(a,b,k<<1);
	if(b>mid)
		query(a,b,(k<<1)+1);
	return;
}

int res(int n)
{
	int res,idx=0;
	int i;
	for(i=0;i<vet.size();++i)
	{
		it[i]=myset[vet[i]].begin();
	}
	while(n>0)
	{
		res=MAX;
		for(i=0;i<vet.size();++i)
		{
			if(it[i]!=myset[vet[i]].end()&&res>*it[i])
			{
				res=*it[i];
				idx=i;
			}
		}
		++it[idx];
		--n;
	}
	return res;
}

int main()
{
	int n,m;
	int i,a,b,c;
//	freopen("a.txt","r",stdin);
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		init(1,n,1);
		for(i=0;i<n;++i)
		{
			scanf("%d",&a);
			add(i+1,i+1,a,1);
		}
		update(1,n,1);
		for(i=0;i<m;++i)
		{
			scanf("%d%d%d",&a,&b,&c);
			if(a>b)
			{
				int t=a;
				a=b;
				b=t;
			}
			vet.clear();
			query(a,b,1);
			printf("%d\n",res(c));
		}
	}
	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