-
Notifications
You must be signed in to change notification settings - Fork 34
/
1082-Array-Queries.cpp
87 lines (56 loc) · 1.06 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <stdio.h>
#include <limits.h>
using namespace std;
int a[100005];
int tree[400005];
int build_segment_tree(int l, int r, int node)
{
int x;
int y;
if(l == r) {
tree[node] = a[l];
return a[l];
}
x = build_segment_tree(l, (l + r) / 2, node * 2);
y = build_segment_tree((l + r) / 2 + 1, r, node * 2 + 1);
tree[node] = min(x, y);
return tree[node];
}
int query(int l, int r, int e, int f, int node)
{
int x;
int y;
if(e <= l and f >= r) {
return tree[node];
}
if(e > r or f < l) {
return INT_MAX;
}
x = query(l, (l+r)/2, e, f, node * 2);
y = query((l+r)/ 2 + 1, r, e, f, node * 2 + 1);
return min(x, y);
}
int main()
{
int t;
int n;
int qs;
int x;
int y;
scanf("%d", &t);
for (int cs = 1; cs <= t; cs++) {
scanf("%d", &n);
scanf("%d", &qs);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
build_segment_tree(1, n, 1);
printf("Case %d:\n", cs);
for(int i = 0; i < qs; i++) {
scanf("%d", &x);
scanf("%d", &y);
printf("%d\n", query(1, n, x, y, 1));
}
}
}