-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1082 - Array Queries.cpp
67 lines (62 loc) · 1.24 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define mx 100005
int arr[mx];
int tree[mx * 3];
void init (int node , int b , int e)
{
if(b == e)
{
tree[node] = arr[b];
return;
}
int right , left , mid;
left = node * 2;
right = node * 2 + 1;
mid = (b + e) / 2;
init(left , b , mid);
init(right , mid +1 , e);
tree[node] = min(tree[left] , tree[right]);
}
int query (int node , int b , int e, int i , int j)
{
if(i > e or j < b)
return mx;
if(b >= i and e <= j)
{
return tree[node];
}
int left , right , mid;
left = node * 2;
right = node * 2 + 1;
mid = (b + e) / 2;
int x = query(left , b , mid , i , j);
int y = query(right , mid + 1, e , i , j);
return min(x , y);
}
int main()
{
int tes , o = 0;
scanf("%d", &tes);
while(tes--)
{
o++;
int n , q;
scanf("%d", &n);
scanf("%d", &q);
for(int i = 1; i <= n; i++)
{
scanf("%d", &arr[i]);
}
printf("Case %d:\n",o);
init(1, 1, n);
while(q--)
{
int x, y;
scanf("%d %d", &x, &y);
printf("%d\n",query(1 , 1 , n , x, y));
}
}
}