結果

問題 No.866 レベルKの正方形
ユーザー ccppjsrbccppjsrb
提出日時 2020-06-10 09:07:54
言語 Go
(1.22.1)
結果
AC  
実行時間 1,434 ms / 6,000 ms
コード長 3,132 bytes
コンパイル時間 14,211 ms
コンパイル使用メモリ 209,432 KB
実行使用メモリ 328,964 KB
最終ジャッジ日時 2023-09-03 22:07:24
合計ジャッジ時間 34,097 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1,418 ms
326,980 KB
testcase_09 AC 1,323 ms
328,960 KB
testcase_10 AC 1,418 ms
328,964 KB
testcase_11 AC 1,404 ms
328,964 KB
testcase_12 AC 1,228 ms
328,964 KB
testcase_13 AC 1,380 ms
328,960 KB
testcase_14 AC 1,132 ms
328,960 KB
testcase_15 AC 1,358 ms
328,960 KB
testcase_16 AC 1,334 ms
328,964 KB
testcase_17 AC 1,388 ms
328,960 KB
testcase_18 AC 1,434 ms
328,964 KB
testcase_19 AC 1,073 ms
328,964 KB
testcase_20 AC 965 ms
328,960 KB
testcase_21 AC 1,105 ms
328,960 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"math/bits"
	"os"
	"strconv"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	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 getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	h := getNextInt(scanner)
	w := getNextInt(scanner)
	k := getNextInt(scanner)
	sq := makeGrid(h, w)
	for y := 0; y < h; y++ {
		s := getNextString(scanner)
		for x := 0; x < w; x++ {
			sq[y][x].b[0] = sq[y][x].b[0] | uint32(1<<uint(s[x]-'a'))
		}
	}
	for i := 1; i < 16; i++ {
		z := 1 << uint(i-1)
		zz := 1 << uint(i)
		for y := 0; y < h && y+zz <= h; y++ {
			for x := 0; x < w && x+zz <= w; x++ {
				sq[y][x].b[i] = sq[y][x].b[i] | sq[y][x].b[i-1] | sq[y+z][x].b[i-1] | sq[y][x+z].b[i-1] | sq[y+z][x+z].b[i-1]
			}
		}
	}
	var ans int64
	lowers := make([][][2]int, h)
	for i := 0; i < h; i++ {
		lowers[i] = make([][2]int, w)
	}
	for i := 0; i < w; i++ {
		for lw := 0; lw < 2; lw++ {
			lx := i
			for rx := i + 1; rx < w+1; rx++ {
				ry := rx - i
				for rx > lx {
					ly := lx - i
					c := types(ly, lx, rx-lx, sq)
					if c < k+lw {
						break
					}
					lx++
				}
				lowers[ry-1][rx-1][lw] = lx
				ans += int64(lowers[ry-1][rx-1][0] - lowers[ry-1][rx-1][lw])
			}
		}
	}
	for i := 1; i < h; i++ {
		for lw := 0; lw < 2; lw++ {
			ly := i
			for ry := i + 1; ry < h+1; ry++ {
				rx := ry - i
				for ry > ly {
					lx := ly - i
					c := types(ly, lx, ry-ly, sq)
					if c < k+lw {
						break
					}
					ly++
				}
				lowers[ry-1][rx-1][lw] = ly
				ans += int64(lowers[ry-1][rx-1][0] - lowers[ry-1][rx-1][lw])
			}
		}
	}
	fmt.Fprintln(writer, ans)
}

func types(y, x, m int, sq [][]square) int {
	z := 32 - bits.LeadingZeros32(uint32(m))
	zz := 1 << uint(z-1)
	b := sq[y][x].b[z-1] | sq[y+m-zz][x].b[z-1] | sq[y][x+m-zz].b[z-1] | sq[y+m-zz][x+m-zz].b[z-1]
	c := bits.OnesCount32(b)
	return c
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

type square struct {
	b [16]uint32
}

func makeGrid(h, w int) [][]square {
	index := make([][]square, h, h)
	data := make([]square, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}
0