-
Notifications
You must be signed in to change notification settings - Fork 0
/
1082 - Array Queries.cpp
79 lines (66 loc) · 1.7 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
#include <bits/stdc++.h>
using namespace std;
const int Max = 100001;
int arr[Max], tree[Max<<2];
void build (int at, int st, int ed)
{
if(st == ed) {
tree[at] = arr[st];
return;
}
int lft = at * 2;
int rgt = lft + 1;
int mid = (st + ed)/2;
build(lft, st, mid);
build(rgt, mid + 1, ed);
tree[at] = min(tree[lft], tree[rgt]);
}
void update (int at, int st, int ed, int pos, int u)
{
if(st == ed) {
tree[at] = u;// use tree[at] += u if value is increased by u;
return;
}
int lft = at*2;
int rgt = lft + 1;
int mid = (st + ed)/2;
if(pos <= mid)
update(lft, st, mid, pos, u);
else
update(rgt, mid + 1, ed, pos, u);
tree[at] = tree[lft] + tree[rgt];
}
int query(int at, int st, int ed, int l, int r)
{
if(r < st || l > ed)
return 44777778;
if(l<=st && r>=ed)//( l <= L && R <= r )
return tree[at];
int lft = at*2;
int rgt = lft + 1;
int mid = (st + ed)/2;
int sumL = query(lft, st, mid, l, r);
int sumR = query(rgt, mid + 1, ed, l, r);
return min(sumL, sumR);
}
int main()
{
int n,q,t,l,r;
//cin >> t;
scanf("%d",&t);
for(int j = 1; j<=t; j++) {
//cin >> n >> q;
scanf("%d %d",&n,&q);
for(int i = 1; i<=n; i++)
scanf("%d",&arr[i]); //cin>> arr[i];
build(1, 1, n);
printf("Case %d:\n",j);
for(int i = 1; i<=q; i++) {
//cin >> l >> r;
scanf("%d %d",&l,&r);
int x = query(1, 1, n, l, r);
printf("%d\n",x);
}
}
return 0;
}