#include <iostream>
#include <vector>
#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);
    for (int i = 0; i < n; i++) cin >> a[i];

    // dp[i][j] initially stores exact count of triplets ending at i and j.
    // Later it transitions into storing the answer for subarray [i...j].
    vector<vector<long long>> dp(n, vector<long long>(n, 0));

    // O(N^3) base triplet counting
    for (int i = 0; i < n; i++) {
        for (int j = i + 2; j < n; j++) {
            for (int k = i + 1; k < j; k++) {
                if (a[i] + a[k] + a[j] == 0) {
                    dp[i][j]++;
                }
            }
        }
    }

    // 2D Prefix sums to answer queries in O(1)
    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
    while (q--) {
        int u, v;
        cin >> u >> v;
        u--; v--; // Convert to 0-indexed
        cout << dp[u][v] << "\n";
    }

    return 0;
}
