結果

問題 No.876 Range Compress Query
ユーザー ccppjsrb
提出日時 2019-11-09 15:29:20
言語 Go
(1.14)
結果
WA   .
実行時間 -
コード長 3,318 Byte
コンパイル時間 624 ms
使用メモリ 5,672 KB
最終ジャッジ日時 2020-05-14 13:11:38

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 2 ms
1,168 KB
testcase_01 WA -
testcase_02 AC 2 ms
1,192 KB
testcase_03 WA -
testcase_04 AC 2 ms
1,188 KB
testcase_05 AC 1 ms
1,216 KB
testcase_06 AC 3 ms
1,196 KB
testcase_07 WA -
testcase_08 AC 2 ms
1,232 KB
testcase_09 WA -
testcase_10 AC 2 ms
1,224 KB
testcase_11 AC 282 ms
5,520 KB
testcase_12 AC 228 ms
5,292 KB
testcase_13 AC 236 ms
5,528 KB
testcase_14 AC 275 ms
5,520 KB
testcase_15 AC 191 ms
5,572 KB
testcase_16 AC 295 ms
5,540 KB
testcase_17 AC 296 ms
5,660 KB
testcase_18 AC 320 ms
5,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout

	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
		if len(os.Args) > 2 {
			wfp, _ = os.Create(os.Args[2])
		}
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)

	n := getNextInt(scanner)
	q := getNextInt(scanner)

	seg := Segment{}
	seg.init(n)
	seg2 := Segment{}
	seg2.init(n)

	for i := 0; i < n; i++ {
		seg.bucket[0][i] = getNextInt64(scanner)
		if i == 0 {
			continue
		}
		if seg.bucket[0][i] != seg.bucket[0][i-1] {
			seg2.add(i-1, 1)
		}
	}

	for i := 0; i < q; i++ {
		t := getNextInt(scanner)
		l := getNextInt(scanner) - 1
		r := getNextInt(scanner) - 1
		if t == 1 {
			seg.rangeAdd(l, r+1, getNextInt64(scanner))
			if l > 0 {
				if seg.get(l-1) != seg.get(l) {
					seg2.add(l-1, 1-seg2.bucket[0][l-1])
				} else {
					seg2.add(l-1, -seg2.bucket[0][l-1])
				}
			}
			if r < n-1 {
				if seg.get(r+1) != seg.get(r) {
					seg2.add(r, 1-seg2.bucket[0][r-1])
				} else {
					seg2.add(r, -seg2.bucket[0][r-1])
				}
			}
			continue
		}
		fmt.Fprintln(writer, seg2.rangeSum(l, r)+1)
	}
	writer.Flush()
}

// Segment ...
type Segment struct {
	n, h, i, chunk int
	unit           []int
	bucket         [][]int64
}

func (seg *Segment) init(n int) {
	seg.n = n
	seg.unit = make([]int, 1)
	seg.unit[0] = 1
	seg.bucket = make([][]int64, 1)
	seg.bucket[0] = make([]int64, n)

	chunk := 8
	for i := 0; n > 1; i++ {
		n = (n-1)/chunk + 1
		seg.bucket = append(seg.bucket, make([]int64, n))
		seg.unit = append(seg.unit, seg.unit[i]*chunk)
	}
	seg.h = len(seg.unit)
	seg.chunk = chunk
}

func (seg *Segment) get(index int) int64 {
	var value int64
	for seg.i = 0; seg.i < seg.h; seg.i++ {
		value += seg.bucket[seg.i][index/seg.unit[seg.i]]
		index /= seg.unit[seg.i]
	}
	return value
}

func (seg *Segment) rangeAdd(l, r int, value int64) {
	for index := l; index < r; index += seg.unit[seg.i] {
		for seg.i = 0; seg.i < seg.h-1 && index%seg.unit[seg.i+1] == 0; seg.i++ {
			if index+seg.unit[seg.i+1] >= r {
				break
			}
		}
		seg.bucket[seg.i][index/seg.unit[seg.i]] += value
	}
}

func (seg *Segment) add(index int, value int64) {
	for i := 0; i < seg.h; i++ {
		seg.bucket[i][index/seg.unit[i]] += value
	}
}

func (seg *Segment) rangeSum(l, r int) int64 {
	var value int64
	for index := l; index < r; index += seg.unit[seg.i] {
		for seg.i = 0; seg.i < seg.h-1 && index%seg.unit[seg.i+1] == 0; seg.i++ {
			if index+seg.unit[seg.i+1] >= r {
				break
			}
		}
		value += seg.bucket[seg.i][index/seg.unit[seg.i]]
	}
	return value
}
0