题目描述

Farmer John's N cows, conveniently numbered 1…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 for Jerseys. Farmer John would like your help counting the number of cows of each breed that lie within certain intervals of the ordering.

输入

The first line of input contains N and Q (1≤N≤100,000, 1≤Q≤100,000).

The next N lines contain an integer that is either 1, 2, or 3, giving the breed ID of a single cow in the ordering.

The next Q lines describe a query in the form of two integers a,b (a≤b).

输出

For
each of the Q queries (a,b), print a line containing three numbers: the
number of cows numbered a…b that are Holsteins (breed 1), Guernseys
(breed 2), and Jerseys (breed 3).

样例输入

6 3
2
1
1
3
2
1
1 6
3 3
2 4

样例输出

3 2 1
1 0 0
2 0 1

  题目的意思就是给你n个牛槽的位置(编号从1到n),q是查询次数,每个牛槽里有一只奶牛,奶牛有三个品种(分别为1,2,3),告诉你每个牛槽中奶牛的种类。
给你q个查询的区间,让你输出每个查询区间内三种奶牛分别有多少头。
  这个题我写了两种解法,一种是前缀和数组(a[i]表示从1到i一共有多少头X品种牛),还有一种解法就是写树状数组。但是从运行时间上来看,肯定是前缀和要比树状数组要快。
前缀和数组代码如下:
 #include <bits/stdc++.h>
using namespace std;
int sum[][];
int main()
{
int n,q;
//freopen("de.txt","r",stdin);
while (~scanf("%d%d",&n,&q))
{
memset(sum,,sizeof sum);
for (int i=;i<=n;++i)
{
for (int j=;j<;++j)
sum[j][i]=sum[j][i-];
int x;
scanf("%d",&x);
sum[x-][i]++;
}
for (int i=;i<q;++i)
{
int x,y;
scanf("%d%d",&x,&y);
printf("%d %d %d\n",sum[][y]-sum[][x-],sum[][y]-sum[][x-],sum[][y]-sum[][x-]);
}
} }
   Time:100 ms
    Memory:2868 kb

树状数组代码如下:

 #include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;
#define maxn 111111
int sum[maxn<<][],sum2[maxn<<],sum3[maxn<<];
void pushup (int rt,int num)
{
sum[rt][num]=sum[rt<<][num]+sum[rt<<|][num];
} void update (int p,int l,int r,int rt,int num)
{
if (l==r)
{
sum[rt][num]++;
return;
}
int m=(l+r)>>;
if (p<=m)
update(p,l,m,rt<<,num);
else
update(p,m+,r,rt<<|,num);
pushup(rt,num);
}
int query (int ll,int rr,int l,int r,int rt,int num)
{
if (ll<=l&&rr>=r)
return sum[rt][num]; int ret=;
int m=(l+r)>>; if (ll<=m)
ret+=query(ll,rr,l,m,rt<<,num);
if (rr>m)
ret+=query(ll,rr,m+,r,rt<<|,num);
return ret;
}
int main()
{
int n,q;
//freopen("de.txt","r",stdin);
while (~scanf("%d%d",&n,&q))
{
memset(sum,,sizeof sum);
for (int i=;i<=n;++i)
{
int x;
scanf("%d",&x);
update(i,,n,,x-);
}
for (int i=;i<q;++i)
{
int x,y;
scanf("%d%d",&x,&y);
printf("%d ",query(x,y,,n,,));
printf("%d ",query(x,y,,n,,));
printf("%d\n",query(x,y,,n,,));
}
}
return ;
}
Time: ms
Memory: kb

PS:这个树状数组的模板是我hdu1166这个题的模板改的。

 

