1082 - Array Queries
Time Limit: 3 second(s) Memory Limit: 64 MB

Given an array with N elements, indexed from 1 to N. Now you will be given some queries in the form I J, your task is to find the minimum value from index I toJ.

Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

The first line of a case is a blank line. The next line contains two integers N (1 ≤ N ≤ 105)q (1 ≤ q ≤ 50000). The next line contains N space separated integers forming the array. There integers
range in [0, 105].

The next q lines will contain a query which is in the form I J (1 ≤ I ≤ J ≤ N).

Output

For each test case, print the case number in a single line. Then for each query you have to print a line containing the minimum value between index I and J.

Sample Input

Output for Sample Input

2

5 3

78 1 22 12 3

1 2

3 5

4 4

1 1

10

1 1

Case 1:

1

3

12

Case 2:

10

Note

Dataset is huge. Use faster I/O methods.


哈哈,第一次在light oj上做题,居然碰到了这个水题,,虽然还不知道这个oj的含义,不过应该蛮有影响力的,这题很适合初学线段树的人,虽然我学的不是很好,不过对于这种简单线段树的题,还是不在话下的;

其实看样例就可以知道他是什么意思了,给定一个数组,求某个区间的最小值,这就是典型的线段树了,连区间更新都不用,纯粹是查询;初学线段树的时候记得父亲节点是储存两个子节点的和,我们就可以用父亲节点来储存子节点的最小值了,一个简单的建树函数和一个查询函数就搞定了;看代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=100000+10;
int n,m,s[N];
struct node
{
int l,r,n;
}a[N<<2];
void build(int l,int r,int k)
{
a[k].l=l,a[k].r=r;
if(l==r)
{
a[k].n=s[l];
return ;
}
int mid=(l+r)/2;
build(l,mid,2*k);
build(mid+1,r,2*k+1);
a[k].n=min(a[k*2].n,a[k*2+1].n);//回溯将最小值储存在根节点上,然后层层回溯上去;
}
int query(int l,int r,int k)
{
if(a[k].l==l&&a[k].r==r)
return a[k].n;
int mid=(a[k].l+a[k].r)/2;
if(l>mid) return query(l,r,k*2+1);
if(r<=mid) return query(l,r,2*k);
return min(query(l,mid,2*k),query(mid+1,r,2*k+1));
}
int main()
{
int t;
scanf("%d",&t);
int t1=t;
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&s[i]);
build(1,n,1);
printf("Case %d:\n",t1-t);
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
if(a==b)
printf("%d\n",s[a]);
else
printf("%d\n",query(a,b,1));
}
}
return 0;
}

代码写出来相比于其他线段树来讲已经是很简洁的代码了~~

Light OJ-1082 - Array Queries,线段树区间查询最大值,哈哈,水过~~的更多相关文章

  1. ACM_3n+1问题(克拉兹问题+线段树区间查询最大值)

    3n+1问题 Time Limit: 2000/1000ms (Java/Others) Problem Description: 考虑如下的序列生成算法:从整数n开始,如果n是偶数,把它除以2:如果 ...

  2. 线段树 区间查询最大值,单体修改 hdu 1754

    #include<cstdio> #include<algorithm> #include<string.h> #include<math.h> #in ...

  3. HDU 1754 I Hate It(线段树区间查询,单点更新)

    描述 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感.不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老 ...

  4. POJ_3468 A Simple Problem with Integers 【线段树区间查询+修改】

    一.题目 POJ3468 二.分析 裸的线段树区间查询+修改. 三.AC代码 #include <cstdio> #include <iostream> #include &l ...

  5. ACM_最值差(线段树区间查询最值)

    最值差 Time Limit: 2000/1000ms (Java/Others) Problem Description: 给定N个数A1A2A3A4...AN.求任意区间Ai到Aj中的最大数与最小 ...

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

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

  7. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  8. Light oj-1100 - Again Array Queries,又是这个题,上次那个题用的线段树,这题差点就陷坑里了,简单的抽屉原理加暴力就可以了,真是坑~~

                                                                              1100 - Again Array Queries ...

  9. HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)

    题目 线段树 简单题意: 区间(单点?)更新,区间求和  更新是区间内的数开根号并向下取整 这道题不用延迟操作 //注意: //1:查询时的区间端点可能前面的比后面的大: //2:优化:因为每次更新都 ...

随机推荐

  1. 在虚拟机里安装windows或Linux系统时,安装窗口过大按钮有时点不到解决办法(图文详解)

    不多说,直接上干货! 问题详情 解决办法 很简单快捷的解决办法,就是快捷键ALT+F7,可以拖动窗口的位置. 成功!

  2. 域名IP主动验证(一)

    功能:主动验证给定的域名.IP对是否真正的关联 思路: 1.一开始通过修改hosts文件,把待验证的域名.IP对添加到文件里,然后用wget尝试访问,再恢复hosts文件重新验证下一对 2.后来了解到 ...

  3. 微信环境支付宝服务窗环境app手机浏览器pc端混合判断

    //微信环境 if(userAgent.match(/micromessenger/) == 'micromessenger'){ }//支付宝服务窗环境else if(userAgent.match ...

  4. Android提供的对话框

    1.普通对话框: 给出提示信息,有yes.no两个按钮. AlertDialog dialog=new AlertDialog.Builder(this) //this代表当前Activity对象,表 ...

  5. Java&Xml教程(九)Java中通过XSD校验XML合法性

    Java XML校验API能够通过XSD(XML Schema Definition)校验XML文件内容的合法性.在下面的案例中使用javax.xml.validation.Validator 类通过 ...

  6. Android图片压缩上传(二)

    之前有用到libjpeg,还是有一定的局限性,最近用了一个新的方式,效果还是挺不错,随着作者的版本更新,Bug也随之变少,目前项目中运用已上线. 1.之前的方式Android图片压缩,不失真,上线项目 ...

  7. Load average in Linux的精确含义

    Man 上的解释: load average System load averages is the average number of processes that are either in a ...

  8. 通过HA方式操作HDFS

    之前操作hdfs的时候,都是固定namenode的地址,然后去操作.这个时候就必须判断namenode的状态为active还是standby,比较繁琐,如果集群使用了HA的形式,就很方便了 直接上代码 ...

  9. Javaweb之xml

        1 XML概述     1.1 XML是什么? eXtensible Markup Language可扩展标记语言          1.2 XML作用         主要是用于描述数据,而 ...

  10. vue跨域解决及打包

    打包之前需要修改如下配置文件: 配置文件一:build>>>utils.js (修改publicPath:"../../" , 这样写是处理打包后找不到静态文件( ...