题目描述

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. JS-闭包(Closures)和let声明块级作用域变量

    闭包: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Closures 闭包是函数和声明该函数的词法环境的组合. let: https ...

  2. 《图解设计模式》读书笔记5-1 composite模式

    目录 代码 角色 想法 Composite模式即组合模式.它能够使容器和内容具有一致性,创造出递归结构. 举个例子:在文件系统中,文件夹既是内容,也是容器,具有一致性,这样一来,文件系统形成递归结构. ...

  3. delphi中的idhttpserver如何才能收到idhttp发送来的exe\rar文件呢

    http://zhidao.baidu.com/link?url=-q2oXqYCKBZ9OgFDEHAcQwQEY_NroHcqGvVfKW67X5sF9LdjAAB_HPXQo04VxStFVS7 ...

  4. 第 3 章 前端基础之JavaScript

    一.JavaScript概述 1.javascripts的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中),后将其改名ScriptE ...

  5. java c 标签的使用

    头部需要引入: <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 需要 ...

  6. HTML--JS 多列求和

    <html> <head> <title>多列求和</title> <script type="text/javascript" ...

  7. crontab自动执行任务,失败原因记录

    服务器上使用crontab部署这两个每分钟自动执行的命令.首先,这两个命令是之前的人部署的,在我接手之前,就一直在了的.根据命令,实际上应该是做到每分钟都执行一次脚本.但是实际操作中,却发现,其实并没 ...

  8. 网络流强化-UVA10480

    做这道题,自己先是想了好几种找被割的边的方法——都被否决了. 后来发现是最小割:只要一条边的两端在不同的点集里面就代表是被割掉的满流边(这些满流边的流量和等于最大流的流量与最小割的权值和). 但是之前 ...

  9. Android深度探索-卷1第十章心得体会

    本章介绍了传统的printk 函数调试技术和其他的调试技术,如gdb gdbserver  kgdb 对于复杂的Linux 驱动及HAL 等程序库,需要使用各种方法对其进行调试,如,设置断点.逐步跟踪 ...

  10. Orcle获取当前时间加小时

    如下是oracle 获取当前数据库时间加2个小时 select to_date(TO_CHAR (SYSDATE, 'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd hh24: ...