class Solution {
int[] xs = new int[300010];
int[] p = new int[300010];
int[] w = new int[300010];
public int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
Set<Integer> set = new TreeSet();
Map<Integer, Integer> map = new HashMap();
public int[] minInterval(int[][] intervals, int[] queries) {
for (int[] interval : intervals) {
set.add(interval[0]);
set.add(interval[1]);
}
for (int query : queries) set.add(query);
int j = 0;
for (int v : set) {
map.put(v, j);
xs[j] = v;
j ++;
}
int n = j;
for (int i = 0; i < n + 1; i++) {
p[i] = i;
w[i] = -1;
}
Arrays.sort(intervals, (a, b) -> (a[1] - a[0] + 1) - (b[1] - b[0] + 1));
for (int[] interval : intervals) {
int l = interval[0], r = interval[1];
int len = r - l + 1;
l = map.get(l);
r = map.get(r);
while(find(l) <= r) {
l = find(l);
p[l] = l + 1;
w[l] = len;
}
}
int k = queries.length;
int[] res = new int[k];
for (int i = 0; i < k; i++) res[i] = w[map.get(queries[i])];
return res;
}
}