Need a minor help with my code. C++, BIT


  • 0
    S

    This is my code for this problem. It fails on some test cases when submitting it, but when I try those test cases as custom test cases it's giving correct output. I can't seem to understand what might be the problem. Can someone please help me figure out what's wrong?

        int* Bit;
        int size__=0;
        vector<int> a;
    public:
        NumArray(vector<int> nums) {
            size__=nums.size();
            a=vector<int>(size__,0);
            Bit= (int*)malloc((size__+1)*sizeof(int));
            for(int i=0;i<size__;++i)
                update(i,nums[i]),a[i]=nums[i];
        }
        void update(int i, int val) {
            val-=a[i];
            a[i]+=val;
            ++i;
            while(i<=size__){
                Bit[i]+=val;
                i+=i&(-i);
            }
        }
        int sumR(int j){
            int Ans=0;
            while(j>0){
                Ans+=Bit[j];
                j-=(j&-j);
            }
            return Ans;
        }
        
        int sumRange(int i, int j) {
              if(j<i)
                return 0;
            if(i!=0)
                return sumR(j+1)-sumR(i);
            else
                return sumR(j+1);
        }
    };
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.