█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 3543

Problema 0x25 - Suma de un subarreglo grande

  • Autor: toroduque
  • Fecha: 2021-03-21 09:41:24 UTC (Hace alrededor de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.063 s 6 KBi
#2
Correcto
0.06 s 6 KBi
#3
Correcto
0.161 s 33 KBi
#4
Correcto
0.063 s 7 KBi
#5
Correcto
0.066 s 7 KBi
#6
Correcto
0.069 s 7 KBi
#7
Correcto
0.063 s 7 KBi
#8
Correcto
0.432 s 17 KBi
#9
Tiempo límite excedido
1.046 s 30 KBi
#10
Tiempo límite excedido
1.039 s 31 KBi
#11
Tiempo límite excedido
1.051 s 31 KBi
#12
Tiempo límite excedido
1.045 s 31 KBi
#13
Tiempo límite excedido
1.017 s 31 KBi
#14
Correcto
0.717 s 31 KBi
Puntos totales: 65 / 100

Código

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

let lineCounter = 0;
let subTotalsArray;

function sumSubTotals(initialArray) {
  let currentSum = 0
  return initialArray.map((item) => {
    currentSum = item + currentSum
    return currentSum
  })
}

const sumSubArray = (fromIndex, toIndex) => {
  if (fromIndex === 0) return subTotalsArray[toIndex]
  return subTotalsArray[toIndex] - subTotalsArray[fromIndex - 1];
}

rl.on("line", (line) => {
 ++lineCounter;
 if (lineCounter === 2) {
  // Run the initial algorithm
  const initialArray = line.split(" ").map(Number);
  subTotalsArray = sumSubTotals(initialArray)
 }

 if (lineCounter > 3) {
   // these are the inputs Q & P
   const [Q, P] = line.split(" ").map(Number);
   console.log(sumSubArray(Q, P))
 }
})