-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcu3210.cpp
59 lines (52 loc) · 1.09 KB
/
cu3210.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
#include <bits/stdc++.h>
using namespace std;
int n, m, x, me, a, b, cnt = 1;
int tree[400040];
void printTree(){
for(int i = 1; i < 2*cnt; i++){
printf("%d, ", tree[i]);
}
puts("");
}
void renewal(int t, int k){
tree[t] = k;
t = t/2;
while(t > 0){
//printf("%d, ",t);
tree[t] = max(tree[t*2], tree[t*2+1]);
t = t/2;
}
}
int f(int s, int e, int l, int r, int node){
//printf("(%d %d), ", l, r);
if(s <= l && r <= e){
return tree[node];
}
else if(r < s || l > e){
return -1;
}
else{
return max(f(s,e,l,(l+r)/2,node*2), f(s,e,(l+r)/2+1, r, node*2+1));
}
}
int main (){
scanf("%d", &n);
while(cnt < n){
cnt *= 2;
}
for(int i = cnt; i < 2*cnt; i++){
tree[i] = -1;
}
for(int i = 0; i < n; i++){
scanf("%d", tree+cnt+i);
}
for(int i = cnt-1; i > 0; i--){
tree[i] = max(tree[i*2], tree[i*2+1]);
}
printTree();
scanf("%d", &m);
while(m--){
scanf("%d %d", &a, &b);
printf("%d\n",f(a,b,1,cnt,1));
}
}