'Why does my code to USACO Silver Breed Counting not work?

This is my code:

#include <bits/stdc++.h>
using namespace std;

int main() {
    freopen("bcount.in", "r", stdin);
    freopen("bcount.out", "w", stdout);
    int n, q;
    cin >> n >> q;
    vector<int> holsteins(n);
    vector<int> guernseys(n);
    vector<int> jerseys(n);
    for (int i = 0 ; i < n ; i++) {
        holsteins[i+1]=holsteins[i];
        guernseys[i+1]=guernseys[i];
        jerseys[i+1]=jerseys[i];
        int a;
        cin >> a;
        if (a==1) holsteins[i+1]++;
        else if (a==2) guernseys[i+1]++;
        else jerseys[i+1]++;
    }
    for (int i = 0; i < q ; i++) {
        int a, b;
        cin >> a >> b;
        cout << holsteins[b]-holsteins[a-1] << " " << guernseys[b]-guernseys[a-1] << " " << jerseys[b]-jerseys[a-1] << "\n";
    }
    return 0;
}

When I run it, it fails to pass the sample case and the official grader says that there was a runtime error or memory fail. I suspected it had something w/ input output but there wasn't. What's wrong here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source