码迷,mamicode.com
首页 > 其他好文 > 详细

[Usaco2015DEC] Breed Counting

时间:2018-10-25 00:09:25      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:com   ++   efi   getch   code   http   return   turn   \n   

[题目链接]

        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\n",bit1.query(l , r) , bit2.query(l , r) , bit3.query(l , r));        
        }
        
        return 0;
    
}

 

[Usaco2015DEC] Breed Counting

标签:com   ++   efi   getch   code   http   return   turn   \n   

原文地址:https://www.cnblogs.com/evenbao/p/9846636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!