POJ3264 Balanced Lineup 线段树+单点更新

Posted gccbuaa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

Input

Line 1: Two space-separated integers, N and Q
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i 
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.

Output

Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

Sample Input

6 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0

2014-9-4 12:07:18更新:

#include <stdio.h>
#include <algorithm>
#define inf 0x7fffffff
#define maxn 50002
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std;

struct Node{
	int maxv, minv;
} tree[maxn << 2];
int arr[maxn], minv, maxv;

void pushUp(int rt){
	tree[rt].maxv = max(tree[rt << 1].maxv, tree[rt << 1 | 1].maxv);
	tree[rt].minv = min(tree[rt << 1].minv, tree[rt << 1 | 1].minv);
}

void build(int l, int r, int rt)
{
	if(l == r){
		tree[rt].maxv = tree[rt].minv = arr[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(lson); build(rson);
	pushUp(rt);
}

void query(int left, int right, int l, int r, int rt)
{
	if(left == l && right == r){
		maxv = max(maxv, tree[rt].maxv);
		minv = min(minv, tree[rt].minv);
		return;
	}
	int mid = (l + r) >> 1;
	if(right <= mid) return query(left, right, lson);
	else if(left > mid) return query(left, right, rson);
	query(left, mid, lson); query(mid + 1, right, rson);
}

int main()
{
	int n, m, i, a, b;
	while(scanf("%d%d", &n, &m) == 2){
		for(i = 1; i <= n; ++i)
			scanf("%d", &arr[i]);
		build(1, n, 1);
		while(m--){
			scanf("%d%d", &a, &b);
			minv = inf; maxv = 0;
			query(a, b, 1, n, 1);
			printf("%d\n", maxv - minv);
		}
	}
	return 0;
}


#include <stdio.h>
#define maxn 200002
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1

struct Node{
	int min, max;
} tree[maxn << 2];
int maxAns, minAns;

int maxVal(int a, int b)
{
	return a > b ?

a : b; } int minVal(int a, int b) { return a < b ? a : b; } void build(int l, int r, int rt) { if(l == r){ scanf("%d", &tree[rt].min); tree[rt].max = tree[rt].min; return; } int mid = (l + r) >> 1; build(lson); build(rson); tree[rt].max = maxVal(tree[rt << 1].max, tree[rt << 1 | 1].max); tree[rt].min = minVal(tree[rt << 1].min, tree[rt << 1 | 1].min); } void query(int left, int right, int l, int r, int rt) { if(left == l && right == r){ if(tree[rt].max > maxAns) maxAns = tree[rt].max; if(minAns > tree[rt].min) minAns = tree[rt].min; return; } int mid = (l + r) >> 1; if(right <= mid) query(left, right, lson); else if(left > mid) query(left, right, rson); else{ query(left, mid, lson); query(mid + 1, right, rson); } } int main() { int n, q, i, a, b; scanf("%d%d", &n, &q); build(1, n, 1); while(q--){ scanf("%d%d", &a, &b); maxAns = 1; minAns = 1000000; query(a, b, 1, n, 1); printf("%d\n", maxAns - minAns); } return 0; }



以上是关于POJ3264 Balanced Lineup 线段树+单点更新的主要内容,如果未能解决你的问题,请参考以下文章

Balanced Lineup POJ - 3264

POJ 3264 Balanced Lineup

POJ3264 Balanced Lineup

poj3264 Balanced Lineup 2011-12-20

POJ 3264 Balanced Lineup

POJ 3264 Balanced Lineup