An easy problem A

Time Limit: 2000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
Submit Status

N个数排成一列,Q个询问,每次询问一段区间内的数的极差是多少。

Input

第一行两个整数N(1≤N≤50000),Q(1≤Q≤200000)。接下来一行N个整数a1 a2 a3 ....an,(1≤ai≤1000000000)。接下来Q行,每行两个整数L,R(1≤L≤R≤N)。

Output

对于每个询问输出一行,一个整数表示区间内的极差。

Sample input and output

Sample Input Sample Output
5 3
3 2 7 9 10
1 5
2 3
3 5
8
5
3

题目链接:http://acm.uestc.edu.cn/#/contest/show/155

分析:线段树点更新裸题,继续复习线段树,这题要算的是极差,只需要建树和查询两部分,无需更新,所以建树的时候只要去求最大值和最小值即可,然后极差一减得出答案!

下面给出AC代码:

 #include <bits/stdc++.h>
using namespace std;
const int N=;
struct Node
{
int l,r,minn,maxn;
}tree[N<<];
void build(int l,int r,int pos)
{
tree[pos].l=l;
tree[pos].r=r;
if(l==r)
{
scanf("%d",&tree[pos].maxn);
tree[pos].minn=tree[pos].maxn;
return;
}
int mid=(l+r)/;
build(l,mid,pos*);
build(mid+,r,pos*+);
tree[pos].maxn=max(tree[pos*].maxn,tree[pos*+].maxn);
tree[pos].minn=min(tree[pos*].minn,tree[pos*+].minn);
}
int query1(int l,int r,int pos)
{
if(tree[pos].l==l&&tree[pos].r==r)
return tree[pos].minn;
int mid=(tree[pos].l+tree[pos].r)/;
if(r<=mid)
return query1(l,r,pos*);
else if(l>mid)
return query1(l,r,pos*+);
else return min(query1(l,mid,pos*),query1(mid+,r,pos*+));
}
int query2(int l,int r,int pos)
{
if(tree[pos].l==l&&tree[pos].r==r)
return tree[pos].maxn;
int mid=(tree[pos].l+tree[pos].r)/;
if(r<=mid)
return query2(l,r,pos*);
else if(l>mid)
return query2(l,r,pos*+);
else return max(query2(l,mid,pos*),query2(mid+,r,pos*+));
}
int main()
{
int x,y;
scanf("%d%d",&x,&y);
build(,x,);
while(y--)
{
int p,q;
scanf("%d%d",&p,&q);
printf("%d\n",query2(p,q,)-query1(p,q,));
}
return ;
}

UESTC 1591 An easy problem A【线段树点更新裸题】的更多相关文章

  1. hihoCoder #1078 : 线段树的区间修改(线段树区间更新板子题)

    #1078 : 线段树的区间修改 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于小Ho表现出的对线段树的理解,小Hi表示挺满意的,但是满意就够了么?于是小Hi将问题 ...

  2. HDU 5475(2015 ICPC上海站网络赛)--- An easy problem(线段树点修改)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5475 Problem Description One day, a useless calculato ...

  3. ACM学习历程—HDU5475 An easy problem(线段树)(2015上海网赛08题)

    Problem Description One day, a useless calculator was being built by Kuros. Let's assume that number ...

  4. hdu 4578 Transformation 线段树多种操作裸题

    自己写了一个带结构体的WA了7.8次 但是测了几组小数据都对..感觉问题应该出在模运算那里.写完这波题解去对拍一下. 以后线段树绝不写struct!一般的struct都带上l,r 但是一条线段的长度确 ...

  5. POJ 3667 线段树区间合并裸题

    题意:给一个n和m,表示n个房间,m次操作,操作类型有2种,一种把求连续未租出的房间数有d个的最小的最左边的房间号,另一个操作时把从x到x+d-1的房间号收回. 建立线段树,值为1表示未租出,0为租出 ...

  6. FZU Problem 2171 防守阵地 II (线段树区间更新模板题)

    http://acm.fzu.edu.cn/problem.php?pid=2171 成段增减,区间求和.add累加更新的次数. #include <iostream> #include ...

  7. HDU 1698 Just a Hook (线段树区间更新入门题)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. HDU1698:Just a Hook(线段树区域更新模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 Problem Description In the game of DotA, Pudge’s meat ...

  9. poj3468 A Simple Problem with Integers(线段树区间更新)

    https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...

随机推荐

  1. Android活动生命周期

    任务(Task) Android 是使用任务(Task)来管理活动的,一个任务就是一组存放在栈里的活动的集合,这个栈也被称作返回栈(Back Stack).在默认情况下,每当我们启动了一个新的活动,它 ...

  2. 机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 2)

    ##机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 2)---#####注:机器学习资料[篇目一](https://github.co ...

  3. Spring+MVC+Mybatis整合

    本文是对慕课网上"搞定SSM开发"路径的系列课程的总结,详细的项目文档和课程总结放在github上了.点击查看 什么是秒杀业务 网站售卖某产品时,规定在某个日期开始售卖限量的产品, ...

  4. JavaWeb之数据源连接池(4)---自定义数据源连接池

    [续上文<JavaWeb之数据源连接池(3)---Tomcat>] 我们已经 了解了DBCP,C3P0,以及Tomcat内置的数据源连接池,那么,这些数据源连接池是如何实现的呢?为了究其原 ...

  5. Swift学习第一天--面向过程

    //: Playground - noun: a place where people can play import UIKit //---------------------- Hello wor ...

  6. JavaScript的DOM编程--11--插入节点

    插入节点: 1). insertBefore(): 把一个给定节点插入到一个给定元素节点的给定子节点的前面 var reference = element.insertBefore(newNode,t ...

  7. dubbo源码—dubbo简介

    dubbo是一个RPC框架,应用方像使用本地service一样使用dubbo service.dubbo体系架构 上图中的角色: 最重要的是consumer.registry和provider con ...

  8. python sklearn PCA源码阅读:参数n_components的设置(设为‘mle’出错的原因)

    在介绍n_components参数之前,首先贴一篇PCA参数详解的文章:http://www.cnblogs.com/akrusher/articles/6442549.html. 按照文章中对于n_ ...

  9. ubuntu16.04编译安装php7.2

    1,下载解压 tar xf php-7.2.0.tar.gz cd php-7.2.0/ 2,安装必要的库 sudo apt-get install libxml2-devsudo apt-get i ...

  10. 部署Tomcat服务时,解决Cannot invoke Tomcat Manager 异常

    最近,在使用Jenkins对工程一键部署的时候,出现调用Tomcat Manager 异常,对其解决方案特记于次. 异常信息 可能存在的异常:(1)Cannot invoke Tomcat manag ...