Light OJ-1082 - Array Queries,线段树区间查询最大值

                                                                                                              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;
}
代码写出来相比于其他线段树来讲已经是很简洁的代码了~~


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
SDUT-OJ(Software Development University of Tsinghua Online Judge)是一个在线编程平台,提供给清华大学软件学院的学生和爱好者练习和解决算法问题的环境,其中包括各种计算机科学题目,包括数据结构、算法、图形等。对于"最小生成"(Minimum Spanning Tree, MST)问题,它是图论中的经典问题,目标是从一个加权无向图中找到一棵包含所有顶点的,使得的所有边的权重之和最小。 在C语言中,最常见的是使用Prim算法或Kruskal算法来求解最小生成。Prim算法从一个顶点开始,逐步添加与当前生成相连且权重最小的边,直到所有顶点都被包含;而Kruskal算法则是从小到大对所有边排序,每次选取没有形成环的新边加入到中。 如果你想了解如何用C语言实现这些算法,这里简单概括一下: - 通常使用优先队列(堆)来存储边和它们的权重,以便快速查找最小- 从任意一个顶点开始,遍历与其相邻的边,若新边不形成环,就更新,并将新边加入优先队列。 - Kruskal算法: - 先将所有的边按照权重升序排序。 - 创建一个空的最小生成,然后依次取出排序后的边,如果这条边连接的两个顶点不在同一个连通分量,则将其添加到中。 如果你需要更详细的代码示例,或者有具体的问题想了解(比如如何处理环、如何实现优先队列等),请告诉我,我会为你提供相应的帮助。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值