暑期训练狂刷系列——poj 3264 Balanced Lineup(线段树)
题目连接:
http://poj.org/problem?id=3264
题目大意:
有n个数从1开始编号,问在指定区间内,最大数与最小数的差值是多少?
解题思路:
在节点中存储max,min,然后查询指定区间的max、min。
- #include <cstdio>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- const int maxn = ;
- const int INF = 0x3f3f3f3f;
- struct node
- {
- int L, R;
- int Min, Max;
- int Mid()
- {
- return (L + R) / ;
- }
- };
- node tree[maxn];
- int Min, Max;
- void build(int root, int l, int r)
- {
- tree[root].L = l;
- tree[root].R = r;
- tree[root].Min = INF;
- tree[root].Max = -INF;
- if (l == r)
- return ;
- build (*root+, l, tree[root].Mid());
- build (*root+, tree[root].Mid()+, r);
- }
- void insert (int root, int x, int s)
- {
- tree[root].Min = min (tree[root].Min, s);
- tree[root].Max = max (tree[root].Max, s);
- if (tree[root].L == tree[root].R)
- return ;
- if (x <= tree[root].Mid())
- insert (*root+, x, s);
- else if (x > tree[root].Mid())
- insert (*root+, x, s);
- }
- void query (int root, int s, int e)
- {
- if (tree[root].L == s && tree[root].R == e)
- {
- Min = min (Min, tree[root].Min);
- Max = max (Max, tree[root].Max);
- return ;
- }
- if (e <= tree[root].Mid())
- query (*root+, s, e);
- else if (s > tree[root].Mid())
- query (*root+, s, e);
- else
- {
- query (*root+, s, tree[root].Mid());
- query (*root+, tree[root].Mid()+, e);
- }
- }
- int main ()
- {
- int n, q, num;
- while (scanf ("%d %d", &n, &q) != EOF)
- {
- build(, , n);
- for (int i=; i<=n; i++)
- {
- scanf ("%d", &num);
- insert (, i, num);
- }
- while (q --)
- {
- int s, e;
- scanf ("%d %d", &s, &e);
- Min = INF;
- Max = -INF;
- query (, s, e);
- printf ("%d\n", Max - Min);
- }
- }
- return ;
- }
暑期训练狂刷系列——poj 3264 Balanced Lineup(线段树)的更多相关文章
- 暑期训练狂刷系列——poj 3468 A Simple Problem with Integers (线段树+区间更新)
题目连接: http://poj.org/problem?id=3468 题目大意: 给出n个数,有两种操作: 1:"C a b c",[a,b]中的每一个数都加上c. 2:&qu ...
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- POJ 3264 Balanced Lineup 线段树RMQ
http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...
- POJ 3264 Balanced Lineup 线段树 第三题
Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...
- [POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
- POJ 3264 Balanced Lineup (线段树)
Balanced Lineup For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the s ...
- POJ - 3264 Balanced Lineup 线段树解RMQ
这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...
- 【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34140 Accepted: 16044 ...
- 暑期训练狂刷系列——Hdu 1698 Just a Hook (线段树区间更新)
题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1698 题目大意: 有一个钩子有n条棍子组成,棍子有铜银金三种组成,价值分别为1,2,3.为了对付每场 ...
随机推荐
- C# 9.0新特性
CandidateFeaturesForCSharp9 看到标题,是不是认为我把标题写错了?是的,C# 8.0还未正式发布,在官网它的最新版本还是Preview 5,通往C#9的漫长道路却已经开始.前 ...
- Openwrt挂载NTFS硬盘提示“只读”错误的解决方法!
Openwrt是基于Linux代码编写,只支持NTFS格式硬盘的只读权限,否则当挂载的NTFS硬盘写入超过2M左右,就会出现"error:read-only file system" ...
- 配置activeMQ
一.加入以下的库 并配置好路径 ws2_32.lib;Mswsock.lib;cppunit.lib;libapr-1.lib;libapriconv-1.lib;libaprutil-1.lib;l ...
- coco2dx新建项目报错,ld: -pie can only be used when targeting iOS 4.2 or later clang: error: linker command
在新建cocos2d-x以后,执行发现下面错误: ld: -pie can only be used when targeting iOS 4.2 or later clang: error: lin ...
- android 不同进程间的调用 AIDL 实现通讯服务
android 不同进程间的调用 AIDL 实现通讯服务 近期对aidl android 不同进程间的调用,不同运用间的调用做了一些尝试: 过程例如以下: 1:首先在要被调用的程序里写好 ...
- [转] logback logback.xml常用配置详解(一)<configuration> and <logger>
转载文章:原文出处:http://aub.iteye.com/blog/1101260 详细整理了logback常用配置, 不是官网手册的翻译版,而是使用总结,旨在更快更透彻的理解其配置 根节点< ...
- ZOJ 3962 E.Seven Segment Display / The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple E.数位dp
Seven Segment Display Time Limit: 1 Second Memory Limit: 65536 KB A seven segment display, or s ...
- SSH无密码验证可能出现的问题
雪影工作室版权所有,转载请注明[http://blog.csdn.net/lina791211] 一.安装和启动SSH协议 假设没有安装ssh和rsync,可以通过下面命令进行安装. sudo apt ...
- select case when if
select case when if 的一些用法 - 马丁传奇 - 博客园 https://www.cnblogs.com/martinzhang/p/3220595.html Write a SQ ...
- 强类型DataSet (2011-12-30 23:16:59)转载▼ 标签: 杂谈 分类: Asp.Net练习笔记 http://blog.sina.com.cn/s/blog_9d90c4140101214w.html
强类型DataSet (2011-12-30 23:16:59) 转载▼ 标签: 杂谈 分类: Asp.Net练习笔记 using System; using System.Collections.G ...