日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

POJ 3264 Balanced Lineup 【线段树】

發(fā)布時間:2025/3/17 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 3264 Balanced Lineup 【线段树】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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
用線段樹查詢每個區(qū)間上最大值與最小值的差值,用一個線段樹維護最小值,一個維護最大值。

#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

總結

以上是生活随笔為你收集整理的POJ 3264 Balanced Lineup 【线段树】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。