1. 程式人生 > >POJ3264 Balanced Lineup 【線段樹】+【單點更新】

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;
}


相關推薦

hdu 1116 敵兵布陣 線段 區間求和 更新

freopen names 區間求和 add 知識 scan urn code blog 線段樹的基本知識可以先google一下,不是很難理解 線段樹功能:update:單點增減 query:區間求和 #include <bits/stdc++.h>

Codeforces768B-Code For 1-類似線段-枚舉+更新or區間更新

pre \n turn .net main std blog query amp 目錄 Catalog Solution: (有任何問題歡迎留言或私聊 && 歡迎交流討論哦 Catalog Problem:Portal傳送門 ?原題目描述在最下面。 ?

線段學習(更新+區間更新+區間查詢)(C++模板)

一、線段樹的用處        在對一組連續的資料進行修改或者求和(求最值)操作時,線段樹可以通過快速的修改子區間上的值來達成你的目標。二、線段樹是什麼        線段樹是一種二叉搜尋樹,它將一個區間劃分成一些單元區間,每個單元區間對應線段樹中的一個葉結點。使用線段樹可以

線段總結(更新,區間更新,區間求和,區間求最值)

注:每個功能在程式碼中有註釋,具體詳解可自己輸出測試 #include<iostream> #include<cstdio> #include<cstring> using namespace std; #define N 4000

線段模板(更新,區間更新,RMQ)

1.單點更新 說明 單點更新,區間求和(你問我單點求和??你就不會把區間長度設為0啊?) • sum[]為線段樹,需要開闢四倍的元素數量的空間。 • build()為建樹操作 • update()為更新操作 • query()為查詢操作 時間複雜度:O(nlo

線段學習筆記(更新+區間查詢最大值+lazy標記+pushdown操作+區間更新+求區間和)

目錄 什麼是線段樹? 線段樹基本操作: 建立線段樹 線段樹單點更新 區間查詢最大最小值 延遲標記(懶人標記)+pushdown操作 區間更新 求區間和 注:以下所有程式碼都是針對維護區間和的。 什麼是線段樹? 線段樹是一種二叉搜尋樹,與區間

線段基礎:更新,區間最值(和)查詢

單點更新,區間查詢 線段樹可以解決一類區間問題,例如最基礎的單點更新,區間最值查詢。 程式碼如下: #include<bits/stdc++.h> using namespace std; const int maxn = 10000; //原

線段版(更新,區間查詢)

#define lid (id << 1) #define rid (id << 1 | 1) const int N = 100005; struct Segtree {

線段專題 A(更新)

#include <iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=50000+5; int num[maxn]; char

hdu 4027 Can you answer these queries?(線段)(更新 但需要標記記錄剪枝)

題目:http://acm.hdu.edu.cn/showproblem.php?pid=4027 題目大意:給定100000個數,兩種操作,0 i j表示將i j這段的數字都開根號(向下取整),1

18.9.10 週一 線段經典(更新 區間求和)

敵兵佈陣  https://vjudge.net/contest/229162#problem/A C國的死對頭A國這段時間正在進行軍事演習,所以C國間諜頭子Derek和他手下Tidy又開始忙乎了。A國在海岸線沿直線佈置了N個工兵營地,Derek和Tidy的任務就是要監

POJ3264 Balanced Lineup 線段+更新

Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 32778 Accepted: 15425 Case Time Limit: 2000MS Descripti

CHOJ 4301線段+區間最大子段和

描述 給定長度為N的數列A,以及M條指令 (N≤500000, M≤100000),每條指令可能是以下兩種之一: “2 x y”,把 A[x] 改成 y。 “1 x y”,查詢區間 [x,y] 中的最大連續子段和,即 max(x≤l≤r≤y)⁡ { ∑(i=l~r) A[i

HDU 1166-敵兵佈陣狀陣列&&線段更新模板

C國的死對頭A國這段時間正在進行軍事演習,所以C國間諜頭子Derek和他手下Tidy又開始忙乎了。A國在海岸線沿直線佈置了N個工兵營地,Derek和Tidy的任務就是要監視這些工兵營地的活動情況。由於採取了某種先進的監測手段,所以每個工兵營地的人數C國都掌握的一清二楚,每個工兵營地的人數都有可能發生變動,可

Balanced Lineup線段的簡單了解)

個人 ica tree ngs mat can scanf rate class 個人心得:線段樹就是將一段序列拆分為一個個單獨的節點,不過每倆個節點又可以聯系在一起,所以就能很好的結合,比如這一題, 每次插入的時候都將這一段區間的最大最小值更新,就能大大減少時間。 這個線

POJ 3264 Balanced Lineup線段 區間最值)

lld color href .org balanced stream ios void def 題目鏈接:http://poj.org/problem?id=3264 題意:n個數,給定m個區間,求出每個區間內最大值和最小值之差 題解:區間最值問題,挺裸的一道題

G - Balanced Lineup線段+區間查詢無更新

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 orga

Balanced Lineup線段

Balanced Lineup For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the

Balanced Lineup線段—指標實現)

線段樹這一類樹狀結構一般可以用兩種形式來實現—陣列和指標。 下面學習了一下別人的指標實現的線段樹。 和陣列實現的一樣分為三步:建樹,新增值,查詢。 #include<cstdio> #include<cstring> #include<iost

Balanced Lineup線段區間查詢)

D - Balanced LineupTime Limit: 5000 MS Memory Limit: 0 KB64-bit integer IO format: %I64d , %I64u Java class name: MainDescriptionFor the d