Розбір
Let's declare three integer arrays . The prefix sums for cows of breed will be stored in the array .
The answer to the query is computed as follows:
The number of cows of breed is ;
The number of cows of breed is ;
The number of cows of breed is ;
Example
For the given example, the prefix sum arrays will be as follows:
Consider the query on the interval :
The number of cows of breed is ;
The number of cows of breed is ;
The number of cows of breed is ;
Algorithm realization
Declare arrays for storing prefix sums.
#define MAX 100001 int s[4][MAX];
Read the input data.
scanf("%d %d", &n, &q);
Compute the prefix sum arrays.
memset(s, 0, sizeof(s)); for (i = 1; i <= n; i++) { scanf("%d", &x); for (j = 1; j <= 3; j++) s[j][i] = s[j][i - 1]; s[x][i]++; }
Process the queries sequentially.
for (i = 0; i < q; i++) { scanf("%d %d", &a, &b); printf("%d %d %d\n", s[1][b] - s[1][a - 1], s[2][b] - s[2][a - 1], s[3][b] - s[3][a - 1]); }