|
| 1 | +local Point = {} |
| 2 | +Point.__index = Point |
| 3 | +Point.__lt = function(a, b) |
| 4 | + if a.x == b.x then |
| 5 | + return a.y < b.y |
| 6 | + end |
| 7 | + return a.x < b.x |
| 8 | +end |
| 9 | + |
| 10 | +function Point:new(x, y, idx) |
| 11 | + return setmetatable({ x = x, y = y, idx = idx }, Point) |
| 12 | +end |
| 13 | + |
| 14 | +while true do |
| 15 | + local ranks = {} |
| 16 | + |
| 17 | + function find_ranks(pts) |
| 18 | + local sz = #pts |
| 19 | + local l_sz = math.floor(sz / 2) |
| 20 | + local r_sz = sz - l_sz |
| 21 | + |
| 22 | + if sz == 1 then |
| 23 | + return pts |
| 24 | + end |
| 25 | + |
| 26 | + local pts_l = find_ranks(table.pack(table.unpack(pts, 1, l_sz))) |
| 27 | + local pts_r = find_ranks(table.pack(table.unpack(pts, l_sz + 1))) |
| 28 | + local res = {} |
| 29 | + local i, j = 1, 1 |
| 30 | + |
| 31 | + while true do |
| 32 | + if pts_l[i].y <= pts_r[j].y then |
| 33 | + table.insert(res, pts_l[i]) |
| 34 | + i = i + 1 |
| 35 | + if i > l_sz then |
| 36 | + while j <= r_sz do |
| 37 | + ranks[pts_r[j].idx] = ranks[pts_r[j].idx] + i - 1 |
| 38 | + table.insert(res, pts_r[j]) |
| 39 | + j = j + 1 |
| 40 | + end |
| 41 | + break |
| 42 | + end |
| 43 | + else |
| 44 | + table.insert(res, pts_r[j]) |
| 45 | + ranks[pts_r[j].idx] = ranks[pts_r[j].idx] + i - 1 |
| 46 | + j = j + 1 |
| 47 | + if j > r_sz then |
| 48 | + table.move(pts_l, i, l_sz, #res + 1, res) |
| 49 | + break |
| 50 | + end |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + return res |
| 55 | + end |
| 56 | + |
| 57 | + local pts = {} |
| 58 | + local n = io.read("*n") |
| 59 | + if n == 0 then |
| 60 | + break |
| 61 | + end |
| 62 | + |
| 63 | + for i = 1, n do |
| 64 | + x, y = io.read("*n", "*n") |
| 65 | + pts[i] = Point:new(x, y, i) |
| 66 | + ranks[i] = 0 |
| 67 | + end |
| 68 | + |
| 69 | + table.sort(pts, cmp) |
| 70 | + find_ranks(pts) |
| 71 | + print(table.concat(ranks, ' ')) |
| 72 | +end |
0 commit comments