본문 바로가기

USACO/Silver

백준 11969 Breed Counting (USACO December 2015 Silver 3번)

문제 링크: https://www.acmicpc.net/problem/11969

 

11969번: Breed Counting

Farmer John's \(N\) cows, conveniently numbered \(1 \ldots N\), are all standing in a row (they seem to do so often that it now takes very little prompting from Farmer John to line them up). Each cow has a breed ID: 1 for Holsteins, 2 for Guernseys, and 3

www.acmicpc.net

Prefix Sum

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <bits/stdc++.h>
using namespace std;
 
int N, Q, cow[4][100001];
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    cin >> N >> Q;
    for(int i = 1; i <= N; i++) {
        int num;
        cin >> num;
        for(int j = 1; j <= 3; j++) {
            if(num == j) cow[j][i] = cow[j][i - 1+ 1;
            else cow[j][i] = cow[j][i - 1];
        }
    }
 
    while(Q--) {
        int a, b;
        cin >> a >> b;
        for(int i = 1; i <= 3; i++)
            cout << cow[i][b] - cow[i][a - 1<< ' ';
        cout << '\n';
    }
 
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter