题目描述

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. 点击" ./start_navicat"安装出现界面便面为乱码

    环境:ubuntu16.4 下载Navicat:navicat112_mariadb_cs_x64.tar.gz 点击" ./start_navicat"安装出现界面便面为乱码 解 ...

  2. Android中国官网资源网站

    现在android开发者官网在中国有中文版已经不是太大的新闻,为了平时查询资料和学习方便,记录如下. PS:建议用Google浏览器浏览,你懂的!! https://developers.google ...

  3. Java常用工具——java包装类

    一.包装类和基本数据类型 装箱:基本数据类型——包装类 拆箱:包装类——基本数据类型 package com.imooc.wrap; public class WrapTestOne { public ...

  4. Vagrant 手册之 Vagrantfile - 配置版本

    原文地址 配置版本是 Vagrant 1.1+(引入了大量新功能和配置选项) 能够与 Vagrant 1.0.x Vagrantfiles 保持向后兼容的机制. 现在运行 vagrant init 时 ...

  5. WPF样式统一之DevExpress设置窗体,控件为Office风格

    DevExpress相信不少人用过,虽然人家不是免费的,但是用过的应该都知道,确实是花了心血的C#插件,下面来介绍下在DevExpress下如何统一设置自己的WPF程序为经典Windows风格. 窗体 ...

  6. JQuery 字符串转时间格式

    //字符串转时间格式 function getDate(strDate) { var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$) ...

  7. 12.定义Lock类,用于锁定数据.三步走,锁的优缺点

    #在threading模块当中定义了一个Lock类,可以方便的使用锁定: # #1.创建锁 # mutex = threading.Lock() # # #2.锁定 ''' mutex.acquire ...

  8. Redis进行数据同步

    数据库中的数据一般都涉及到需要对数据进行备份的,这样可以保证数据的安全性,并且如果将一个主设备的数据同步到多个从设备上,允许用户访问数据时可以从多个从设备进行读取, 这样还可以缓解主设备的压力,Red ...

  9. mysql 5.7 事务隔离级别

    事务的隔离级别分为:未提交读(read uncommitted).已提交读(read committed).可重复读(repeatable read).串行化(serializable). 未提交读: ...

  10. Javascript中中括号的几种形式

    有以下几种形式 var arr = []; var b = [1,1,1]; var c = b[0]; var obj = {'name':'tom','age':23}; var d = obj[ ...