-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1082.cpp
More file actions
84 lines (64 loc) · 1.42 KB
/
Copy path1082.cpp
File metadata and controls
84 lines (64 loc) · 1.42 KB
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
83
84
/**
* Problem : 1082-Array Queries
* Status : Accepted.
* Writer : Mehaidi Hasan Menon
* Date : 23.11.16
**/
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int mx = 100005;
int ara[mx];
int tree[mx * 3];
void init(int node, int b, int e)
{
if(b == e) {
tree[node] = ara[b];
return ;
}
int mid = (b + e) / 2;
int left = 2 * node;
int right = 2 * node + 1;
init(left, b, mid);
init(right, mid + 1, e);
tree[node] = min(tree[left], tree[right]);
}
int quary(int node, int b, int e, int i, int j)
{
if(b > j || e < i) {
return mx; // 100005
}
if(b >= i && e <= j) {
return tree[node];
}
int mid = (b + e) / 2;
int left = 2 * node;
int right = 2 * node + 1;
int q1 = quary(left, b, mid, i, j);
int q2 = quary(right, mid + 1, e, i, j);
return min(q1, q2);
}
int main()
{
freopen("1082 - Array Queries.txt", "r+", stdin);
int tc, n, q, i, j;
scanf("%d", &tc);
for(int a = 1; a <= tc; a++)
{
scanf("%d %d", &n, &q);
for(int b = 1; b <= n; b++) {
scanf("%d", &ara[b]);
}
init(1, 1, n);
int ans;
printf("Case %d:\n", a);
for(int c = 1; c <= q; c++)
{
scanf("%d %d", &i, &j);
ans = quary(1, 1, n, i, j);
printf("%d\n", ans);
}
}
return 0;
}