bzoj4397【Usaco2015 Dec】Breed Counting(前缀和、树状数组)
题目描述
输入
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).
输出
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(前缀和、树状数组)的更多相关文章
- bzoj4397[Usaco2015 dec]Breed Counting*
bzoj4397[Usaco2015 dec]Breed Counting 题意: 给定一个长度为N的序列,每个位置上的数只可能是1,2,3中的一种.有Q次询问,每次给定两个数a,b,请分别输出区间[ ...
- bzoj 4397: [Usaco2015 dec]Breed Counting -- 前缀和
4397: [Usaco2015 dec]Breed Counting Time Limit: 10 Sec Memory Limit: 128 MB Description Farmer John ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- HDU 5862 Counting Intersections (树状数组)
Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...
- 13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 2224: Boring Counting Time Limit: 3 Sec ...
- HDU 5862 Counting Intersections 扫描线+树状数组
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Counting Intersections Time Limit: 12000/ ...
- TOJ 4105 Lines Counting(离线树状数组)
4105. Lines Counting Time Limit: 2.0 Seconds Memory Limit: 150000K Total Runs: 152 Accepted Ru ...
- BZOJ-2743: [HEOI2012]采花 前缀和 树状数组
BZOJ-2743 LUOGU:https://www.luogu.org/problemnew/show/P4113 题意: 给一个n长度的序列,m次询问区间,问区间中出现两次及以上的数字的个数.n ...
- Gym - 101630G The Great Wall (前缀和+树状数组+二分)
题意:有一个序列,一开始所有的元素都是ai,你可以选择两个长度相等的区间,如果某个元素被一个区间覆盖,那么变为bi,如果被两个区间都覆盖,那么变为ci.问所有区间的选择方法中产生的第k小的元素总和. ...
- [CSP-S模拟测试]:斯诺(snow)(数学+前缀和+树状数组)
题目传送门(内部题37) 输入格式 第一行一个整数$n$,表示区间的长度. 第二行一个长度为$n$的只包含$0,1,2$的字符串,表示给出的序列. 输出格式 一行一个整数,表示革命的区间的数量. 样例 ...
随机推荐
- 126B Password[扩展kmp学习]
题目大意 给你一个字符串,求它的一个子串使得这个子串即使前缀又是后缀又出现在不是前缀且不是后缀的地方 分析 扩展kmp就是定义z[i]表示i~n的子串与整个串的最长公共前缀的长度是z[i] 所以这个题 ...
- canvas绘制加载特效
css样式: body{ text-align: center; } canvas{ background: #ddd; } canvas标签: <canvas id="canvas& ...
- CET-6 分频周计划生词筛选(番外篇:百词斩)
点我阅读 番外-百词斩 2016.09.18 12:00pm transverse counterpart accessory cult gorgeous sediment assimilate st ...
- Java IO(1)
IO这一部分内容还是比较多的,对于基础的枯燥但是又重要的内容还是将它记下来比较好. 关于File类 Ø File类直接继承与Object类,File类描述了文件本身的一些属性,File类用来获取或者处 ...
- MySQL数据类型-整型
MySQL支持SQL标准整数类型integer(或INT)和SMALLINT.作为标准的扩展,MySQL还支持整数类型TINYINT.MEDIUMINT和BIGINT. 类型 所占字节 有符号最小 ...
- 自定义Aspect风格的AOP框架
本篇博客参考<架构探险--从零开始写java web框架>4.3章节 1代理接口: package smart.myaop.framework; public interface Prox ...
- python学习第二十二天文件byte类型
所有的文件在计算机里面存储为二进制形式,但是我们有时候有需要将二进制转换为gbk或者utf-8形式,编码的时候encode 解码的时候decode ,下面简单阐述python二进制在文件传输过程的作用 ...
- 为什么需要在 React 类组件中为事件处理程序绑定this?
https://juejin.im/post/5afa6e2f6fb9a07aa2137f51 事件绑定作为回调函数参数传递给函数,丢失其上下文,执行的是默认绑定,不是隐式绑定 类声明和类表达式的主体 ...
- 2019-9-2-win10-uwp-标题栏
title author date CreateTime categories win10 uwp 标题栏 lindexi 2019-09-02 12:57:38 +0800 2018-2-13 17 ...
- 2018-2-13-win10-uwp-BadgeLogo-颜色
title author date CreateTime categories win10 uwp BadgeLogo 颜色 lindexi 2018-2-13 17:23:3 +0800 2018- ...