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

using namespace std;

int main() {
    // Fast I/O
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

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

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

    // Sort to group identical values and order them by original index
    // Takes O(N log N)
    sort(sorted_a.begin(), sorted_a.end());

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

    // O(N^2 log N) - Binary Searching on a contiguous, cache-friendly array
    for (int i = 0; i < n; i++) {
        for (int j = i + 2; j < n; j++) {
            long long target = -(a[i] + a[j]);
            
            // We want to find elements with value == target and i < index < j.
            // C++ pairs are compared by first element, then second.
            pair<long long, int> lower_val = {target, i + 1};
            pair<long long, int> upper_val = {target, j};
            
            // lower_bound finds the first element >= our pair
            auto it1 = lower_bound(sorted_a.begin(), sorted_a.end(), lower_val);
            auto it2 = lower_bound(sorted_a.begin(), sorted_a.end(), upper_val);
            
            // The distance between these iterators is exactly the count of valid k's
            if (it1 < it2) {
                dp[i][j] = it2 - it1;
            }
        }
    }

    // O(N^2) Range expansion via 2D Prefix Sums
    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];
        }
    }

    // Answer Q queries in O(1) each
    while (q--) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        cout << dp[u][v] << "\n";
    }

    return 0;
}
