Untitled
unknown
java
a year ago
3.4 kB
15
Indexable
import java.io.*;
import java.util.*;
public class Main {
static class FastScanner {
private final InputStream in;
private final byte[] buffer = new byte[1 << 16];
private int ptr = 0, len = 0;
FastScanner(InputStream is) { in = is; }
private int read() throws IOException {
if (ptr >= len) {
len = in.read(buffer);
ptr = 0;
if (len <= 0) return -1;
}
return buffer[ptr++];
}
int nextInt() throws IOException {
int c; do { c = read(); } while (c <= ' ' && c != -1);
int sign = 1;
if (c == '-') { sign = -1; c = read(); }
int val = 0;
while (c > ' ') { val = val * 10 + (c - '0'); c = read(); }
return val * sign;
}
long nextLong() throws IOException {
int c; do { c = read(); } while (c <= ' ' && c != -1);
int sign = 1;
if (c == '-') { sign = -1; c = read(); }
long val = 0;
while (c > ' ') { val = val * 10 + (c - '0'); c = read(); }
return val * sign;
}
}
public static void main(String[] args) throws Exception {
FastScanner fs = new FastScanner(System.in);
StringBuilder out = new StringBuilder();
int T = fs.nextInt();
while (T-- > 0) {
int N = fs.nextInt();
int M = fs.nextInt();
int Q = fs.nextInt();
long[] A = new long[N + 1];
for (int i = 1; i <= N; i++) A[i] = fs.nextLong();
long[] diff = new long[N + 2];
for (int i = 0; i < M; i++) {
int l = fs.nextInt();
int r = fs.nextInt();
diff[l] += 1;
if (r + 1 <= N) diff[r + 1] -= 1;
}
long[] freq = new long[N + 1];
for (int i = 1; i <= N; i++) freq[i] = freq[i - 1] + diff[i];
Map<Long, Long> map = new HashMap<>();
for (int i = 1; i <= N; i++) {
if (freq[i] > 0) {
map.put(A[i], map.getOrDefault(A[i], 0L) + freq[i]);
}
}
int size = map.size();
long[][] arr = new long[size][2];
int idx = 0;
for (Map.Entry<Long, Long> e : map.entrySet()) {
arr[idx][0] = e.getKey();
arr[idx][1] = e.getValue();
idx++;
}
Arrays.sort(arr, Comparator.comparingLong(x -> x[0]));
long[] prefix = new long[size];
prefix[0] = arr[0][1];
for (int i = 1; i < size; i++) {
prefix[i] = prefix[i - 1] + arr[i][1];
}
long total = prefix[size - 1];
for (int i = 0; i < Q; i++) {
long K = fs.nextLong();
if (K > total) {
out.append(-1).append(" ");
} else {
int pos = Arrays.binarySearch(prefix, K);
if (pos < 0) pos = -pos - 1;
out.append(arr[pos][0]).append(" ");
}
}
out.append("\n");
}
System.out.print(out);
}
}
Editor is loading...
Leave a Comment