Java | BIT ( Fenwick Tree )
\nclass Solution {\n    \n    private int N;\n    private HashMap<Integer, TreeSet<Integer>> tree;\n    \n    private void update(int rs, int id) {\n        rs 
```\nclass Solution {\n    \n    private int N;\n    private HashMap<Integer, TreeSet<Integer>> tree;\n    \n    private void update(int rs, int id) {\n        rs = N - rs + 1;\n        for (;rs <= N; rs += rs & (-rs)){\n            tree.put(rs, tree.getOrDefault(rs,new TreeSet<>()));\n            tree.get(rs).add(id);\n        }\n    }\n    \n    private int query(int rs, int rid) {\n        rs = N - rs + 1;\n        int res = -1, min_diff = Integer.MAX_VALUE, diff;\n        for (; rs > 0; rs -= rs & (-rs)) {\n            \n            TreeSet<Integer> ts = tree.get(rs);\n            if (ts==null || ts.size() == 0){\n                continue;\n            } \n            \n            Integer id = ts.ceiling(rid);\n            \n            if (id != null) {\n                diff = id - rid;\n                if (diff < min_diff){\n                    res = id;\n                    min_diff = diff;\n                }else if (diff == min_diff){\n                    res = Math.min(res, id);\n                } \n            }\n            id = ts.floor(rid);\n            if (id != null) {\n                \n                diff = rid - id;\n                if (diff < min_diff){\n                    res = id;\n                    min_diff = diff;\n                }else if (diff == min_diff){\n                    res = Math.min(res, id);\n                } \n            }\n        }\n        return res;\n    }\n    public int[] closestRoom(int[][] rooms, int[][] queries) {\n        \n        tree = new HashMap<>();\n        N = 10_000_000;\n        for (int[] r : rooms) {\n            update(r[1], r[0]);\n        }\n    \n        int[] result = new int[queries.length];\n        \n        for(int i = 0; i < queries.length; i++){\n            result[i] = query(queries[i][1], queries[i][0]);\n        }\n        \n        return result;\n        \n    }\n}\n```
[]