[Usaco2015DEC] Breed Counting
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=4397
[算法]
树状数组
时间复杂度 : O(QlogN)
[代码]
- #include<bits/stdc++.h>
- using namespace std;
- #define MAXN 100010
- int n , q;
- struct Binary_Indexed_Tree
- {
- int c[MAXN];
- inline int lowbit(int x)
- {
- return x & (-x);
- }
- inline void modify(int x , int val)
- {
- for (int i = x; i <= n; i += lowbit(i)) c[i] += val;
- }
- inline int query(int x)
- {
- int ret = ;
- for (int i = x; i; i -= lowbit(i)) ret += c[i];
- return ret;
- }
- inline int query(int l , int r)
- {
- return query(r) - query(l - );
- }
- } bit1 , bit2 , bit3;
- template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
- template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
- template <typename T> inline void read(T &x)
- {
- T f = ; x = ;
- char c = getchar();
- for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
- for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
- x *= f;
- }
- int main()
- {
- read(n); read(q);
- for (int i = ; i <= n; i++)
- {
- int x;
- read(x);
- if (x == ) bit1.modify(i , );
- else if (x == ) bit2.modify(i , );
- else bit3.modify(i , );
- }
- while (q--)
- {
- int l , r;
- read(l); read(r);
- printf("%d %d %d\n",bit1.query(l , r) , bit2.query(l , r) , bit3.query(l , r));
- }
- return ;
- }
[Usaco2015DEC] Breed Counting的更多相关文章
- bzoj 4397: [Usaco2015 dec]Breed Counting -- 前缀和
4397: [Usaco2015 dec]Breed Counting Time Limit: 10 Sec Memory Limit: 128 MB Description Farmer John ...
- bzoj4397【Usaco2015 Dec】Breed Counting
4397: [Usaco2015 dec]Breed Counting Time Limit: 10 Sec Memory Limit: 128 MB Submit: 29 Solved: 25 ...
- bzoj4397[Usaco2015 dec]Breed Counting*
bzoj4397[Usaco2015 dec]Breed Counting 题意: 给定一个长度为N的序列,每个位置上的数只可能是1,2,3中的一种.有Q次询问,每次给定两个数a,b,请分别输出区间[ ...
- bzoj4397【Usaco2015 Dec】Breed Counting(前缀和、树状数组)
题目描述 Farmer John's N cows, conveniently numbered 1…N, are all standing in a row (they seem to do so ...
- [Usaco2015 dec]Breed Counting
原题链接https://www.lydsy.com/JudgeOnline/problem.php?id=4397 用线段树维护区间和即可.时间复杂度\(O((N+Q)logN)\). #includ ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- BZOJ 刷题总结(持续更新)
本篇博客按照题号排序(带*为推荐题目) 1008 [HNOI2008]越狱 很经典的题了..龟速乘,龟速幂裸题,, 1010 [HNOI2008]玩具装箱toy* 斜率优化 基本算是裸题. 1012 ...
- 萌新笔记——Cardinality Estimation算法学习(二)(Linear Counting算法、最大似然估计(MLE))
在上篇,我了解了基数的基本概念,现在进入Linear Counting算法的学习. 理解颇浅,还请大神指点! http://blog.codinglabs.org/articles/algorithm ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
随机推荐
- [luoguP1866]滑动窗口(单调队列)
传送门 可以搞2个单调队列. 然后,然后就没有然后了. # include <iostream> # include <cstdio> # include <cstrin ...
- POJ3321Apple Tree【dfs 树状数组】
题目大意:一棵树(不一定是二叉树!!),树的节点上本来都有一个苹果,要求完成以下操作: 1.指定某个节点,如果这个节点原本有苹果则拿去,如果没有苹果则填上一个苹果 2.询问某个节点以及其子树一共有多少 ...
- Attribute "not-null" must be declared for element type "property"解决办法
Attribute "not-null" must be declared for element type "property"解决办法 在hiberante ...
- java web 项目常用框架
java框架实在是太多了,网上一搜索一大箩筐,根本就了解不到什么. 我还是以我的经验来说一下j2ee的框架. 1.首先力推struts2框架,这是最经典的框架(可以说没有“之一”).可以帮你快速搭建出 ...
- 潜伏者(codevs 1171)
题目描述 Description [问题描述]R 国和S 国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动.历尽艰险后,潜伏于 S 国的R 国间谍小C 终于摸清了S 国军用密码的编码规则:1. ...
- [NOIP2003] 提高组 洛谷P1041 传染病控制
题目背景 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国大范围流行,该国政府决定不惜一切代价控制传染病的蔓延.不幸的是,由于人们尚未完全认识这种传染病,难以准确判别病毒携带 ...
- (二)Commonjs规范与模块化
在之前的学习中我们使用require()来引入我们需要的包,这其实就是模块化,各模块相互独立,可以通过某种方式引入别的模块.而这些引入方式都是遵循一定的规范的,这就是CommonJS规范. 一.Com ...
- 魔咒词典--hdu1880(字符串 暴力)
http://acm.hdu.edu.cn/showproblem.php?pid=1880 不要想其他的 暴力就能过 #include <iostream> #include < ...
- Lca 之倍增算法
引入: 比如说要找树上任意两个点的路上的最大值.如果是一般的做法 会 接近o(n)的搜,从一个点搜到另一个点,但是如果询问多了复杂度就很高了. 然后我们会预处理.预处理是o(n²)的,询问是o(1)的 ...
- 寒武纪camp Day2
补题进度:8/10 A(计数+BIT) 题意: 给一个长度为n的数组a[],任意选0<=i<=j<n,将a[i]~a[j]从小到大排序,形成新的数组.问有多少个不同的新数组. N,a ...