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;
}
时间: 2024-10-11 23:08:18

POJ3264 Balanced Lineup 【线段树】+【单点更新】的相关文章

POJ 3264 Balanced Lineup (线段树单点更新 区间查询)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36820   Accepted: 17244 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(线段树/单点更新)

解题报告 题意: 求区间内最大值和最小值的差值. 思路: 裸线段树,我的线段树第一发.区间最值. #include <iostream> #include <cstring> #include <cstdio> #define inf 99999999 #define LL long long using namespace std; LL minn[201000],maxx[201000]; void update(LL root,LL l,LL r,LL p,LL

POJ3264 Balanced Lineup 线段树 RMQ ST算法应用

Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 36813 Accepted: 17237 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 de

POJ-3264 Balanced Lineup(线段树)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 47042   Accepted: 22071 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 线段树区间最大值 最小值

Q个数 问区间最大值-区间最小值 1 // #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #i

【POJ】3264 Balanced Lineup ——线段树 区间最值

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 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训练计划2828_Buy Tickets(线段树/单点更新)

解题报告 题意: 插队完的顺序. 思路: 倒着处理数据,第i个人占据第j=pos[i]+1个的空位. 线段树维护区间空位信息. #include <iostream> #include <cstdio> #include <cstring> using namespace std; struct node { int x,v; } num[201000]; int sum[1000000],ans[201000]; void cbtree(int rt,int l,in

HDU2852_KiKi&#39;s K-Number(线段树/单点更新)

解题报告 题目传送门 题意: 意思很好理解. 思路: 每次操作是100000次,数据大小100000,又是多组输入.普通模拟肯定不行. 线段树结点记录区间里存在数字的个数,加点删点操作就让该点个数+1,判断x存在就查询[1,x]区间的个数和[1,x-1]的个数. 求x之后第k大的数就先确定小于x的个数t,第t+k小的数就是要求的. #include <iostream> #include <cstdio> #include <cstring> using namespa

HDU1166_敌兵布阵(线段树/单点更新)

解题报告 题意: 略 思路: 线段树单点增减和区间求和. #include <iostream> #include <cstring> #include <cstdio> #define LL long long using namespace std; int sum[201000]; void update(int root,int l,int r,int p,int v) { int mid=(l+r)/2; if(l==r)sum[root]+=v; else

HDU1754_I Hate It(线段树/单点更新)

解题报告 题意: 略 思路: 单点替换,区间最值 #include <iostream> #include <cstring> #include <cstdio> #define inf 99999999 using namespace std; int maxx[808000]; void update(int root,int l,int r,int p,int v) { int mid=(l+r)/2; if(l==r)maxx[root]=v; else if(