#include <iostream>
#include <algorithm>
#define endl '\n'
using namespace std;

long long a[2005];
pair<long long, int> sorted_a[2005];
long long dp[2005][2005];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, q;
    if (!(cin >> n >> q)) return 0;

    for (int i = 0; i < n; i++) {
        cin >> a[i];
        sorted_a[i] = {a[i], i};
    }

    // Sort the C-style array using pointers
    sort(sorted_a, sorted_a + n);

    // O(N^2 log N) - Binary Searching on contiguous arrays
    for (int i = 0; i < n; i++) {
        for (int j = i + 2; j < n; j++) {
            long long target = -(a[i] + a[j]);
            
            pair<long long, int> lower_val = {target, i + 1};
            pair<long long, int> upper_val = {target, j};
            
            // lower_bound works with raw pointers
            auto it1 = lower_bound(sorted_a, sorted_a + n, lower_val);
            auto it2 = lower_bound(sorted_a, sorted_a + n, upper_val);
            
            if (it1 < it2) {
                dp[i][j] = it2 - it1;
            }
        }
    }

    for (int len = 3; len <= n; len++) {
        for (int i = 0; i + len - 1 < n; i++) {
            int j = i + len - 1;
            dp[i][j] += dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1];
        }
    }

    while (q--) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        cout << dp[u][v] << "\n";
    }

    return 0;
}
