結果
問題 | No.649 ここでちょっとQK! |
ユーザー | ccppjsrb |
提出日時 | 2020-09-04 21:20:06 |
言語 | Go (1.22.1) |
結果 |
WA
|
実行時間 | - |
コード長 | 7,435 bytes |
コンパイル時間 | 10,497 ms |
コンパイル使用メモリ | 232,508 KB |
実行使用メモリ | 10,752 KB |
最終ジャッジ日時 | 2024-05-04 21:14:59 |
合計ジャッジ時間 | 13,364 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 20 ms
5,376 KB |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | AC | 58 ms
5,504 KB |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | AC | 1 ms
5,376 KB |
testcase_10 | AC | 1 ms
5,376 KB |
testcase_11 | AC | 1 ms
5,376 KB |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | AC | 2 ms
5,376 KB |
testcase_28 | AC | 2 ms
5,376 KB |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | AC | 1 ms
5,376 KB |
testcase_33 | AC | 1 ms
5,376 KB |
testcase_34 | AC | 1 ms
5,376 KB |
testcase_35 | AC | 1 ms
5,376 KB |
ソースコード
package main import ( "bufio" "fmt" "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) { q := getNextInt(scanner) k := getNextInt(scanner) t := newTree(func(a, b interface{}) int { aa := a.(int64) bb := b.(int64) if aa == bb { return 0 } if aa < bb { return -1 } return 1 }) cnt := 0 for i := 0; i < q; i++ { ord := getNextInt(scanner) if ord == 1 { increment(t, getNextInt64(scanner)) cnt++ continue } if cnt < k { fmt.Fprintln(writer, -1) continue } v, _ := t.At(k) fmt.Fprintln(writer, v.Key) decrement(t, v.Key) cnt-- } } func increment(t *tree, key interface{}) { value, found := t.Get(key) if found { t.Put(key, value.(int)+1) return } t.Put(key, 1) } func decrement(t *tree, key interface{}) { value, found := t.Get(key) if !found { return } y := value.(int) - 1 if y == 0 { t.Remove(key) return } t.Put(key, y) } type tree struct { Root *node Comparator func(a, b interface{}) int } type node struct { Left, Right, Parent *node Key interface{} Value interface{} size int } func newTreeWithIntComparator() *tree { return newTree(func(a, b interface{}) int { aa := a.(int) bb := b.(int) if aa == bb { return 0 } if aa < bb { return -1 } return 1 }) } func newTree(comparator func(interface{}, interface{}) int) *tree { return &tree{Root: nil, Comparator: comparator} } func newNode(parent *node, key, value interface{}) *node { return &node{Parent: parent, Key: key, Value: value, size: 1} } func (n *node) IsRoot() bool { return n.Parent == nil } func (n *node) IsLeft() bool { return !n.IsRoot() && n.Parent.Left == n } func (n *node) IsRight() bool { return !n.IsRoot() && n.Parent.Right == n } func (n *node) Update() { n.size = 1 + n.Left.Size() + n.Right.Size() } func (n *node) Size() int { if n == nil { return 0 } return n.size } func (n *node) Merge(m *node) *node { if n == nil { return m } if m == nil { return n } if n.Size() > m.Size() { n.Left = n.Left.Merge(n.Right) n.Right = m if n.Left != nil { n.Left.Parent = n } if n.Right != nil { n.Right.Parent = n } n.Update() return n } m.Right = m.Left.Merge(m.Right) m.Left = n if m.Left != nil { m.Left.Parent = m } if m.Right != nil { m.Right.Parent = m } m.Update() return m } func (n *node) MaximumNode() *node { if n == nil { return n } if n.Right == nil { return n } return n.Right.MaximumNode() } func (n *node) MinimumNode() *node { if n == nil { return n } if n.Left == nil { return n } return n.Left.MinimumNode() } func (n *node) AppendLeft(m *node) { if n == nil { return } n.Left = m n.Update() if m == nil { return } m.Parent = n } func (n *node) AppendRight(m *node) { if n == nil { return } n.Right = m n.Update() if m == nil { return } m.Parent = n } func (t *tree) Put(key, value interface{}) { if t.Root == nil { t.Root = newNode(nil, key, value) return } now := t.Root for { compared := t.Comparator(key, now.Key) switch { case compared == 0: now.Value = value return case compared < 0: if now.Left == nil { now.Left = newNode(now, key, value) t.Update(now) return } now = now.Left case compared > 0: if now.Right == nil { now.Right = newNode(now, key, value) t.Update(now) return } now = now.Right } } } func (t *tree) Get(key interface{}) (interface{}, bool) { now := t.Lookup(key) if now == nil { return nil, false } return now.Value, true } func (t *tree) Lookup(key interface{}) *node { now := t.Root for now != nil { compared := t.Comparator(key, now.Key) switch { case compared == 0: return now case compared < 0: now = now.Left case compared > 0: now = now.Right } } return nil } func (t *tree) Remove(key interface{}) { now := t.Lookup(key) if now == nil { return } parent := now.Parent partialTree := now.Left.Merge(now.Right) switch { case now.IsRoot(): t.Root = partialTree if t.Root != nil { t.Root.Parent = nil } case now.IsLeft(): parent.AppendLeft(partialTree) case now.IsRight(): parent.AppendRight(partialTree) } for parent != nil { parent.Update() parent = parent.Parent } } func (t *tree) Update(now *node) { for now != nil { now.Update() parent := now.Parent switch { case now.Left.Size() > now.Right.Size(): t.RotateRight(now.Left) case now.Left.Size() < now.Right.Size(): t.RotateLeft(now.Right) } now = parent } } func (t *tree) RotateLeft(now *node) { parent := now.Parent grand := parent.Parent parent.AppendRight(now.Left) now.AppendLeft(parent) switch { case grand == nil: t.Root = now now.Parent = nil case grand.Left == parent: grand.AppendLeft(now) case grand.Right == parent: grand.AppendRight(now) } } func (t *tree) RotateRight(now *node) { parent := now.Parent grand := parent.Parent parent.AppendLeft(now.Right) now.AppendRight(parent) switch { case grand == nil: t.Root = now t.Root.Update() now.Parent = nil case grand.Left == parent: grand.AppendLeft(now) case grand.Right == parent: grand.AppendRight(now) } } func (t *tree) Right() *node { return t.Root.MaximumNode() } func (t *tree) Left() *node { return t.Root.MinimumNode() } func (t *tree) Floor(key interface{}) (*node, bool) { now := t.Root var n *node for now != nil { compared := t.Comparator(key, now.Key) switch { case compared == 0: return now, true case compared < 0: now = now.Left case compared > 0: n = now now = now.Right } } return n, n != nil } func (t *tree) Ceiling(key interface{}) (*node, bool) { now := t.Root var n *node for now != nil { compared := t.Comparator(key, now.Key) switch { case compared == 0: return now, true case compared < 0: n = now now = now.Left case compared > 0: now = now.Right } } return n, n != nil } func (t *tree) Empty() bool { return t.Root == nil } func (t *tree) At(i int) (*node, bool) { var n *node now := t.Root for now != nil && i > 0 { if now.Left != nil { l := now.Left.Value.(int) if i <= l { now = now.Left continue } i -= l } m := now.Value.(int) if i <= m { n = now } i -= m now = now.Right } return n, n != nil }