poj 3264 【线段树】
此题为入门级线段树
题意:给定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 【线段树】的更多相关文章
- POJ——3264线段树
题目: 输入两个数(m,n),m表示牛的头数,n表示查询的个数.查询时输入两个数(x,y),表示查询范围的起始值和终止值,查询结果是,这个区间内牛重量的最大值减去牛重量的最小值,数量级为1000,00 ...
- poj 3264 线段树 求区间最大最小值
Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same ...
- POJ - 3264 线段树模板题 询问区间最大最小值
这是线段树的一个模板题,给出一串数字,然后询问区间的最大最小值. 这个其实很好办,只需把线段树的节点给出两个权值,一个是区间的最小值,一个是区间的最大值,初始化为负无穷和正无穷,然后通过不断地输入节点 ...
- POJ 3264 线段树入门解题报告
题意:给n个值, Q次询问, 每次询问给定一个区间, 要求输出该区间最大最小值之差 思路:暴力的话每次询问都要遍历多次for循环一定会超时, 用线段树记录区间的信息(左边界右边界, 该区间最大值最小值 ...
- poj 3264 线段树
题目意思:给定Q(1<=Q<=200000)个数A1,A2,```,AQ, 多次求任一区间Ai-Aj中最大数和最小数的差 线段树太弱了,题目逼格一高连代码都读不懂,今天开始重刷线段树,每天 ...
- POJ 3264 线段树 ST
题意:给你一个数列,从中挑一段,问你这段数的最大值减最小值是多少. 思路:线段树. // by Sirius_Ren #include <cstdio> #include <algo ...
- G - Balanced Lineup POJ - 3264 线段树最大最小值区间查询模版题
题意 给出一个序列 每次查询区间的max-min是多少 思路:直接维护max 和min即可 写两个query分别查最大最小值 #include<cstdio> #include< ...
- poj 2886 线段树+反素数
Who Gets the Most Candies? Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 12744 Acc ...
- poj 3468(线段树)
http://poj.org/problem?id=3468 题意:给n个数字,从A1 …………An m次命令,Q是查询,查询a到b的区间和,c是更新,从a到b每个值都增加x.思路:这是一个很明显的线 ...
- POJ 2828 线段树(想法)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 15422 Accepted: 7684 Desc ...
随机推荐
- oracle/node-oracledb 数据库驱动 与 Meteor 驱动包!
oracle/node-oracledb: https://github.com/oracle/node-oracledb Oracle 官方维护. metstrike/meteor-oracle ...
- codecademy-command line_filesystem
$:shell prompt (命令提示符) In the terminal, first you see $. This is called a shell prompt. It appears w ...
- Android Broadcast 和 iOS Notification
感觉以上2个机能有许多相似之处,作个记录,待研究!
- TokuDB 引擎安装测试
背景: TokuDB引擎是有Tokutek开发的一个数据库存储引擎,在设计之初便引入了独特的索引算法,在其官网测试的文章中看到TokuDB性能比InnoDB高出很多. MySQL是一个插件式的数据库, ...
- 【Android】如何将eclipse工程导入studio
1.eclipse工程右键->export->Generate Gradle build files 2.studio->New->import project->选择工 ...
- [ 转]Collections.unmodifiableList方法的使用与场景
在公司接触到Collections.unmodifiableList(List<? extends T> list)) 觉得用法挺特殊的,所以学习了下,简单而言,看名字就知道,将参数中的L ...
- 常用iOS第三方库以及XCode插件介绍
第三方库 CocoaPod CocoaPod并不是iOS上的第三方库 而是大名鼎鼎的第三方库的管理工具 在CocoaPod没有出现之前 第三方库的管理是非常痛苦的 尤其是一些大型的库(比如nimbus ...
- 【leetcode】 Unique Path ||(easy)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【leetcode】 Search a 2D Matrix (easy)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- iOS runtime 的经典作用