Advertisement
Guest User

Untitled

a guest
Jul 12th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.96 KB | None | 0 0
  1. import com.sun.org.apache.xml.internal.utils.IntStack
  2. import java.io.*
  3. import java.util.*
  4.  
  5. class FastIO(private val inp: InputStream, private val out: OutputStream) : PrintWriter(out, false)
  6. {
  7.     private val reader = BufferedReader(InputStreamReader(inp))
  8.     private var tokenizer = StringTokenizer("")
  9.  
  10.     fun get() : String?
  11.     {
  12.         while (!tokenizer.hasMoreTokens())
  13.         {
  14.             val s = reader.readLine() ?: return null
  15.             tokenizer = StringTokenizer(s)
  16.         }
  17.  
  18.         return tokenizer.nextToken()
  19.     }
  20.  
  21.     fun getWord() : String = get()!!
  22.     fun getInt() : Int = getWord().toInt()
  23.     fun getLong() : Long = getWord().toLong()
  24.     fun getDouble() : Double = getWord().toDouble()
  25.  
  26.     fun getLine() : String?
  27.     {
  28.         tokenizer = StringTokenizer("")
  29.         return reader.readLine()
  30.     }
  31. }
  32.  
  33. fun main(args: Array<String>)
  34. {
  35. //    FastIO(FileInputStream("paint.in"), FileOutputStream("paint.out")).use {
  36. //        main(it)
  37. //    }
  38.  
  39.     FastIO(System.`in`, System.out).use {
  40.         main(it)
  41.     }
  42. }
  43.  
  44. fun main(io: FastIO)
  45. {
  46.     val n = io.getInt()
  47.  
  48.     // x y i
  49.     val data = MutableList<IntArray>(n, { IntArray(3, {-1}) })
  50.  
  51.     // p l r
  52.     val graph = MutableList<IntArray>(n, { IntArray(3, {-1}) })
  53.  
  54.     for (i in 0 until n)
  55.     {
  56.         data[i][0] = io.getInt() // x
  57.         data[i][1] = io.getInt() // y
  58.         data[i][2] = i
  59.     }
  60.  
  61.     data.sortBy { it[0] }
  62.  
  63.     val s = IntStack()
  64.  
  65.     var root = -1
  66.  
  67.     for (i in 0 until n)
  68.     {
  69. //        val j = data[i][2]
  70.  
  71.         while (true)
  72.         {
  73.             if (s.empty()) break
  74.             if (data[s.peek()][1] < data[i][1]) break
  75.  
  76.             s.pop()
  77.         }
  78.  
  79.         if (s.empty())
  80.         {
  81.             graph[i][1] = root
  82.             if (root != -1) graph[root][0] = i
  83.  
  84.             root = i
  85.         }
  86.         else
  87.         {
  88.             graph[i][1] = graph[s.peek()][2]
  89.             if (graph[i][1] != -1) graph[   graph[i][1]   ][0] = i
  90.  
  91.             graph[s.peek()][2] = i
  92.             graph[i][0] = s.peek()
  93.         }
  94.  
  95.         s.push(i)
  96.     }
  97.  
  98.     fun getId(t: Int): Int
  99.     {
  100.         if (t == -1) return 0
  101.         return data[t][2] + 1
  102.     }
  103.  
  104.     val shuffle = IntArray(n)
  105.  
  106.     for (i in 0 until n)
  107.     {
  108.         shuffle[i] = data[i][2]
  109.     }
  110.  
  111.     for (i in 0 until n)
  112.     {
  113.         graph[shuffle[i]][0] = getId(graph[shuffle[i]][0])
  114.         graph[shuffle[i]][1] = getId(graph[shuffle[i]][1])
  115.         graph[shuffle[i]][2] = getId(graph[shuffle[i]][2])
  116.     }
  117.  
  118. //    data.sortBy { it[5] }
  119.  
  120.     io.println("YES")
  121.  
  122.     for (i in 0 until n)
  123.     {
  124.         io.print(graph[shuffle[i]][0])
  125.         io.print(' ')
  126.         io.print(graph[shuffle[i]][1])
  127.         io.print(' ')
  128.         io.println(graph[shuffle[i]][2])
  129.     }
  130.  
  131. //    for (item in graph)
  132. //    {
  133. //        io.print(item[0])
  134. //        io.print(' ')
  135. //        io.print(item[1])
  136. //        io.print(' ')
  137. //        io.println(item[2])
  138. //    }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement