此题为入门级线段树

题意:给定Q(1<=Q<=200000)个数A1A2…AQ,多次求任一区间Ai-Aj中最大数和最小数的差

#include<algorithm>
#include<cstdio>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
typedef long long LL;
const int INF = 0x7FFFFFFF;
const int maxn = 1e3 + 10; int minV = INF;
int maxV = -INF;
struct Node
{
int L, R;
int minV, maxV;
//Node *pLeft, *pRight;
int Mid()
{
return (L + R) / 2;
}
};
Node tree[800010];//4倍叶子节点的数量就够 void BuildTree(int root, int L, int R)
{
tree[root].L = L;
tree[root].R = R;
tree[root].minV = INF;
tree[root].maxV = -INF;
if (L != R)
{
BuildTree(2 * root + 1, L, (L + R) / 2);
BuildTree(2 * root + 2, (L + R) / 2 + 1, R);
}
} void Insert(int root, int i, int v)
//将第i个数,其值为v,插入线段树
{
if (tree[root].L == tree[root].R)
{
//成立则亦有tree[root].R==i
tree[root].minV = tree[root].maxV = v;
return;
}
tree[root].minV = min(tree[root].minV, v);
tree[root].maxV = max(tree[root].maxV, v);
if (i <= tree[root].Mid())
Insert(2 * root + 1, i, v);
else
Insert(2 * root + 2, i, v);
} void Query(int root, int s, int e)
//查询区间[s,e]中的最小值和最大值,如果更优就记在全局变量里
{
if (tree[root].minV >= minV&&tree[root].maxV <= maxV)
return;
if (tree[root].L == s&&tree[root].R == e)
{
minV = min(minV, tree[root].minV);
maxV = max(maxV, tree[root].maxV);
return;
}
if (e <= tree[root].Mid())
Query(2 * root + 1, s, e);
else if (s > tree[root].Mid())
Query(2 * root + 2, s, e);
else
{
Query(2 * root + 1, s, tree[root].Mid());
Query(2 * root + 2, tree[root].Mid() + 1, e);
}
} int main()
{
int n, q, h;
int i, j, k;
scanf("%d%d", &n, &q);
BuildTree(0, 1, n);
for (i = 1; i <= n; i++)
{
scanf("%d", &h);
Insert(0, i, h);
}
for (i = 0; i < q; i++)
{
int s, e;
scanf("%d%d", &s, &e);
minV = INF;
maxV = -INF;
Query(0, s, e);
printf("%d\n", maxV - minV);
}
return 0;
}

poj 3264 【线段树】的更多相关文章

  1. POJ——3264线段树

    题目: 输入两个数(m,n),m表示牛的头数,n表示查询的个数.查询时输入两个数(x,y),表示查询范围的起始值和终止值,查询结果是,这个区间内牛重量的最大值减去牛重量的最小值,数量级为1000,00 ...

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

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

  3. POJ - 3264 线段树模板题 询问区间最大最小值

    这是线段树的一个模板题,给出一串数字,然后询问区间的最大最小值. 这个其实很好办,只需把线段树的节点给出两个权值,一个是区间的最小值,一个是区间的最大值,初始化为负无穷和正无穷,然后通过不断地输入节点 ...

  4. POJ 3264 线段树入门解题报告

    题意:给n个值, Q次询问, 每次询问给定一个区间, 要求输出该区间最大最小值之差 思路:暴力的话每次询问都要遍历多次for循环一定会超时, 用线段树记录区间的信息(左边界右边界, 该区间最大值最小值 ...

  5. poj 3264 线段树

    题目意思:给定Q(1<=Q<=200000)个数A1,A2,```,AQ, 多次求任一区间Ai-Aj中最大数和最小数的差 线段树太弱了,题目逼格一高连代码都读不懂,今天开始重刷线段树,每天 ...

  6. POJ 3264 线段树 ST

    题意:给你一个数列,从中挑一段,问你这段数的最大值减最小值是多少. 思路:线段树. // by Sirius_Ren #include <cstdio> #include <algo ...

  7. G - Balanced Lineup POJ - 3264 线段树最大最小值区间查询模版题

    题意 给出一个序列  每次查询区间的max-min是多少 思路:直接维护max 和min即可  写两个query分别查最大最小值 #include<cstdio> #include< ...

  8. poj 2886 线段树+反素数

    Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 12744   Acc ...

  9. poj 3468(线段树)

    http://poj.org/problem?id=3468 题意:给n个数字,从A1 …………An m次命令,Q是查询,查询a到b的区间和,c是更新,从a到b每个值都增加x.思路:这是一个很明显的线 ...

  10. POJ 2828 线段树(想法)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 15422   Accepted: 7684 Desc ...

随机推荐

  1. MongoDB-Getting Started with the C# Driver

    简介:本文仅提供快速入门级别的使用C# Driver操作MongoDB,高手跳过 Downloading the C# Driver 猛击下载 添加相关的dll引用 MongoDB.Bson.dll ...

  2. AngularJS概述&指令

    AngularJS 指令 AngularJS 应用组成如下: View(视图), 即 HTML. Model(模型), 当前视图中可用的数据. Controller(控制器), 即 JavaScrip ...

  3. Largest BST Subtree

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  4. Qt Connect 信号 槽

    信号和槽机制是 QT 的核心机制 .信号和槽是一种高级接口,应用于对象之间的通信,它是 QT 的核心特性,也是 QT 区别于其它工具包的重要地方.信号和槽是 QT 自行定义的一种通信机制,它独立于标准 ...

  5. C++中Cstring、wstring 和string互相转换总结

    通过前一篇文章<C++中string,wstring,CString的基本概念和用法>,对Cstring.wstring 和string有了一个了解.string是C++提供的标准字符串操 ...

  6. PHP中文字符串编码转换

    2016年2月26日 16:47:13 星期五 情景: PHP从csv导入数据时乱码 $name = mb_convert_encoding($name, 'UTF-8', 'ASCII,GBK,GB ...

  7. 用dygraphs图表分析xdebug的trace结果

    2015年12月1日 19:44:23 推荐这一篇用百度的ECharts图表工具 本文用到的js图表库: dygraphs 注: 测试数据量 19108个数据点, 最好将数据的量级调小, 这样渲染会很 ...

  8. 禁用visual studio实时调试器

    最近每次开机时都会出来一个visual Studio实时调试器,报“发生了未处理的异常(‘System ComponentModel.Win32Exception’,发生位置是 BSSocketSms ...

  9. XE 的程序升级 XE5 问题处理记录

    XE 的程序升级 XE5 问题处理记录 1.  [dcc32 Fatal Error] frxClass.pas(3556): F1026 File not found: 'xxxxx\Registr ...

  10. Delphi XE5 常见问题解答

    Delphi XE5 常见问题解答 有关于新即时试用的问题吗?请看看 RAD Studio 即时试用常见问答. 常见问题 什么是 Delphi? Embarcadero? Delphi? XE5 是易 ...