BZOJ 4397: [Usaco2015 dec]Breed Counting【Hash】

4397: [Usaco2015 dec]Breed Counting

【题目描述】
传送门

【题解】

简单的hash。

代码如下

#include<cstdio>
using namespace std;
int n,m,hsh[100005][5];
int main(){
    #ifndef ONLINE_JUDGE
    freopen("prob.in","r",stdin);
    freopen("prob.out","w",stdout);
    #endif
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        int x;scanf("%d",&x);hsh[i][x]++;
        for(int j=1;j<=3;j++) hsh[i][j]+=hsh[i-1][j];
    }
    for(int i=1;i<=m;i++){
        int x,y;scanf("%d%d",&x,&y);x--;
        printf("%d %d %d\n",hsh[y][1]-hsh[x][1],hsh[y][2]-hsh[x][2],hsh[y][3]-hsh[x][3]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41357771/article/details/80468314