-
Notifications
You must be signed in to change notification settings - Fork 0
/
answer.js
55 lines (48 loc) · 1.1 KB
/
answer.js
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
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let index = 0;
let M = 0;
let A;
let DP = [];
let sum = 0;
let answer = [];
const getNumArr = (line) => {
return line.split(" ").map((n) => Number(n));
};
const getSumArr = (line) => {
return line.split(" ").map((n, i) => {
sum += +n;
DP[i] = sum;
return +n;
});
};
const solve = (line) => {
const ranges = getNumArr(line);
const startIndex = Math.min(ranges[0], ranges[1]) - 1;
const lastIndex = Math.max(ranges[0], ranges[1]) - 1;
if (startIndex === lastIndex) {
answer.push(A[startIndex]);
return;
}
let preSum = 0;
if (startIndex > 0) preSum = DP[startIndex - 1];
answer.push(DP[lastIndex] - preSum);
};
rl.on("line", function (line) {
if (index === 0) {
const data = getNumArr(line);
M = data[1];
} else if (index === 1) {
A = getSumArr(line);
} else if (index >= 2) {
solve(line);
if (index >= M + 1) rl.close();
}
index++;
}).on("close", function () {
console.log(answer.join("\n"));
process.exit();
});