import java.util.*; import java.io.*; public class Main { public static void main (String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] first = br.readLine().split(" ", 2); int n = Integer.parseInt(first[0]); int q = Integer.parseInt(first[1]); Point[] points = new Point[n]; String[] second = br.readLine().split(" ", n); for (int i = 0; i < n; i++) { points[i] = new Point(i + 1, Integer.parseInt(second[i])); } Arrays.sort(points); TreeSet num = new TreeSet<>(); num.add(0); ArrayList list = new ArrayList<>(); for (Point p : points) { list.add(new Order(p.idx, num.lower(p.idx))); num.add(p.idx); } for (int i = 0; i < q; i++) { String[] line = br.readLine().split(" ", 4); list.add(new Order(i, Integer.parseInt(line[1]), Integer.parseInt(line[2]))); } BinaryIndexedTree counts = new BinaryIndexedTree(n + 1); Collections.sort(list); int[] ans = new int[q]; for (Order x : list) { if (x.isValue()) { counts.add(x.idx, 1); } else { ans[x.idx] = counts.getSum(x.left, x.right); } } StringBuilder sb = new StringBuilder(); for (int x : ans) { sb.append(x).append("\n"); } System.out.print(sb); } static class Point implements Comparable { int idx; int value; public Point(int idx, int value) { this.idx = idx; this.value = value; } public int compareTo(Point another) { if (value == another.value) { return idx - another.idx; } else { return another.value - value; } } } static class Order implements Comparable { int idx; int left; int right; public Order(int idx, int left, int right) { this.idx = idx; this.left = left; this.right = right; } public Order(int idx, int left) { this(idx, left, 0); } public boolean isValue() { return right == 0; } public int compareTo(Order another) { if (left == another.left) { return another.right - right; } else { return left - another.left; } } public String toString() { return idx + ":" + left + ":" + right; } } } class BinaryIndexedTree { int size; int[] tree; public BinaryIndexedTree(int size) { this.size = size; tree = new int[size]; } public void add(int idx, int value) { int mask = 1; while (idx < size) { if ((idx & mask) != 0) { tree[idx] += value; idx += mask; } mask <<= 1; } } public int getSum(int from, int to) { return getSum(to) - getSum(from - 1); } public int getSum(int x) { int mask = 1; int ans = 0; while (x > 0) { if ((x & mask) != 0) { ans += tree[x]; x -= mask; } mask <<= 1; } return ans; } }