Skip to content

Instantly share code, notes, and snippets.

@nmreadelf
Created January 19, 2019 15:23
Show Gist options
  • Save nmreadelf/e9dd2b61e0bb439582e0464f5dfee40c to your computer and use it in GitHub Desktop.
Save nmreadelf/e9dd2b61e0bb439582e0464f5dfee40c to your computer and use it in GitHub Desktop.
差分数组
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
// Complete the arrayManipulation function below.
func arrayManipulation(n int32, queries [][]int32) int64 {
var result []int64 = make([]int64, n)
var e int64
for i := range(queries) {
a, b, k := queries[i][0], queries[i][1], queries[i][2]
result[a-1] += int64(k)
if (b+1 <= n) {
result[b] -= int64(k)
}
}
var i int32
for i = 1; i<n; i++ {
result[i] += result[i - 1]
if result[i] > e {
e = result[i]
}
}
return e
}
func main() {
reader := bufio.NewReaderSize(os.Stdin, 1024 * 1024)
stdout, err := os.Create(os.Getenv("OUTPUT_PATH"))
checkError(err)
defer stdout.Close()
writer := bufio.NewWriterSize(stdout, 1024 * 1024)
nm := strings.Split(readLine(reader), " ")
nTemp, err := strconv.ParseInt(nm[0], 10, 64)
checkError(err)
n := int32(nTemp)
mTemp, err := strconv.ParseInt(nm[1], 10, 64)
checkError(err)
m := int32(mTemp)
var queries [][]int32
for i := 0; i < int(m); i++ {
queriesRowTemp := strings.Split(readLine(reader), " ")
var queriesRow []int32
for _, queriesRowItem := range queriesRowTemp {
queriesItemTemp, err := strconv.ParseInt(queriesRowItem, 10, 64)
checkError(err)
queriesItem := int32(queriesItemTemp)
queriesRow = append(queriesRow, queriesItem)
}
if len(queriesRow) != int(3) {
panic("Bad input")
}
queries = append(queries, queriesRow)
}
result := arrayManipulation(n, queries)
fmt.Fprintf(writer, "%d\n", result)
writer.Flush()
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
@nmreadelf
Copy link
Author

很有意思的一个东西,这是我刷题的答案,我放在这里

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment