using System; using System.IO; using System.Linq; using System.Collections.Generic; using System.Text; public class Program { public void Proc() { int[] qk = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray(); int q = qk[0]; int k = qk[1]; StringBuilder ans = new StringBuilder(); for (int i = 0; i < q; i++) { long[] inpt = Reader.ReadLine().Split(' ').Select(a => long.Parse(a)).ToArray(); if(inpt[0]==1) { long num = inpt[1]; if (TreeNode.Root == null) { TreeNode r = new TreeNode(num); } else { TreeNode.Root.Add(num); } } else { if(TreeNode.Root == null || TreeNode.Root.Count < k) { ans.AppendLine("-1"); } else { TreeNode c = TreeNode.Root.Get(k - 1); ans.AppendLine(c.Key.ToString()); c.Remove(); } } } Console.Write(ans.ToString()); } private class TreeNode { public void Remove() { if(--this.ValueValue>0) { return; } if(this.Left != null && this.Right != null) { TreeNode cp = this.Parent; TreeNode cc1; TreeNode cc2; if(this.Left.Length >= this.Right.Length) { cc1 = this.Left; cc2 = this.Right; } else { cc1 = this.Right; cc2 = this.Left; } cc1.Parent = cp; cc2.Parent = null; if(cp != null) { if(cp.Left == this) { cp.Left = cc1; } else { cp.Right = cc1; } } cc1.Add(cc2); return; } if(this.Left == null&&this.Right == null) { if(this.Parent == null) { TreeNode.Root = null; } else { if(this.Parent.Left == this) { this.Parent.Left = null; } else { this.Parent.Right = null; } this.Parent.Refresh(); } } else { TreeNode cp = this.Parent; TreeNode cc; if(this.Left != null) { cc = this.Left; } else { cc = this.Right; } cc.Parent = cp; if(cp == null) { TreeNode.Root = cc; } else { if(cp.Left == this) { cp.Left = cc; } else { cp.Right = cc; } cp.Refresh(); } } } public TreeNode Get(int idx) { if(this.Left == null && this.Right == null) { if(idx ==0) { return this; } else { return null; } } if(this.Left == null) { if(idx=this.Left.Count&&idx=2) { TreeNode cp = this.Parent; TreeNode cc; if(rightLength>leftLength) { cc = this.Right; this.Right = null; } else { cc = this.Left; this.Left = null; } cc.Parent = cp; if(cp != null) { if(cp.Right == this) { cp.Right = cc; } else { cp.Left = cc; } } cc.Add(this); return; } int length = Math.Max(leftLength, rightLength); if(this.Count==count&&this.Length==length&&(this.Left!=null||this.Right!=null)) { return; } this.CountValue = count; this.LengthValue = length; if(this.Parent!=null) { this.Parent.Refresh(); } else { Root = this; } } } public class Reader { private static StringReader sr; public static bool IsDebug = false; public static string ReadLine() { if (IsDebug) { if (sr == null) { sr = new StringReader(InputText.Trim()); } return sr.ReadLine(); } else { return Console.ReadLine(); } } private static string InputText = @" 15 3 1 3 1 4 1 5 2 2 1 10 1 10 1 1 2 1 3 2 1 1000 2 1 0 2 "; } public static void Main(string[] args) { #if DEBUG Reader.IsDebug = true; #endif Program prg = new Program(); prg.Proc(); } }