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$的字符串,表示给出的序列. 输出格式 一行一个整数,表示革命的区间的数量. 样例 ...
随机推荐
- P1058立体图(面对代码解释)
传送 样例: 输出样例: (洛谷上面的那个太丑了就不放了) 大佬博客在此 乍一看好像真的没有什么思路 所以我们结合ybr大佬的代码进行分析 疑点都将在代码下面进行分析(面对代码做题模式开始) #inc ...
- spring4.1.8扩展实战之四:感知spring容器变化(SmartLifecycle接口)
本章是<spring4.1.8扩展实战>的第四篇,如果业务上需要在spring容器启动和关闭的时候做一些操作,可以自定义SmartLifecycle接口的实现类来扩展,本章我们通过先分析再 ...
- spring boot创建多模块聚合工程
环境:java1.8,idea 聚合工程优势: 1.统一maven操作.可以在一个maven工程管理多个子工程(每个子工程可单独打包,重启,调试.也可通过聚合工程一起管理). 2.统一管理依赖版本.可 ...
- 大数据学习笔记之Zookeeper(四):Zookeeper实战篇(二)
文章目录 4.1 分布式安装部署 4.2 客户端命令行操作 4.3 API应用 4.3.1 eclipse环境搭建 4.3.2 创建ZooKeeper客户端: 4.3.3 创建子节点 4.3.4 获取 ...
- poj1742Coins(多重背包)
People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony ...
- SEC1- 数据库的相关概念
一.数据库的好处1. 可以持久化数据到本地2. 结构化查询 二.数据库的常见概念1. DB:data base数据库,存储数据的容器2. DBMS:database management sysy ...
- Hibernate入门1
Hibernate概述: 1. 什么是框架: 写程序,在使用框架之后,帮我们实现一部分的功能,使用框架的好处可以少写一部分代码实现功能 2. 什么是hibernate框架: hibernate框架应用 ...
- mybatis入门总结一
1.parameterType 表示输入参数的类型 2.resultType 表示输出结果的类型 不管输出的是一条还是多条,都只代表单条记录所映射的java对象类 3.#{} 表示sql语句中的占位 ...
- 工厂模式vs简单工厂
前言 工厂方法模式(Factory Method),定义一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法使一个类的实例化延迟到其子类. 简单工厂模式的最大优点在于工厂类中包含了必要的逻辑判断 ...
- oracle用户权限管理
oralce对权限管理比较严谨,普通用户之间也是默认不能互相访问的,需要互相授权 1.查看当前数据库所有用户: select * from all_users; 2.查看表所支持的权限: select ...