LightOj 1082 Array Queries Solution
LightOj Problem Link: https://lightoj.com/problem/array-queries
Solution in C++:
///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
#include <bits/stdc++.h>
#define sf(a) scanf("%d",&a)
using namespace std;
const int MX=1e5+5;
int a[MX],tree[MX*4];
int build(int node,int b,int e)
{
if(b==e)
return tree[node]=a[b];
int mid=(b+e)/2;
int x=build(node*2,b,mid);
int y=build(node*2+1,mid+1,e);
return tree[node]=min(x,y);
}
int query(int node,int b, int e,int l,int r)
{
if(r<b||l>e)
return INT_MAX;
else if(l<=b&&r>=e)
return tree[node];
int mid=(b+e)/2;
int x=query(node*2,b,mid,l,r);
int y=query(node*2+1,mid+1,e,l,r);
return min(x,y);
}
int main()
{
int t,i,j,n,q,x1,y1;
sf(t);
for(i=1; i<=t; i++)
{
sf(n);
sf(q);
for(j=1; j<=n; j++)
sf(a[j]);
build(1,1,n);
printf("Case %d:\n",i);
while(q--)
{
sf(x1);
sf(y1);
printf("%d\n",query(1,1,n,x1,y1));
}
}
}
No comments