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

const int OFFSET = 2000000;

// Maximum bounds for N=5000 testcases
long long a[5005];
long long dp[5005][5005];
int freq[4000005];

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];
    }

    // O(N^2) Exact Count Tracking using a sliding window frequency map
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            long long target = -(a[i] + a[j]);
            if (target >= -2000000 && target <= 2000000) {
                dp[i][j] = freq[target + OFFSET];
            }
            freq[a[j] + OFFSET]++;
        }
        
        // Clean up the frequency array for the next 'i' iteration
        for (int j = i + 1; j < n; j++) {
            freq[a[j] + OFFSET]--;
        }
    }

    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;
}
