-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathStreamingStatistics.java
214 lines (158 loc) · 4.77 KB
/
StreamingStatistics.java
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// link: https://spotify.kattis.com/problems/streamstats
// name: Streaming Statistics
package kattis;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class StreamingStatistics {
private static final double MS_IN_S = 1000.0;
public static void main(String[] args) throws IOException {
Kattio io = new Kattio(System.in, System.out);
int n = io.getInt();
TimePoint[] bitrateChanges = new TimePoint[2 * n];
for (int i = 0; i < n; ++i) {
long end = io.getLong();
long duration = io.getLong();
long bitrate = io.getLong();
bitrateChanges[2 * i] = new TimePoint(end - duration, bitrate);
bitrateChanges[2 * i + 1] = new TimePoint(end, -bitrate);
}
Arrays.sort(bitrateChanges);
TimePoint[] bitrateValues = reduceValues(bitrateChanges);
TimePoint[] bitrateSums = reduceSums(bitrateValues);
int m = io.getInt();
for (int i = 0; i < m; i++) {
long a = io.getLong();
long b = io.getLong();
int aPosition = Arrays.binarySearch(bitrateSums, new TimePoint(a, 0));
int bPosition = Arrays.binarySearch(bitrateSums, new TimePoint(b, 0));
int aInsertionPoint = aPosition >= 0 ? aPosition : -aPosition - 1;
int bInsertionPoint = bPosition >= 0 ? bPosition : -bPosition - 1;
long result = bitrateSums[bInsertionPoint - 1].value - bitrateSums[aInsertionPoint].value;
if (bInsertionPoint != 0 && bInsertionPoint != bitrateSums.length) {
result += (b - bitrateValues[bInsertionPoint - 1].time) * bitrateValues[bInsertionPoint - 1].value;
}
if (aInsertionPoint != 0 && aInsertionPoint != bitrateSums.length) {
result += (bitrateValues[aInsertionPoint].time - a) * bitrateValues[aInsertionPoint - 1].value;
}
System.out.println(String.format("%.3f", result / MS_IN_S));
}
io.close();
}
private static TimePoint[] reduceSums(TimePoint[] bitrateValues) {
TimePoint[] sums = new TimePoint[bitrateValues.length];
sums[0] = new TimePoint(bitrateValues[0].time, 0);
int position = 1;
TimePoint last = null;
for (TimePoint current : bitrateValues) {
if (last != null) {
long time = current.time;
long currentSum = sums[position - 1].value + last.value * (current.time - last.time);
sums[position] = new TimePoint(time, currentSum);
position++;
}
last = current;
}
return sums;
}
private static TimePoint[] reduceValues(TimePoint[] bitrateChanges) {
int distinctValues = 1;
for (int i = 1; i < bitrateChanges.length; i++) {
if (bitrateChanges[i].time != bitrateChanges[i - 1].time) {
distinctValues++;
}
}
TimePoint[] bitrateValue = new TimePoint[distinctValues];
int position = 0;
long totalValue = 0;
TimePoint last = null;
for (TimePoint current : bitrateChanges) {
totalValue += current.value;
if (current.sameTime(last)) {
last.value = totalValue;
continue;
}
bitrateValue[position] = new TimePoint(current.time, totalValue);
last = bitrateValue[position];
position++;
}
return bitrateValue;
}
static class TimePoint implements Comparable<TimePoint> {
long time;
long value;
public TimePoint(long time, long value) {
this.time = time;
this.value = value;
}
@Override
public int compareTo(TimePoint o) {
if (this.time < o.time) {
return -1;
}
if (this.time > o.time) {
return 1;
}
return 0;
}
public boolean sameTime(TimePoint o) {
if (o == null) {
return false;
}
return this.time == o.time;
}
}
static class Kattio extends PrintWriter {
public Kattio(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}
public Kattio(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}
public boolean hasMoreTokens() {
return peekToken() != null;
}
public int getInt() {
return Integer.parseInt(nextToken());
}
public double getDouble() {
return Double.parseDouble(nextToken());
}
public long getLong() {
return Long.parseLong(nextToken());
}
public String getWord() {
return nextToken();
}
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null)
return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) {}
return token;
}
private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
}
}