[题目链接]

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的更多相关文章

  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. Linux CentOS命令行界面字体重复问题解决记录

    问题描述: 安装完CentOS 6.5 mini版之后,安装图形界面,启动之后出现如下问题,字体有重复 应该是因为字体原因, 我的解决方法: yum -y install dejavu-sans-* ...

  2. squid重定向(python 代码)

    #!/usr/bin/python2.7 # coding: utf-8 import sys,os,stat import transport def redirect_url(line,concu ...

  3. hihoCoder#1086 Browser Caching

    原题地址 list+map可以轻松搞定,如果不借助STL实现起来还是挺麻烦的 代码: #include <iostream> #include <string> #includ ...

  4. 【搜索】codeforces C. The Tag Game

    http://codeforces.com/contest/813/problem/C [题意] 给定一棵有n个结点的树,初始时Alice在根结点1,Bob在非根结点x; Alice和Bob轮流走,每 ...

  5. github新建本地仓库,再同步远程仓库基本用法

    github新建本地仓库,再同步远程仓库基本用法 1 mkdir gitRepo 2 cd gitRepo 3 git init  #初始化本地仓库 4 git add xxx  #添加要push到远 ...

  6. Apache 文件根目录设置修改方法 (Document Root)

    最近在学习WordPress,使用appServ 在windows上搭建Php开发环境 在网上查找到的关于修改Apache服务器根目录的资料,对比学习,再此记录 在安装 Apache 时,系统会给定一 ...

  7. CodeForces 592A PawnChess

    简单暴力模拟. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm&g ...

  8. React学习之State

    本文基于React v16.4.1 初学react,有理解不对的地方,欢迎批评指正^_^ 一.定义组件的两种方式 1.函数定义组件 function Welcome(props) { return & ...

  9. Maven+mybatis教程

    首先,配置maven 在eclipse中把maven路径和settings.xml文件配置好之后,否则后续会有一些问题 可以设一个环境变量M2_HOME指向你的maven安装目录 M2_HOME=G: ...

  10. Linux进程IPC浅析[进程间通信SystemV共享内存]

    Linux进程IPC浅析[进程间通信SystemV共享内存] 共享内存概念,概述 共享内存的相关函数 共享内存概念,概述: 共享内存区域是被多个进程共享的一部分物理内存 多个进程都可把该共享内存映射到 ...