#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#define endl '\n'

using namespace std;

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

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

    vector<long long> a(n);
    unordered_map<long long, vector<int>> pos;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        pos[a[i]].push_back(i);
    }

    vector<vector<long long>> dp(n, vector<long long>(n, 0));

    // O(N^2 log N) - Binary Searching for the third element
    for (int i = 0; i < n; i++) {
        for (int j = i + 2; j < n; j++) {
            long long target = -(a[i] + a[j]);
            if (pos.count(target)) {
                auto& vec = pos[target];
                // Count indices strictly between i and j
                auto it1 = upper_bound(vec.begin(), vec.end(), i);
                auto it2 = lower_bound(vec.begin(), vec.end(), j);
                if (it1 < it2) {
                    dp[i][j] += (it2 - it1);
                }
            }
        }
    }

    // O(N^2) Range expansion
    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;
}
