-
Notifications
You must be signed in to change notification settings - Fork 2
/
1082 - Array Queries .cpp
47 lines (46 loc) · 1.11 KB
/
1082 - Array Queries .cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<bits/stdc++.h>
using namespace std;
int ara[100005];
int tree[4*100005];
void build(int at, int begin,int end)
{
if(begin == end){
tree[at] = ara[begin];
return;
}
int left = at*2;
int right = at*2+1;
int mid = (begin+end)/2;
build(left,begin,mid);
build(right,mid+1,end);
tree[at] = min(tree[left] , tree[right]);
}
int query(int at,int begin,int end,int i, int j)
{
if(i>end || j<begin) return INT_MAX;
if(begin>=i && end<=j) return tree[at];
int left = at*2;
int right = at*2+1;
int mid = (begin+end)/2;
int p1 = query(left,begin,mid,i,j);
int p2 = query(right,mid+1,end,i,j);
return min(p1,p2);
}
int main()
{
//freopen("out.txt","w",stdout);
int tc,n,q,i,j;
scanf("%d",&tc);
for(int t=1; t<=tc; t++){
scanf("%d%d",&n,&q);
for(int i=0; i<n; i++)
scanf("%d",&ara[i+1]);
build(1,1,n);
printf("Case %d:\n",t);
while(q--){
scanf("%d%d",&i,&j);
printf("%d\n",query(1,1,n,i,j));
}
}
return 0;
}