3
3
// #Hard #Array #Dynamic_Programming #Divide_and_Conquer #Segment_Tree
4
4
// #2024_06_02_Time_1927_ms_(87.75%)_Space_82.1_MB_(5.31%)
5
5
6
- import java .util .Arrays ;
6
+ import java .util .stream . Stream ;
7
7
8
8
public class Solution {
9
- private static int MOD = 1_000_000_007 ;
9
+ private static final int MOD = 1_000_000_007 ;
10
10
11
11
public int maximumSumSubsequence (int [] nums , int [][] queries ) {
12
- var n = nums .length ;
13
12
int ans = 0 ;
14
13
SegTree segTree = new SegTree (nums );
15
14
for (int [] q : queries ) {
@@ -23,43 +22,44 @@ public int maximumSumSubsequence(int[] nums, int[][] queries) {
23
22
}
24
23
25
24
class SegTree {
26
- private class Record {
25
+ private static class Record {
27
26
int takeFirstTakeLast ;
28
27
int takeFirstSkipLast ;
29
28
int skipFirstSkipLast ;
30
29
int skipFirstTakeLast ;
31
30
32
31
public Integer getMax () {
33
- return Arrays . asList (
32
+ return Stream . of (
34
33
this .takeFirstSkipLast ,
35
34
this .takeFirstTakeLast ,
36
35
this .skipFirstSkipLast ,
37
36
this .skipFirstTakeLast )
38
- .stream ()
39
37
.max (Integer ::compare )
40
38
.orElse (null );
41
39
}
42
40
43
41
public Integer skipLast () {
44
- return Arrays . asList (this .takeFirstSkipLast , this .skipFirstSkipLast ). stream ( )
42
+ return Stream . of (this .takeFirstSkipLast , this .skipFirstSkipLast )
45
43
.max (Integer ::compare )
46
44
.orElse (null );
47
45
}
48
46
49
47
public Integer takeLast () {
50
- return Arrays . asList (this .skipFirstTakeLast , this .takeFirstTakeLast ). stream ( )
48
+ return Stream . of (this .skipFirstTakeLast , this .takeFirstTakeLast )
51
49
.max (Integer ::compare )
52
50
.orElse (null );
53
51
}
54
52
}
55
53
56
54
private static Record [] seg ;
57
- private static int [] nums ;
55
+ private final int [] nums ;
58
56
59
57
public SegTree (int [] nums ) {
60
58
this .nums = nums ;
61
59
seg = new Record [4 * nums .length ];
62
- for (int i = 0 ; i < 4 * nums .length ; ++i ) seg [i ] = new Record ();
60
+ for (int i = 0 ; i < 4 * nums .length ; ++i ) {
61
+ seg [i ] = new Record ();
62
+ }
63
63
build (0 , nums .length - 1 , 0 );
64
64
}
65
65
0 commit comments