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