2017-10-07 17:54:55

writer:pprp

/*
@theme: 最基本的线段树
@writer:pprp
@end:17:38
@attention:记录的数组应该从1开始,不能从0开始
*/ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int maxn = +;
const int INF = 0x3f3f3f3f; struct tree
{
int nmax,nmin;
int r,l;
};
tree tt[maxn*];
int nMax,nMin; int a[maxn];
void build(int l, int r, int i)
{
tt[i].l = l;
tt[i].r = r;
if(l == r)
{
tt[i].nmin = tt[i].nmax = a[l];
return;
}
int mid = (l+r)>>;
build(l,mid,*i);
build(mid+,r,*i+);
tt[i].nmin = min(tt[i<<].nmin,tt[i<<|].nmin);
tt[i].nmax = max(tt[i<<].nmax,tt[i<<|].nmax);
}
void query(int i, int l, int r)
{
if(tt[i].nmax <= nMax && tt[i].nmin >= nMin)
return ;
if(tt[i].l == l && tt[i].r == r)
{
nMax = max(tt[i].nmax,nMax);
nMin = min(tt[i].nmin,nMin);
return;
}
int mid = (tt[i].l+tt[i].r) >> ;
if(r <= mid)
query(i<<,l,r);
else if(l > mid)
query(i<<|,l,r);
else
{
query(i<<,l,mid);
query(i<<|,mid+,r);
}
} int main()
{
// freopen("in.txt","r",stdin);
cin.sync_with_stdio(false);
int n, q;
int l, r;
while(cin >> n >> q)
{
for(int i = ; i <= n ; i++)
{
cin>>a[i];
}
build(,n,);
for(int i = ; i <= q; i++)
{
cin >> l >> r;
nMax = -INF,nMin = INF;
query(,l,r); cout << nMax-nMin << endl;
}
} return ;
}

解题报告:poj 3264 最基本的线段树的更多相关文章

  1. POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 ...

  2. POJ 3264 Balanced Lineup 【线段树】

    <题目链接> 题目大意: 求给定区间内最大值与最小值之差. 解题分析: 线段树水题,每个节点维护两个值,分别代表该区间的最大和最小值即可. #include <cstdio> ...

  3. poj 3264:Balanced Lineup(线段树,经典题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32820   Accepted: 15447 ...

  4. poj 3264 Balanced Lineup (线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 42489   Accepted: 20000 ...

  5. POJ 3264 Balanced Lineup 【线段树/区间最值差】

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 62103 Accepted: 29005 Cas ...

  6. POJ 3264 Balanced Lineup【线段树】

    题意:给出n个数,a1,a2,a3,---,an,再给出q次询问区间al到ar之间的最大值和最小值的差 学习线段树的第一道题目 学习的这一篇 http://www.cnblogs.com/kuangb ...

  7. POJ - 3264 Balanced Lineup(线段树或RMQ)

    题意:求区间最大值-最小值. 分析: 1.线段树 #include<cstdio> #include<cstring> #include<cstdlib> #inc ...

  8. 暑期训练狂刷系列——poj 3264 Balanced Lineup(线段树)

    题目连接: http://poj.org/problem?id=3264 题目大意: 有n个数从1开始编号,问在指定区间内,最大数与最小数的差值是多少? 解题思路: 在节点中存储max,min,然后查 ...

  9. POJ 3264 Balanced Lineup (线段树查找最大最小值)

    http://poj.org/problem?id=3264 题意:给你一个长度为n的序列a[N] (1 ≤ N ≤ 50000),询问Q(1 ≤ Q ≤ 200000) 次,每次输出[L, R]区间 ...

随机推荐

  1. Storm-源码分析-Topology Submit-Supervisor

    mk-supervisor (defserverfn mk-supervisor [conf shared-context ^ISupervisor isupervisor] (log-message ...

  2. vue中回车键登录

    created() { let that = this; document.onkeypress = function(e) { var keycode = document.all ? event. ...

  3. 原!linux 监控 jar定时任务 挂了重启 脚本

    #!/bin/bash time=$(date "+%Y-%m-%d %H:%M:%S") echo "monitor start at: ${time}" P ...

  4. win32调试工具原理OutputDebugString以及如何获取输出信息

    在应用程序和调试器之间传递数据是通过一个 4KB 大小的共享内存块完成的,并有一个互斥量和两个事件对象用来保护对他的访问.下面就是相关的四个内核对象: 对象名称 对象类型 DBWinMutex Mut ...

  5. NSUserDefaults保存对象数组报错

    在使用NSUserDefaults的时候插入数据有时候会报以下错误:Attempt to set a non-property-list objec 这种错误的原因是插入了不识别的数据类型,NSUse ...

  6. celery-rabbitmq 安装部署

    一:Python安装 1.下载python3源码 wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz 2.解压 tar xf P ...

  7. 华为交换机VRP用户界面配置及Telnet登录实验

    user privilege level level 设置使用以上用户界面登录后的用户级别 5 acl acl-number { inbound | outbound } (可选)在用户界面上应用AC ...

  8. Mac 升级 OpenSSL

    [转载自 https://blog.csdn.net/focusjava/article/details/51179297 ] [升级Mac的openssl] 终端下 openssl version ...

  9. phpcms 添加稿件到栏目 add_content

    private $db; private $content_model; public function __construct() { parent::__construct (); $this-& ...

  10. Go实现查找目录下(包括子目录)替换文件内容

    [功能] 按指定的目录查找出文件,如果有子目录,子目录也将进行搜索,将其中的文件内容进行替换. [缺陷] 1. 没有过滤出文本文件 2. 当文件过大时,效率不高 [代码] package main i ...