Caso # | Resultado | Tiempo | Memoria |
---|---|---|---|
#1 |
Correcto
|
0.005 s | 48 KBi |
#2 |
Correcto
|
0.005 s | 5 KBi |
#3 |
Correcto
|
0.005 s | 5 KBi |
#4 |
Correcto
|
0.005 s | 6 KBi |
#5 |
Correcto
|
0.006 s | 5 KBi |
#6 |
Correcto
|
0.006 s | 5 KBi |
#7 |
Correcto
|
0.007 s | 6 KBi |
#8 |
Correcto
|
0.092 s | 6 KBi |
#9 |
Correcto
|
0.195 s | 6 KBi |
#10 |
Correcto
|
0.199 s | 5 KBi |
#11 |
Correcto
|
0.188 s | 6 KBi |
#12 |
Correcto
|
0.197 s | 6 KBi |
#13 |
Correcto
|
0.197 s | 6 KBi |
#14 |
Correcto
|
0.188 s | 7 KBi |
package main import ( "fmt" ) func solve(a, s []int, p, q int) { // O(n) -- too slow! // sum := 0 // for i := p; i <= q; i++ { // sum += a[i] // } // O(1) // sum := s[q] // a[0] + a[1] + ... + a[q] // if (p - 1 >= 0) { // sum -= s[p - 1] // } 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) // for i := 0; i < n; i++ { // s[i] = a[i] // if i - 1 >= 0 { // s[i] += s[i - 1] // } // } // 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) } }