Problem 718
Number of Greater Elements to the Right
Medium · 20 pts · Array, Fenwick Tree, Offline Queries
Problem
You are given an array and Q queries. Each query is an index i (counting from 0), and asks how many elements to the right of position i are strictly greater than A[i]. Answer every query. Counting each query separately is O(N) per query. Instead, compute the answer for every index at once: scan from right to left and keep a Fenwick tree over the compressed values seen so far, so each index needs one prefix query.
Input
The first line contains N and Q. The second line contains the N values. The third line contains the Q query indices.
Output
Print Q integers separated by spaces, one answer per query.
Constraints
1 <= N, Q <= 100000; -10^9 <= A[i] <= 10^9; 0 <= index < N
Example input
8 2 3 4 2 7 5 8 10 6 0 5
Example output
6 1
Explanation
For 3 4 2 7 5 8 10 6 with queries 0 and 5: after index 0 (value 3), the larger values are 4, 7, 5, 8, 10 and 6, so 6. After index 5 (value 8), only 10 is larger, so 1. The answer is 6 1.