Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sun.org.apache.xml.internal.utils.IntStack
- import java.io.*
- import java.util.*
- class FastIO(private val inp: InputStream, private val out: OutputStream) : PrintWriter(out, false)
- {
- private val reader = BufferedReader(InputStreamReader(inp))
- private var tokenizer = StringTokenizer("")
- fun get() : String?
- {
- while (!tokenizer.hasMoreTokens())
- {
- val s = reader.readLine() ?: return null
- tokenizer = StringTokenizer(s)
- }
- return tokenizer.nextToken()
- }
- fun getWord() : String = get()!!
- fun getInt() : Int = getWord().toInt()
- fun getLong() : Long = getWord().toLong()
- fun getDouble() : Double = getWord().toDouble()
- fun getLine() : String?
- {
- tokenizer = StringTokenizer("")
- return reader.readLine()
- }
- }
- fun main(args: Array<String>)
- {
- // FastIO(FileInputStream("paint.in"), FileOutputStream("paint.out")).use {
- // main(it)
- // }
- FastIO(System.`in`, System.out).use {
- main(it)
- }
- }
- fun main(io: FastIO)
- {
- val n = io.getInt()
- // x y i
- val data = MutableList<IntArray>(n, { IntArray(3, {-1}) })
- // p l r
- val graph = MutableList<IntArray>(n, { IntArray(3, {-1}) })
- for (i in 0 until n)
- {
- data[i][0] = io.getInt() // x
- data[i][1] = io.getInt() // y
- data[i][2] = i
- }
- data.sortBy { it[0] }
- val s = IntStack()
- var root = -1
- for (i in 0 until n)
- {
- // val j = data[i][2]
- while (true)
- {
- if (s.empty()) break
- if (data[s.peek()][1] < data[i][1]) break
- s.pop()
- }
- if (s.empty())
- {
- graph[i][1] = root
- if (root != -1) graph[root][0] = i
- root = i
- }
- else
- {
- graph[i][1] = graph[s.peek()][2]
- if (graph[i][1] != -1) graph[ graph[i][1] ][0] = i
- graph[s.peek()][2] = i
- graph[i][0] = s.peek()
- }
- s.push(i)
- }
- fun getId(t: Int): Int
- {
- if (t == -1) return 0
- return data[t][2] + 1
- }
- val shuffle = IntArray(n)
- for (i in 0 until n)
- {
- shuffle[i] = data[i][2]
- }
- for (i in 0 until n)
- {
- graph[shuffle[i]][0] = getId(graph[shuffle[i]][0])
- graph[shuffle[i]][1] = getId(graph[shuffle[i]][1])
- graph[shuffle[i]][2] = getId(graph[shuffle[i]][2])
- }
- // data.sortBy { it[5] }
- io.println("YES")
- for (i in 0 until n)
- {
- io.print(graph[shuffle[i]][0])
- io.print(' ')
- io.print(graph[shuffle[i]][1])
- io.print(' ')
- io.println(graph[shuffle[i]][2])
- }
- // for (item in graph)
- // {
- // io.print(item[0])
- // io.print(' ')
- // io.print(item[1])
- // io.print(' ')
- // io.println(item[2])
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement