[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=4397

[算法]

树状数组

时间复杂度 : O(QlogN)

[代码]

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define MAXN 100010
  4.  
  5. int n , q;
  6.  
  7. struct Binary_Indexed_Tree
  8. {
  9. int c[MAXN];
  10. inline int lowbit(int x)
  11. {
  12. return x & (-x);
  13. }
  14. inline void modify(int x , int val)
  15. {
  16. for (int i = x; i <= n; i += lowbit(i)) c[i] += val;
  17. }
  18. inline int query(int x)
  19. {
  20. int ret = ;
  21. for (int i = x; i; i -= lowbit(i)) ret += c[i];
  22. return ret;
  23. }
  24. inline int query(int l , int r)
  25. {
  26. return query(r) - query(l - );
  27. }
  28. } bit1 , bit2 , bit3;
  29.  
  30. template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
  31. template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
  32. template <typename T> inline void read(T &x)
  33. {
  34. T f = ; x = ;
  35. char c = getchar();
  36. for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
  37. for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
  38. x *= f;
  39. }
  40. int main()
  41. {
  42.  
  43. read(n); read(q);
  44. for (int i = ; i <= n; i++)
  45. {
  46. int x;
  47. read(x);
  48. if (x == ) bit1.modify(i , );
  49. else if (x == ) bit2.modify(i , );
  50. else bit3.modify(i , );
  51. }
  52. while (q--)
  53. {
  54. int l , r;
  55. read(l); read(r);
  56. printf("%d %d %d\n",bit1.query(l , r) , bit2.query(l , r) , bit3.query(l , r));
  57. }
  58.  
  59. return ;
  60.  
  61. }

[Usaco2015DEC] Breed Counting的更多相关文章

  1. bzoj 4397: [Usaco2015 dec]Breed Counting -- 前缀和

    4397: [Usaco2015 dec]Breed Counting Time Limit: 10 Sec  Memory Limit: 128 MB Description Farmer John ...

  2. bzoj4397【Usaco2015 Dec】Breed Counting

    4397: [Usaco2015 dec]Breed Counting Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 29  Solved: 25 ...

  3. bzoj4397[Usaco2015 dec]Breed Counting*

    bzoj4397[Usaco2015 dec]Breed Counting 题意: 给定一个长度为N的序列,每个位置上的数只可能是1,2,3中的一种.有Q次询问,每次给定两个数a,b,请分别输出区间[ ...

  4. 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 ...

  5. [Usaco2015 dec]Breed Counting

    原题链接https://www.lydsy.com/JudgeOnline/problem.php?id=4397 用线段树维护区间和即可.时间复杂度\(O((N+Q)logN)\). #includ ...

  6. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  7. BZOJ 刷题总结(持续更新)

    本篇博客按照题号排序(带*为推荐题目) 1008 [HNOI2008]越狱 很经典的题了..龟速乘,龟速幂裸题,, 1010 [HNOI2008]玩具装箱toy* 斜率优化 基本算是裸题. 1012 ...

  8. 萌新笔记——Cardinality Estimation算法学习(二)(Linear Counting算法、最大似然估计(MLE))

    在上篇,我了解了基数的基本概念,现在进入Linear Counting算法的学习. 理解颇浅,还请大神指点! http://blog.codinglabs.org/articles/algorithm ...

  9. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

随机推荐

  1. [luoguP1866]滑动窗口(单调队列)

    传送门 可以搞2个单调队列. 然后,然后就没有然后了. # include <iostream> # include <cstdio> # include <cstrin ...

  2. POJ3321Apple Tree【dfs 树状数组】

    题目大意:一棵树(不一定是二叉树!!),树的节点上本来都有一个苹果,要求完成以下操作: 1.指定某个节点,如果这个节点原本有苹果则拿去,如果没有苹果则填上一个苹果 2.询问某个节点以及其子树一共有多少 ...

  3. Attribute "not-null" must be declared for element type "property"解决办法

    Attribute "not-null" must be declared for element type "property"解决办法 在hiberante ...

  4. java web 项目常用框架

    java框架实在是太多了,网上一搜索一大箩筐,根本就了解不到什么. 我还是以我的经验来说一下j2ee的框架. 1.首先力推struts2框架,这是最经典的框架(可以说没有“之一”).可以帮你快速搭建出 ...

  5. 潜伏者(codevs 1171)

    题目描述 Description [问题描述]R 国和S 国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动.历尽艰险后,潜伏于 S 国的R 国间谍小C 终于摸清了S 国军用密码的编码规则:1. ...

  6. [NOIP2003] 提高组 洛谷P1041 传染病控制

    题目背景 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国大范围流行,该国政府决定不惜一切代价控制传染病的蔓延.不幸的是,由于人们尚未完全认识这种传染病,难以准确判别病毒携带 ...

  7. (二)Commonjs规范与模块化

    在之前的学习中我们使用require()来引入我们需要的包,这其实就是模块化,各模块相互独立,可以通过某种方式引入别的模块.而这些引入方式都是遵循一定的规范的,这就是CommonJS规范. 一.Com ...

  8. 魔咒词典--hdu1880(字符串 暴力)

    http://acm.hdu.edu.cn/showproblem.php?pid=1880 不要想其他的   暴力就能过 #include <iostream> #include < ...

  9. Lca 之倍增算法

    引入: 比如说要找树上任意两个点的路上的最大值.如果是一般的做法 会 接近o(n)的搜,从一个点搜到另一个点,但是如果询问多了复杂度就很高了. 然后我们会预处理.预处理是o(n²)的,询问是o(1)的 ...

  10. 寒武纪camp Day2

    补题进度:8/10 A(计数+BIT) 题意: 给一个长度为n的数组a[],任意选0<=i<=j<n,将a[i]~a[j]从小到大排序,形成新的数组.问有多少个不同的新数组. N,a ...