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

Envío 4128

Problema 0x25 - Suma de un subarreglo grande

  • Autor: williamzborja
  • Fecha: 2021-05-17 20:04:17 UTC (Hace casi 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.006 s 9 KBi
#2
Correcto
0.006 s 13 KBi
#3
Correcto
0.003 s 4 KBi
#4
Correcto
0.004 s 7 KBi
#5
Correcto
0.003 s 3 KBi
#6
Correcto
0.008 s 14 KBi
#7
Correcto
0.004 s 4 KBi
#8
Correcto
0.218 s 8 KBi
#9
Tiempo límite excedido
1.096 s 11 KBi
#10
Tiempo límite excedido
1.052 s 11 KBi
#11
Tiempo límite excedido
1.085 s 11 KBi
#12
Tiempo límite excedido
1.093 s 10 KBi
#13
Tiempo límite excedido
1.082 s 10 KBi
#14
Tiempo límite excedido
1.005 s 10 KBi
Puntos totales: 58 / 100

Código

package main

import (
	"fmt"
)

func solve(a, s []int, p, q int) {
	// O(1)
	sum := s[q] // a[0] + a[1] + ... + a[q]
	if p-1 >= 0 {
		sum -= s[p-1]
	}

	// Alternatively, this works too, and some people might like it more.
	// sum := s[q] - s[p] + a[p];
	fmt.Println(sum)
}

func main() {
	var n int
	_, err := fmt.Scanf("%d", &n)
	if err != nil {
		panic(err)
	}

	a := make([]int, n)
	for i := 0; i < n; i++ {
		_, err := fmt.Scanf("%d", &a[i])
		if err != nil {
			panic(err)
		}
	}

	s := make([]int, n)
	// s[i] = sum of a[0] + ... + a[i]

	// O(n)
	sum := 0
	for i := 0; i < n; i++ {
		sum += a[i]
		s[i] = sum
	}

	var c int
	_, err = fmt.Scanf("%d", &c)
	if err != nil {
		panic(err)
	}

	for k := 0; k < c; k++ {
		var p, q int
		_, err := fmt.Scanf("%d %d", &p, &q)
		if err != nil {
			panic(err)
		}

		solve(a, s, p, q)
	}
}