zoukankan      html  css  js  c++  java
  • [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;
        
    }
  • 相关阅读:
    14-补充内容:MySQl创建用户和授权
    15-可视化工具Navicat的使用
    11-数据的增删改
    12-单表查询
    09-完整性约束
    10-外键的变种 三种关系
    07-数据类型
    08-数据类型(2)
    Mysql 基本语法
    E. K-periodic Garland
  • 原文地址:https://www.cnblogs.com/evenbao/p/9846636.html
Copyright © 2011-2022 走看看