POJ - 3264(线段树实现)

题目:poj.org/problem?id=3264

题意:求一段区间内最大值与最小值的差。

看到区间最值首先想到RMQ--ST算法。但本题出现在了kuangbin专题的线段树里。

用线段树也无思维难点,但有两个坑:

1. 查询函数中,区间不包含时的返回值。

2.用cin,cout会TLE。用c的输入方式。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<string>
 5 #include<vector>
 6 #include<list>
 7 #include<cmath>
 8 #include<set>
 9 #include<utility>
10 #include<algorithm>
11 using namespace std;
12
13
14 #define maxn 50010
15 int N, Q;
16 int cow[maxn];
17 struct node
18 {
19     int minn, maxx;
20 }tree[maxn*4];
21
22 void build_tree(int l,int r,int i)
23 {
24     if (l == r)
25     {
26         tree[i].maxx = cow[l];
27         tree[i].minn = cow[l];
28         return;
29     }
30     int mid = (l + r) / 2;
31     build_tree(l, mid, i + i);
32     build_tree(mid + 1, r, i + i + 1);
33     tree[i].maxx = max(tree[i + i].maxx, tree[i + i + 1].maxx);
34     tree[i].minn = min(tree[i + i].minn, tree[i + i + 1].minn);
35 }
36 int querymax(int tl, int tr, int l, int r, int i)
37 {
38     if (tl > r || tr < l)return 0;
39     if (tl <= l&&r <= tr)
40     {
41         return tree[i].maxx;
42     }
43     int mid = (l + r) / 2;
44     return max(querymax(tl, tr, l, mid, i + i) , querymax(tl, tr, mid + 1, r, i + i + 1));
45 }
46 int querymin(int tl, int tr, int l, int r, int i)
47 {
48     if (tl > r || tr < l)
49         return INT_MAX;//这里return返回的值要注意
50     if (tl <= l&&r <= tr)
51     {
52         return tree[i].minn;
53     }
54     int mid = (l + r) / 2;
55     return min(querymin(tl, tr, l, mid, i + i), querymin(tl, tr, mid + 1, r, i + i + 1));
56 }
57 int main()
58 {
59
60     scanf("%d%d", &N, &Q);
61     for (int i = 1; i <= N; i++)
62         scanf("%d", &cow[i]);
63     build_tree(1, N, 1);
64     int a, b;
65     for (int i = 1; i <= Q; i++)
66     {
67         scanf("%d%d", &a, &b);
68         if (a == b)
69         {
70             cout << "0" << endl; continue;
71         }
72         int qjmax = querymax(a, b, 1, N, 1);
73         int qjmin = querymin(a, b, 1, N, 1);
74         printf("%d\n", qjmax - qjmin);
75     }
76     return 0;
77 }

原文地址:https://www.cnblogs.com/nuchenghao/p/11180127.html

时间: 2024-11-04 20:44:47

POJ - 3264(线段树实现)的相关文章

poj 3264 线段树

线段树太弱了,题目逼格一高连代码都读不懂,今天开始重刷线段树,每天一题,风格用kuangbin大神和以前的,两种都写一遍 RMQ做法:poj3264 1 /* 2 POJ 3264 Balanced Lineup 3 题目意思:给定Q(1<=Q<=200000)个数A1,A2,```,AQ, 4 多次求任一区间Ai-Aj中最大数和最小数的差 5 */ 6 #include<stdio.h> 7 #include<algorithm> 8 #include<iost

POJ&mdash;&mdash;3264线段树

题目: 输入两个数(m,n),m表示牛的头数,n表示查询的个数.查询时输入两个数(x,y),表示查询范围的起始值和终止值,查询结果是,这个区间内牛重量的最大值减去牛重量的最小值,数量级为1000,000 设计每次查询的复杂度为logm. 例如, 输入: 6 3 1 7 3 4 2 5 1 5 4 6 2 2 输出: 6 3 0     分析:这道题是典型的空间换时间的题.一看到是区间的查找,我们应该想到线段树,这里有一篇我觉得写得挺好的介绍线段树的博客和大家分享一下:http://www.cnb

poj 3264 线段树 求区间最大最小值

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 rang

G - Balanced Lineup POJ 3264 (线段树+区间查询无更新)

G - Balanced Lineup Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3264 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 线段树+二分

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 rang

POJ 3264 线段树模板题

之前做的那道是区间求和的,这道题是求区间最大值和最小值之差的,感觉这道题更简单.只需在插入时把每个区间的最大值最小值求出来保存在根节点上就可以啦~\(^o^)/ Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 39475   Accepted: 18524 Case Time Limit: 2000MS Description For the daily milking, Farmer Jo

POJ 2528 (线段树+离散化) Mayor&#39;s posters

因为将每个单位都作为一个最小单元的话会爆内存的 所以,将海报的每个端点进行排序,将这些端点最为最小的区间. 毕竟是刚刚接触线段树,理解起来还有些吃力,还是那句话,题做多了慢慢就好了. 萌萌的AC代码君贴上. 1 //#define LOCAL 2 #include <iostream> 3 #include <algorithm> 4 #include <cmath> 5 using namespace std; 6 7 int n; 8 struct CPost 9

poj 2777 线段树的区间更新

Count Color Time Limit: 1000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description Chosen Problem Solving and Program design as an optional course, you are required to solve al

转载::POJ 2991 线段树+计算几何(有c++结构体操作)

POJ 2991 线段树+计算几何 (2011-02-27 21:13:44) 转载▼ 标签: 杂谈 分类: OI 话说这一题真的是很恶心很恶心,不过确实改变了我对线段树的一些看法,算是很经典的题目. 题意:有一个吊车由很多个不同长度的线段组成,一开始是一条长直线起点在(0,0),尾节点在(0,sum[n]),每条线段之间的夹角的初始值是180度.然后有一些操作a. b将第a条线段和a+1之间的夹角变成b度,经过每一次操作都要求出尾节点的坐标. 首先要用到一个计算几何的知识(没学过..请教而来)