[Usaco2015DEC] Breed Counting

Posted evenbao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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 = 0;
                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 - 1);
        }
} 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 = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == -) f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - 0;
    x *= f;
}
int main()
{
        
        read(n); read(q);
        for (int i = 1; i <= n; i++)
        {
                int x;
                read(x);
                if (x == 1) bit1.modify(i , 1);
                else if (x == 2) bit2.modify(i , 1);
                else bit3.modify(i , 1); 
        } 
        while (q--)
        {
                int l , r;
                read(l); read(r);
                printf("%d %d %d
",bit1.query(l , r) , bit2.query(l , r) , bit3.query(l , r));        
        }
        
        return 0;
    
}

 

以上是关于[Usaco2015DEC] Breed Counting的主要内容,如果未能解决你的问题,请参考以下文章

[bzoj4397] [Usaco2015 dec]Breed Counting

bzoj4390: [Usaco2015 dec]Max Flow

[Usaco2015 dec]Max Flow

bzoj4393[Usaco2015 Dec]Fruit Feast*

bzoj4396[Usaco2015 dec]High Card Wins*

bzoj4390[Usaco2015 dec]Max Flow*