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

TLE使我死得不明不白,很有冲动重新再写!!!!!

Posted by CSUST_14 at 2012-08-18 09:11:26 on Problem 3145
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <list>
#include <queue>
#include <stack>
#include <cmath>

using namespace std;

#define PF(x) (scanf("%d",&x))
#define PT(x,y) (scanf("%d%d",&x,&y))
#define PR(x) (printf("%d\n",x))
#define PRT(x,y)(printf("%d %d\n",x,y))
#define PB(x)(scanf("%I64d",&x))
#define PRB(x)(printf("%I64d\n",(x)))
typedef __int64 LL;
#define N 500005
#define M 105
#define Mod 1000
#define Inf 0x7ffffff

struct tree
{
	int l,r;
	int mins;
	int id;
};
tree T[4*N];
int ar[N];
int n;
tree result;

void build(int l,int r,int k)
{
	T[k].l = l;
	T[k].r = r;
	T[k].mins = Inf;
	T[k].id = 0;
	if(l==r) return ;
	int mid = (l+r)>>1;
	build(l,mid,2*k);
	build(mid+1,r,2*k+1);
}
void insert(int l,int r,int k,int v,int id)
{
	if(l== v && r == v) 
	{
		T[k].mins = v;
		T[k].id = id;
		return ;
	}
	int mid = (l+r)>>1;
	if(v<=mid) insert(l,mid,2*k,v,id);
	else insert(mid+1,r,2*k+1,v,id);
	if(T[2*k].mins<T[k].mins)
	{
		T[k].mins = T[2*k].mins;
		T[k].id = T[2*k].id;
	}
	if(T[2*k+1].mins<T[k].mins)
	{
		T[k].mins = T[2*k+1].mins;
		T[k].id = T[2*k+1].id;
	}
}
void query(int l,int r,int k,int ll,int rr)
{
	/*if(l == ll && r == rr) return T[k];
	int mid = (l+r)>>1;
	if(rr<=mid) return query(l,mid,2*k,ll,rr);
	if(ll>mid) return query(mid+1,r,2*k+1,ll,rr);
	tree a = query(l,mid,2*k,ll,mid);
	tree b = query(mid+1,r,2*k+1,mid+1,rr);
	if(a.mins>b.mins) return b;
	return a;*/

	if(l>=ll && r<=rr) 
	{
		if(result.mins>T[k].mins) result = T[k];
		return ;
	}
	int mid = (l+r)>>1;
	if(mid>=ll) query(l,mid,2*k,ll,rr);
	if(mid<rr) query(mid+1,r,2*k+1,ll,rr);
	
}
int fun(int mod,int tos)
{
	int id,ans;
	id = tos;
	ans = -1;
	for(int i=tos;i>0;i--)
	{
		if(ans == -1)
			ans = ar[i]%mod;
		else if(ans>ar[i]%mod)
			ans = ar[i]%mod,id = i;
	}
	return id;
}
void init()
{
	int test=1;
	bool flag = 0;
	while(1)
	{
		PF(n);
		if(n == 0) break;
		build(1,N,1);

		if(flag) puts("");flag = 1;
		printf("Case %d:\n",test++);

		char s[10];
		int x;
		int tos=1;
		for(int i=0;i<n;i++)
		{
			scanf("%s%d",s,&x);
			if(s[0] == 'B')
			{
				ar[tos] = x;
				insert(1,N,1,x,tos);
				tos++;
			}
			else
			{
				if(tos==1) {puts("-1");continue;}
				if(x<4000)
					printf("%d\n",fun(x,tos-1));
				else
				{
					int l,r;
					l = 1,r = x-1;
					int ans,id;
					ans = -1;

					while(l<=N)
					{
						result.mins = Inf;
						query(1,N,1,l,r);
						if(l==1) l = x;
						else l+=x;
						r+=x;
						if(r>N) r = N;

						if(result.mins == Inf) continue;
						if(ans == -1) ans = result.mins%x,id = result.id;
						else if(ans>result.mins%x) 
							ans = result.mins%x,id = result.id;
						
					}
					printf("%d\n",id);
				}
			}
		}
	}
	return ;
}

int main()
{
	init();
	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