POJ 3264 Balanced Lineup 【线段树】

本文介绍了一种使用线段树解决区间最大值与最小值差值问题的方法,并提供了完整的C++实现代码。通过构建两棵线段树分别维护区间的最小值和最大值,可以高效地查询任意指定区间内的高度差。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 50004 Accepted: 23434
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
Source

USACO 2007 January Silver
用线段树查询每个区间上最大值与最小值的差值,用一个线段树维护最小值,一个维护最大值。

#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
//#include <bits/stdc++.h>
//#define LOACL
#define space " "
using namespace std;
typedef long long LL;
typedef __int64 Int;
typedef pair<int, int> paii;
const int INF = 0x3f3f3f3f;
const double ESP = 1e-5;
const double PI = acos(-1.0);
const int MOD = 1e9 + 7;
const int MAXN = 200000 + 10;
int data[MAXN];
struct node {
    int l, r, value;
} segmin[MAXN], segmax[MAXN];
int build_segm_max(int x, int lson, int rson) {
    segmax[x].l = lson;
    segmax[x].r = rson;
    if (lson != rson) {
        int a = build_segm_max(x << 1, lson, (rson + lson)/2);
        int b = build_segm_max((x << 1) + 1, (rson + lson)/2 + 1, rson);
        return segmax[x].value = max(a, b);
    }
    return segmax[x].value = data[lson];
}
int build_segm_min(int x, int lson, int rson) {
    segmin[x].l = lson;
    segmin[x].r = rson;
    if (lson != rson) {
        int a = build_segm_min(x << 1, lson, (rson + lson)/2);
        int b = build_segm_min((x << 1) + 1, (rson + lson)/2 + 1, rson);
        return segmin[x].value = min(a, b);
    }
    return segmin[x].value = data[lson];
}
int query_seg_max(int x, int lson, int rson) {
    if (segmax[x].l > rson || segmax[x].r < lson) return 0;
    if (segmax[x].l >= lson && segmax[x].r <= rson) return segmax[x].value;
    int a = query_seg_max(x << 1, lson, rson);
    int b = query_seg_max((x << 1) + 1, lson, rson);
    return max(a, b);
}
int query_seg_min(int x, int lson, int rson) {
    if (segmin[x].l > rson || segmin[x].r < lson) return INF;
    if (segmin[x].l >= lson && segmin[x].r <= rson) return segmin[x].value;
    int a = query_seg_min(x << 1, lson, rson);
    int b = query_seg_min((x << 1) + 1, lson, rson);
    return min(a, b);
}
int main() {
    int N, Q, a, b;
    scanf("%d%d", &N, &Q);
    for (int i = 1; i <= N; i++) scanf("%d", &data[i]);
    build_segm_max(1, 1, N); build_segm_min(1, 1, N);
    for (int i = 0; i < Q; i++) {
        scanf("%d%d", &a, &b);
        printf("%d\n", query_seg_max(1, a, b) - query_seg_min(1, a, b));
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/cniwoq/p/6770747.html

内容概要:本文深入探讨了Kotlin语言在函数式编程和跨平台开发方面的特性和优势,结合详细的代码案例,展示了Kotlin的核心技巧和应用场景。文章首先介绍了高阶函数和Lambda表达式的使用,解释了它们如何简化集合操作和回调函数处理。接着,详细讲解了Kotlin Multiplatform(KMP)的实现方式,包括共享模块的创建和平台特定模块的配置,展示了如何通过共享业务逻辑代码提高开发效率。最后,文章总结了Kotlin在Android开发、跨平台移动开发、后端开发和Web开发中的应用场景,并展望了其未来发展趋势,指出Kotlin将继续在函数式编程和跨平台开发领域不断完善和发展。; 适合人群:对函数式编程和跨平台开发感兴趣的开发者,尤其是有一定编程基础的Kotlin初学者和中级开发者。; 使用场景及目标:①理解Kotlin中高阶函数和Lambda表达式的使用方法及其在实际开发中的应用场景;②掌握Kotlin Multiplatform的实现方式,能够在多个平台上共享业务逻辑代码,提高开发效率;③了解Kotlin在不同开发领域的应用场景,为选择合适的技术栈提供参考。; 其他说明:本文不仅提供了理论知识,还结合了大量代码案例,帮助读者更好地理解和实践Kotlin的函数式编程特性和跨平台开发能力。建议读者在学习过程中动手实践代码案例,以加深理解和掌握。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值