using System;
using System.Linq;
class Query
{
public int l, r, x, idx;
public Query(int l, int r, int x, int idx) {
this.l = l;
this.r = r;
this.x = x;
this.idx = idx;
}
}
class ArrayElement : IComparable<ArrayElement>
{
public int val, idx;
public ArrayElement(int val, int idx) {
this.val = val;
this.idx = idx;
}
public int CompareTo(ArrayElement other) {
return this.val.CompareTo(other.val);
}
}
public class GFG
{
static bool cmp1(Query q1, Query q2)
{
return q1.x < q2.x;
}
static void update(int[] bit, int idx, int val, int n)
{
for (; idx <= n; idx += idx & -idx)
bit[idx] += val;
}
static int query(int[] bit, int idx, int n)
{
int sum = 0;
for (; idx > 0; idx -= idx & -idx)
sum += bit[idx];
return sum;
}
static void answerQueries(int n, Query[] queries, int q, ArrayElement[] arr)
{
int[] bit = new int[n + 1];
Array.Fill(bit, 0);
Array.Sort(arr);
Array.Sort(queries, (q1, q2) => q1.x.CompareTo(q2.x));
int curr = 0;
int[] ans = new int[q];
for (int i = 0; i < q; i++)
{
while (curr < n && arr[curr].val <= queries[i].x)
{
update(bit, arr[curr].idx + 1, 1, n);
curr++;
}
ans[queries[i].idx] = query(bit, queries[i].r + 1, n) -
query(bit, queries[i].l, n);
}
for (int i = 0; i < q; i++)
Console.WriteLine(ans[i]);
}
public static void Main(string[] args)
{
int n = 4;
ArrayElement[] arr = new ArrayElement[n];
arr[0] = new ArrayElement(2, 0);
arr[1] = new ArrayElement(3, 1);
arr[2] = new ArrayElement(4, 2);
arr[3] = new ArrayElement(5, 3);
int q = 2;
Query[] queries = new Query[q];
queries[0] = new Query(0, 2, 2, 0);
queries[1] = new Query(0, 3, 5, 1);
answerQueries(n, queries, q, arr);
}
}