-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRefvalueofRangeREF.java
More file actions
61 lines (56 loc) · 2 KB
/
Copy pathRefvalueofRangeREF.java
File metadata and controls
61 lines (56 loc) · 2 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
import java.io.*;
import java.util.*;
public class source {
static int N, Q;
static int TS, ST;
static long[] S;
static int[] mntree, mxtree;
static int get_min(int l, int r) {
int ret = Integer.MAX_VALUE;
l += ST; r += ST;
while (l <= r){
if ((l&1) == 1) ret = Math.min(ret, mntree[l++]);
if ((r&1) == 0) ret = Math.min(ret, mntree[r--]);
l >>= 1; r >>= 1;
}
return ret;
}
static int get_max(int l, int r) {
int ret = Integer.MIN_VALUE;
l += ST; r += ST;
while (l <= r){
if ((l&1) == 1) ret = Math.max(ret, mxtree[l++]);
if ((r&1) == 0) ret = Math.max(ret, mxtree[r--]);
l >>= 1; r >>= 1;
}
return ret;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
N = Integer.parseInt(br.readLine());
for (TS=1;TS<N;TS<<=1); TS <<= 1; ST = TS/2-1;
mntree = new int[TS]; mxtree = new int[TS];
S = new long[N+1];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i=1;i<=N;i++){
int v = Integer.parseInt(st.nextToken());
S[i] = S[i-1] + v;
mntree[ST+i] = mxtree[ST+i] = v;
}
for (int i=ST;i>0;i--){
mntree[i] = Math.min(mntree[i+i], mntree[i+i+1]);
mxtree[i] = Math.max(mxtree[i+i], mxtree[i+i+1]);
}
Q = Integer.parseInt(br.readLine());
while (Q-- > 0){
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int mn = get_min(a, b), mx = get_max(a, b); long sum = S[b] - S[a-1];
bw.write(mn + " " + mx + " " + sum + "\n");
}
bw.flush();
bw.close();
}
}