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

用的RMQ,为什么TLE啊。。。。

Posted by juventus at 2015-04-05 14:42:32 on Problem 3264
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
	int N, Q;
	cin >> N >> Q;
	int *cow = new int[N + 1];
	int i, j;
	for (i = 1; i <= N; ++i) cin >> cow[i];
//	int Fmin[50001][50001],Fmax[50001][50001];
	int **Fmin = new int*[N + 1];
	for (i = 0; i <= N; ++i) Fmin[i] = new int[17];
	int **Fmax = new int*[N + 1];
	for (i = 0; i <= N; ++i) Fmax[i] = new int[17];
	for(i = 1; i <= N; ++i)
	{
		Fmin[i][0] = cow[i];
		Fmax[i][0] = cow[i];
	}
	for (j = 1; j <= 16; ++j)
	{
		for (i = 1; i <= N; ++i)
		{
			if (i + (1 << j) - 1 <= N)
			{
				Fmin[i][j] = min(Fmin[i][j - 1], Fmin[i + (1 << (j - 1))][j - 1]);
				Fmax[i][j] = max(Fmax[i][j - 1], Fmax[i + (1 << (j - 1))][j - 1]);
			}
		}
	}
	int ii, low, high;
	for (ii = 0; ii < Q; ++ii)
	{
		cin >> low >> high;
		int k = int(log(floor(high - low + 1.0))/log(2.0));
		int mymin = min(Fmin[low][k], Fmin[high - (1<<k) + 1][k]), mymax = max(Fmax[low][k], Fmax[high - (1<<k) + 1][k]);
		cout << mymax - mymin << endl;
	}

	delete []cow;
	for (i = 0; i <= N; ++i)
	{
		delete []Fmin[i];
		delete []Fmax[i];
	}
	delete []Fmin;
	delete []Fmax;
	return 0;
}
谢谢。。bow

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