bzoj4397【Usaco2015 Dec】Breed Counting(前缀和、树状数组)的更多相关文章

  1. bzoj4397[Usaco2015 dec]Breed Counting*

    bzoj4397[Usaco2015 dec]Breed Counting 题意: 给定一个长度为N的序列,每个位置上的数只可能是1,2,3中的一种.有Q次询问,每次给定两个数a,b,请分别输出区间[ ...

  2. bzoj 4397: [Usaco2015 dec]Breed Counting -- 前缀和

    4397: [Usaco2015 dec]Breed Counting Time Limit: 10 Sec  Memory Limit: 128 MB Description Farmer John ...

  3. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  4. HDU 5862 Counting Intersections (树状数组)

    Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...

  5. 13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 2224: Boring Counting Time Limit: 3 Sec   ...

  6. HDU 5862 Counting Intersections 扫描线+树状数组

    题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Counting Intersections Time Limit: 12000/ ...

  7. TOJ 4105 Lines Counting(离线树状数组)

    4105.   Lines Counting Time Limit: 2.0 Seconds   Memory Limit: 150000K Total Runs: 152   Accepted Ru ...

  8. BZOJ-2743: [HEOI2012]采花 前缀和 树状数组

    BZOJ-2743 LUOGU:https://www.luogu.org/problemnew/show/P4113 题意: 给一个n长度的序列,m次询问区间,问区间中出现两次及以上的数字的个数.n ...

  9. Gym - 101630G The Great Wall (前缀和+树状数组+二分)

    题意:有一个序列,一开始所有的元素都是ai,你可以选择两个长度相等的区间,如果某个元素被一个区间覆盖,那么变为bi,如果被两个区间都覆盖,那么变为ci.问所有区间的选择方法中产生的第k小的元素总和. ...

  10. [CSP-S模拟测试]:斯诺(snow)(数学+前缀和+树状数组)

    题目传送门(内部题37) 输入格式 第一行一个整数$n$,表示区间的长度. 第二行一个长度为$n$的只包含$0,1,2$的字符串,表示给出的序列. 输出格式 一行一个整数,表示革命的区间的数量. 样例 ...

随机推荐

  1. redis centos集群搭建和java应用

    1. 首先要ssh免密登录 redis集群,3台虚拟机,6个节点,每台机器2个节点一主一从. 192.168.132.154 c0192.168.132.156 c1192.168.132.155 c ...

  2. eclipse下实现热部署,tomcat不重新reload context

    1. 打开server的编辑器 2. 在modules页签内,修改auto load属性为disabled

  3. centos6.5下apollo1.7.1的搭建

    前言:apollo MQ作为消息队列中间件,在需要消息列表的应用程序环境中,需要使用该服务器中间件 1.准备工作 2.搭建 3.测试 1.准备工作 第一步:linux系统中配置好java环境 A.卸载 ...

  4. Vagrant 手册之多个虚拟机 multi-machine

    原文地址 Vagrant 可以通过一个 Vagrantfile 定义并控制多个客户机.这就是所谓的"multi-machine"多虚拟机环境. 这些机器通常可以协同工作,或者互相关 ...

  5. Python笔记(十四)_永久存储pickle

    pickle模块:将所有的Python对象转换成二进制文件存放 应用场景:编程时最好将大对象(列表.字典.集合等)用pickle写成永久数据包供程序调用,而不是直接写入程序 写入过程:将list转换为 ...

  6. maven添加oracle和sqlserver报错

    Failure to find com.oracle:ojdbc6:jar:12.1.0.1-atlassian-hosted in 'xxx' Missing artifact com.micros ...

  7. 08 (h5*) js第9天--原型、继承

    目录: 1:原型和原型链 2:构造函数的原型可以改变 3:原型的最终指向 4:先修改原型指向,在添加方法. 5:实例对象中的属性和原型属性重合, 6:一个神奇的原型链 7:继承 8:原型链 9:利用c ...

  8. IDEA远程代码实时同步(可以自动实时同步)

    前言 开发时一般的平台都是windows,但windows对开发极其不友好,一般都会在本地开启虚拟机,安装上linux环境进行项目的部署测试.下面介绍一种windows主机与linux虚拟机代码同步的 ...

  9. [ARC083]Collecting Balls

    Description 有一个 \(n\times n\) 的矩阵,矩阵内有 \(2n\) 个球.对于 \(i \in [1,n]\) ,\((0,i) (i,0)\) 的位置各有一个启动后往右走/往 ...

  10. 前端webpack & vue

    地址 : https://blog.csdn.net/jiang7701037