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 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 |
<span style="font-family:Courier New;">#include <iostream>
#include<cstdio>
#include<cstring>
#define maxn 100010
using namespace std;
int n,a[maxn];
int amin[maxn][30];
void init()
{
for(int i=1;i<=n;++i)
amin[i][0]=a[i];
for(int j=1;(1<<j)<=n;++j)
{
for(int i=1;i+(1<<j)-1<=n;++i)
{
amin[i][j]=min(amin[i][j-1],amin[i+(1<<(j-1))][j-1]);
}
}
}
int query(int l,int r)
{
int k=0;
while(1<<(k+1)<=(r-l+1))
k++;
return min(amin[l][k],amin[r-(1<<k)+1][k]);
}
int main()
{
int t,q,l,r,cnt=0;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&q);
for(int i=1;i<=n;++i)
scanf("%d",&a[i]);
init();
printf("Case %d:\n",++cnt);
while(q--)
{
scanf("%d%d",&l,&r);
printf("%d\n",query(l,r));
}
}
return 0;
}</span>