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

// Global arrays are automatically zero-initialized
long long a[505];
long long dp[505][505];

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