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 to J.
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.
最简单的线段树实现区间查询.....
努力学!
#include<stdio.h>
#define maxn 0x3f3f3f3f
#include<algorithm>
using namespace std;
int mintree[400005];
void build(int rt,int l,int r)
{
if(l==r)
{
scanf("%d",&mintree[rt]);
return;
}
int mid=(l+r)>>1,tp=rt<<1;
build(tp,l,mid);build(tp|1,mid+1,r);
mintree[rt]=min(mintree[tp],mintree[tp|1]);
}
int find(int rt,int l,int r,int a,int b)
{
if(l>=a&&r<=b)
{
return mintree[rt];
}
int mid=(l+r)>>1,tp=rt<<1,ret=maxn;
if(mid>=a)
{
ret=min(find(tp,l,mid,a,b),ret);
}
if(mid<b)
{
ret=min(find(tp|1,mid+1,r,a,b),ret);
}
return ret;
}
int main()
{
int t,n,m;
//freopen("shuju.txt","r",stdin);
scanf("%d",&t);
for(int k=1;k<=t;++k)
{
printf("Case %d:\n",k);
scanf("%d%d",&n,&m);
build(1,1,n);
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",find(1,1,n,a,b));
}
}
return 0;
}