Skip to content

Instantly share code, notes, and snippets.

@chermehdi
Created October 9, 2018 20:40
Show Gist options
  • Save chermehdi/b284c643851813a97388e189b587ce24 to your computer and use it in GitHub Desktop.
Save chermehdi/b284c643851813a97388e189b587ce24 to your computer and use it in GitHub Desktop.
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Comparator;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author MaxHeap
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
LittleDeepuAndArray solver = new LittleDeepuAndArray();
solver.solve(1, in, out);
out.close();
}
static class LittleDeepuAndArray {
public void solve(int testNumber, FastReader in, PrintWriter out) {
int n = in.nextInt();
Pair[] arr = new Pair[n];
for (int i = 0; i < n; i++) {
arr[i] = new Pair(in.nextInt(), i);
}
Arrays.sort(arr, Comparator.comparing(e -> e.value));
int q = in.nextInt();
// build the segTree from the sorted array here
while (q-- > 0) {
int cur = in.nextInt();
int lo = 0, hi = n - 1;
int r = -1;
while (lo <= hi) {
int mid = (lo + hi) >> 1;
// here you should do segTree.query(mid).value > cur
if (arr[mid].value > cur) {
r = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
if (r == -1) continue;
// here we should use the segTree.update(r, n, -1)
for (int i = r; i < n; i++) arr[i].value--;
}
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
ans[arr[i].index] = arr[i].value;
}
for (int i = 0; i < n; i++) {
if (i > 0) out.print(" ");
out.print(ans[i]);
}
}
class Pair {
int value;
int index;
public Pair(int value, int index) {
this.value = value;
this.index = index;
}
}
}
static class FastReader {
BufferedReader reader;
StringTokenizer st;
public FastReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
st = null;
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
String line = reader.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment