POJ3264 Balanced Lineup RMQ 线段树

求区间内最大数和最小数的差,用两棵线段树,一个维护区间最大值,一个维护区间最小值。

#include <stdio.h>
#include <vector>
#include <math.h>
#include <string.h>
#include <string>
#include <iostream>
#include <queue>
#include <list>
#include <algorithm>
#include <stack>
#include <map>
#include <time.h>
using namespace std;

#define QUADMEMSET
inline void QuadMemSet(void* dst, int iSize, int value)
{
	iSize = iSize / 4;
	int* newDst = (int*) dst;
#ifdef WIN32
	__asm
	{
		mov edi, dst
		mov ecx, iSize
		mov eax, value
		rep stosd
	}
#else
	for (int i = 0; i < iSize;i++)
	{
		newDst[i] = value;

	}
#endif
}

#define MAXN 132000
int ST[2][MAXN];
#define MAXVALUE 1000000
#define MINVALUE 0

template<class TYPE>
void UpdateValue(TYPE st[],int i, TYPE value, int N, bool bMin)
{
	i += N - 1;
	st[i] = value;
	while (i > 0)
	{
		i = (i - 1) / 2;
		if (bMin)
		{
			st[i] = min(st[i * 2 + 1], st[i * 2 + 2]);
		}
		else
			st[i] = max(st[i * 2 + 1], st[i * 2 + 2]);
	}
}

template<class TYPE>
TYPE QueryST(TYPE st[], int a, int b, int l, int r, int k, bool bMin)
{
	if (l > b || a > r)
	{
		return bMin ? MAXVALUE : MINVALUE;
	}
	if (l >= a && b >= r)
	{
		return st[k];
	}
	else
	{
		TYPE value1 = QueryST(st, a, b, l, (r + l) / 2, k * 2 + 1, bMin);
		TYPE value2 = QueryST(st, a, b, (r + l) / 2 + 1, r, k * 2 + 2, bMin);
		if (bMin)
		{
			return min(value1, value2);
		}
		else
		{
			return max(value1, value2);
		}
	}
}

int main()
{
#ifdef _DEBUG
	freopen("e:\\in.txt", "r", stdin);
#endif
	QuadMemSet(ST[0], sizeof(ST[0]), MAXVALUE);
	QuadMemSet(ST[1], sizeof(ST[1]), MINVALUE);
	int N, Q;
	scanf("%d %d", &N, &Q);
	int maxn = 1;
	while (maxn < N)
	{
		maxn *= 2;
	}
	for (int i = 0; i < N; i++)
	{
		int value;
		scanf("%d", &value);
		UpdateValue<int>(ST[0], i, value, maxn, true);
		UpdateValue<int>(ST[1], i, value, maxn, false);
	}
	for (int i = 0; i < Q;i++)
	{
		int a, b;
		scanf("%d %d", &a, &b);
		int maxr = QueryST<int>(ST[1], a - 1, b - 1, 0, maxn - 1, 0, false);
		int minr = QueryST<int>(ST[0], a - 1, b - 1, 0, maxn - 1, 0, true);
		printf("%d\n", maxr - minr);
	}
	return 0;
}

POJ3264 Balanced Lineup RMQ 线段树,布布扣,bubuko.com

时间: 2024-11-03 20:46:47

POJ3264 Balanced Lineup RMQ 线段树的相关文章

POJ3264——Balanced Lineup(线段树)

本文出自:http://blog.csdn.net/svitter 题意:在1~200,000个数中,取一段区间,然后在区间中找出最大的数和最小的数字,求这两个数字的差. 分析:按区间取值,很明显使用的线段树.区间大小取200000 * 4 = 8 * 10 ^5; 进行查询的时候,注意直接判断l, r 与mid的关系即可,一开始写的时候直接与tree[root].L判断,多余了, 逻辑不正确. #include <iostream> #include <stdio.h> #inc

poj 3264 Balanced Lineup RMQ线段树实现

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36613   Accepted: 17141 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

POJ 3264 Balanced Lineup(RMQ/线段树)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 40312   Accepted: 18936 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

POJ3264 Balanced Lineup 【线段树】+【单点更新】

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32778   Accepted: 15425 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

[POJ3264]Balanced Lineup(线段树,区间最值差)

题目链接:http://poj.org/problem?id=3264 写了一个更优美一点的 1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <climits> 6 #include <complex> 7 #include <fstream> 8 #include <c

POJ 3264 Balanced Lineup (线段树||RMQ)

A - Balanced Lineup Crawling in process... Crawling failed Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3264 Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always li

poj 3246 Balanced Lineup(线段树)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 38942   Accepted: 18247 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

POJ - 3264 - Balanced Lineup (线段树)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 39060   Accepted: 18299 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh