[模板] 樹狀數組 Binary indexed tree

  之前寫了一個BIT模板失敗了,今天練習逆序數對時心血來潮再試一次成功了XD,不過可能是還有潛在的錯誤只是我沒發現罷了@@ (取名叫sum好像怪怪的......)

  I used to fail to make a BIT template; however, I tred it again on a whim when practicing Inversion count problem and succeeded this time. Maybe there are still some mistake not found@@
(the name "sum" seems to be weird......)

class BIT{
    private:
        vector<int> BITT;
        int dis;
    public:
        inline void init(int n)
        {
            BITT.clear();
            BITT.resize(n+1,0);
            dis = n;
        }
        inline void update(int i,int x)
        {
            while(i <= dis)
            {
                BITT[i] += x;
                i += i&-i;
            }
        }
        inline int sum(int i)
        {
            int res = 0;
            while(i > 0)
            {
                res += BITT[i];
                i -= i&-i;
            }
            return res;
        }
};

留言

這個網誌中的熱門文章

Shellshock.io從入門到上手(針對單狙)(沒有圖片、影片版本)

[TIOJ] 1007燈泡問題