// Arup Guha
// 6/18/2020
// Solution to 2020 January Gold USACO Problem: Farmer John Solves 3SUM

import java.util.*;
import java.io.*;

public class threesum {
	
	final public static int OFFSET = 1000000;
	final public static int MAX = 2000001;
	
	public static void main(String[] args) throws Exception {
		
		// Read in array size, # queries.
		BufferedReader stdin = new BufferedReader(new FileReader("threesum.in"));
		StringTokenizer tok = new StringTokenizer(stdin.readLine());
		int n = Integer.parseInt(tok.nextToken());
		int numQ = Integer.parseInt(tok.nextToken());
		
		// locs[i+OFFSET] will be a cum freq array for # of times value i occurs.
		int[][] locs = new int[MAX][];
		for (int i=0; i<MAX; i++) locs[i] = null;
		
		boolean[] used = new boolean[MAX];
		
		// Store values of the array here.
		int[] vals = new int[n];
		tok = new StringTokenizer(stdin.readLine());
		for (int i=0; i<n; i++) {
			
			// Read in the value.
			vals[i] = Integer.parseInt(tok.nextToken());
		
			// Make this array if we need to.
			if (!used[vals[i] + OFFSET]) {
				used[vals[i] + OFFSET] = true;
				locs[vals[i] + OFFSET] = new int[n+1];
			}
			
			// Store 1 in appropriate index, off by 1 for cum freq...
			locs[vals[i] + OFFSET][i+1] = 1;
		}
		
		// Make all locs array, cumulative frequency.
		for (int i=0; i<MAX; i++) {
			if (locs[i] == null) continue;
			for (int j=1; j<=n; j++)
				locs[i][j] += locs[i][j-1];
		}
		
		// freq[i][j] will store all triples that start at index i-1 and end at index j-1.
		long[][] freq = new long[n+1][n+1];
		
		// Brute force ending location.
		for (int i=2; i<n; i++) {
			
			// and starting location.
			for (int j=0; j<i-1; j++) {
				
				// Sum we desire to pair with v[i], v[j].
				int target = -(vals[i]+vals[j]);
				
				// These cases add nothing.
				if (target < -OFFSET || target > OFFSET) continue;
				if (locs[target+OFFSET] == null) continue;
				
				// # of streaks added from j to i.
				int add = locs[target+OFFSET][i] - locs[target+OFFSET][j+1];
				
				// Do one based indexing so I can easily convert to cumulative frequency arrays.
				freq[j+1][i+1] = add;
			}
		}
		
		// Makes cumulative frequency - for rows.
		for (int i=0; i<=n; i++) 
			for (int j=1; j<=n; j++)
				freq[i][j] += freq[i][j-1];
			
		// Makes cumulative frequency - for cols.
		for (int j=0; j<=n; j++)
			for (int i=1; i<=n; i++)
				freq[i][j] += freq[i-1][j];
			
		// Store all answers here.
		StringBuffer sb = new StringBuffer();
		
		// Go through queries.
		for (int i=0; i<numQ; i++) {
			
			// Get query.
			tok = new StringTokenizer(stdin.readLine());
			int s = Integer.parseInt(tok.nextToken());
			int e = Integer.parseInt(tok.nextToken());
			
			// This is how you get the sum of a rectangle using a 2D cum freq array.
			long res = freq[e][e] - freq[s-1][e] - freq[e][s-1] + freq[s-1][s-1];
			sb.append(res+"\n");
		}
		
		// Output result.
		PrintWriter out = new PrintWriter(new FileWriter("threesum.out"));
		out.print(sb);
		out.close();		
		stdin.close();
	}
}