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 ...
随机推荐
- Git是如何存储对象的
原文:http://gitbook.liuhui998.com/7_1.html 一.前言 所有的对象都以SHA值为索引用gzip格式压缩存储, 每个对象都包含了对象类型, 大小和内容. Git中存在 ...
- 一个Try多个Catch需要注意的事项
一个程序包含一个try块和两个catch块,两个catch子句都有能力捕捉一个try块发出的异常,若两个catch子句次序不同时程序结果会发生变化吗? 一个try块后有两个catch块,这很正常,因为 ...
- React 源码解读参考,理解原理。
Rubix - ReactJS Powered Admin Template 文档: http://rubix-docs.sketchpixy.com/ ===================== ...
- Python之异常追踪模块:traceback
正常时输出追踪信息: import traceback def stack(): print 'The python stack:' traceback.print_stack() from twis ...
- 如何恢复低版本的FlashPlayer
本人做页游开发时,游戏用户那边经常会遇到一些很奇怪的问题.比如: 1.用户进入游戏,只显示游戏部分界面,chrome浏览器是正常的,就IE死活不行. 2.进入游戏时白屏或者一直加载不上. 3.玩游戏时 ...
- 渲染物体到一张UITexture上
把这个脚本挂到一个Camera上 using UnityEngine; using System.Collections; [RequireComponent(typeof(Camera))] pub ...
- swfit中的同步锁
swfit 中 objective-c 中的@syncronized 这个东西不能用了,应该用 objc_sync_enter(self) 代码 objc_sync_exit(self) 代替!
- FindinFiles - Windows文件内查找插件
FindInFiles for Windows 今天分享一个不错的插件工具:FindInFiles.如其名,其功能和Visual Studio的Ctrl+H快捷键类似,方便Windows使用者在资源管 ...
- asp.net 短信群发
protected void Btn_Save_Click(object sender, EventArgs e) { string Contents = this.Txt_SmsContents.T ...
- Web上的支持的图片格式以及它们之间的区别
一.GIF(图形交换格式) GIF格式的图片最多只能保存256中颜色,该格式支持透明色,支持动画效果. 二.JPEG(联合图像专家组) JPEG格式不支持透明色及动画,颜色可达1670种. 三.PNG ...