- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSegmentTreeFast.java
More file actions
Latest commit
161 lines (145 loc) · 4.38 KB
/
Copy pathSegmentTreeFast.java
File metadata and controls
161 lines (145 loc) · 4.38 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
packageData_Structures;
importjava.util.*;
publicclassSegmentTreeFast {
// Modify the following 5 methods to implement your custom operations on the tree.
// This example implements Add/Sum operations. Operations like Add/Max, Set/Max can also be implemented.
intmodifyOperation(intx, inty) {
returnx + y;
}
// query (or combine) operation
intqueryOperation(intleftValue, intrightValue) {
returnleftValue + rightValue;
}
intdeltaEffectOnSegment(intdelta, intsegmentLength) {
if (delta == getNeutralDelta()) returngetNeutralDelta();
// Here you must write a fast equivalent of following slow code:
// int result = delta;
// for (int i = 1; i < segmentLength; i++) result = queryOperation(result, delta);
// return result;
returndelta * segmentLength;
}
intgetNeutralDelta() {
return0;
}
intgetInitValue() {
return0;
}
// generic code
int[] value;
int[] delta; // delta[i] affects value[i], delta[2*i+1] and delta[2*i+2]
intjoinValueWithDelta(intvalue, intdelta) {
if (delta == getNeutralDelta()) returnvalue;
returnmodifyOperation(value, delta);
}
intjoinDeltas(intdelta1, intdelta2) {
if (delta1 == getNeutralDelta()) returndelta2;
if (delta2 == getNeutralDelta()) returndelta1;
returnmodifyOperation(delta1, delta2);
}
voidpushDelta(inti) {
intd = 0;
for (; (i >> d) > 0; d++) {
}
for (d -= 2; d >= 0; d--) {
intx = i >> d;
value[x >> 1] = joinNodeValueWithDelta(x >> 1, 1 << (d + 1));
delta[x] = joinDeltas(delta[x], delta[x >> 1]);
delta[x ^ 1] = joinDeltas(delta[x ^ 1], delta[x >> 1]);
delta[x >> 1] = getNeutralDelta();
}
}
publicSegmentTreeFast(intn) {
value = newint[2 * n];
for (inti = 0; i < n; i++) {
value[i + n] = getInitValue();
}
for (inti = 2 * n - 1; i > 1; i -= 2) {
value[i >> 1] = queryOperation(value[i], value[i ^ 1]);
}
delta = newint[2 * n];
Arrays.fill(delta, getNeutralDelta());
}
intjoinNodeValueWithDelta(inti, intlen) {
returnjoinValueWithDelta(value[i], deltaEffectOnSegment(delta[i], len));
}
publicintquery(intfrom, intto) {
from += value.length >> 1;
to += value.length >> 1;
pushDelta(from);
pushDelta(to);
intres = 0;
booleanfound = false;
for (intlen = 1; from <= to; from = (from + 1) >> 1, to = (to - 1) >> 1, len <<= 1) {
if ((from & 1) != 0) {
res = found ? queryOperation(res, joinNodeValueWithDelta(from, len)) : joinNodeValueWithDelta(from, len);
found = true;
}
if ((to & 1) == 0) {
res = found ? queryOperation(res, joinNodeValueWithDelta(to, len)) : joinNodeValueWithDelta(to, len);
found = true;
}
}
if (!found) thrownewRuntimeException();
returnres;
}
publicvoidmodify(intfrom, intto, intdelta) {
from += value.length >> 1;
to += value.length >> 1;
pushDelta(from);
pushDelta(to);
inta = from;
intb = to;
for (; from <= to; from = (from + 1) >> 1, to = (to - 1) >> 1) {
if ((from & 1) != 0) {
this.delta[from] = joinDeltas(this.delta[from], delta);
}
if ((to & 1) == 0) {
this.delta[to] = joinDeltas(this.delta[to], delta);
}
}
for (inti = a, len = 1; i > 1; i >>= 1, len <<= 1) {
value[i >> 1] = queryOperation(joinNodeValueWithDelta(i, len), joinNodeValueWithDelta(i ^ 1, len));
}
for (inti = b, len = 1; i > 1; i >>= 1, len <<= 1) {
value[i >> 1] = queryOperation(joinNodeValueWithDelta(i, len), joinNodeValueWithDelta(i ^ 1, len));
}
}
// Random test
publicstaticvoidmain(String[] args) {
Randomrnd = newRandom();
for (intstep = 0; step < 1000; step++) {
intn = rnd.nextInt(50) + 1;
int[] x = newint[n];
SegmentTreeFastt = newSegmentTreeFast(n);
Arrays.fill(x, t.getInitValue());
for (inti = 0; i < 1000; i++) {
intb = rnd.nextInt(n);
inta = rnd.nextInt(b + 1);
intcmd = rnd.nextInt(3);
if (cmd == 0) {
intdelta = rnd.nextInt(100) - 50;
t.modify(a, b, delta);
for (intj = a; j <= b; j++) {
x[j] = t.joinValueWithDelta(x[j], delta);
}
} elseif (cmd == 1) {
intres1 = t.query(a, b);
intres2 = x[a];
for (intj = a + 1; j <= b; j++) {
res2 = t.queryOperation(res2, x[j]);
}
if (res1 != res2) {
thrownewRuntimeException();
}
} else {
for (intj = 0; j < n; j++) {
if (t.query(j, j) != x[j]) {
thrownewRuntimeException();
}
}
}
}
}
System.out.println("Test passed");
}
}