Incorrect update elements in Fenwick Tree

67 Views Asked by At

I wrote a program, which give an sum of range by getting command 's' and update an element by command 'u', but it works incorrect. Help me please.

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

template <typename indexT, typename valueT> class FenwickTree{
public:
                FenwickTree(vector<valueT> &buffer){
                                tree.resize(buffer.size());
                                for (indexT i = 0; i < buffer.size(); ++i)
                                                update(i, buffer[i]);
                }
                valueT getSolution(const indexT l, const indexT r) const{
                                return getSolution(r) - getSolution(l - 1);
                }
                void update(indexT position, const valueT v){
                                valueT delta = v - tree[position];
                                for (indexT i = position; i < tree.size(); i = i | (i + 1))
                                                tree[i] += delta;
                }
private:
                vector <valueT> tree;
                valueT getSolution(const indexT r) const{
                                valueT result = 0;
                                for (indexT i = r; i >= 0; i = (i & (i + 1)) - 1)
                                                result = tree[i] + result;
                                return result;
                }
};

int main(){
                ifstream file;
                file.open("D:\\input.txt", ios::in);
                unsigned long long n, k;
                file >> n;
                vector<long long> buffer(n);
                for (long long i = 0; i < n; ++i)
                                file >> buffer[i];
                file >> k;
                FenwickTree<long long, long long> fenwickTree(buffer);
                for (long long i = 0; i < k; ++i){
                                char command;
                                long long a, b;
                                file >> command >> a >> b;
                                if (command == 's')
                                                cout << fenwickTree.getSolution(a - 1, b - 1) << " ";
                                else if (command == 'u')
                                                fenwickTree.update(a - 1, b);
                }
                file.close();
                return 0;
}

Correct work:

input: 
10
613 263 312 670 216 142 976 355 488 370
10
s 2 7
s 4 8
u 7 969
u 1 558
s 2 7
u 2 731
s 4 9
s 1 3
u 8 76
u 5 377

output:

2579 2359 2572 2840 1601 

I calculate a delta between new old and new value and then update fenwick tree, but it doesn't work for me.

1

There are 1 best solutions below

0
On BEST ANSWER

Fixed it:

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

template <typename indexT, typename valueT> class FenwickTree{
public:
                FenwickTree(vector<valueT> &buffer){
                                tree.resize(buffer.size());
                                for (indexT i = 0; i < buffer.size(); ++i)
                                                update(i, buffer[i]);
                }
                valueT getSolution(const indexT l, const indexT r) const{
                                return getSolution(r) - getSolution(l - 1);
                }
                void update(indexT position, const valueT v){
                                for (indexT i = position; i < tree.size(); i = i | (i + 1))
                                                tree[i] += v;
                }
private:
                vector <valueT> tree;
                valueT getSolution(const indexT r) const{
                                valueT result = 0;
                                for (indexT i = r; i >= 0; i = (i & (i + 1)) - 1)
                                                result += tree[i];
                                return result;
                }
};

int main(){
                ifstream file;
                file.open("D:\\input.txt", ios::in);
                unsigned long long n, k;
                file >> n;
                vector<long long> buffer(n);
                for (long long i = 0; i < n; ++i)
                                file >> buffer[i];
                file >> k;
                FenwickTree<long long, long long> fenwickTree(buffer);
                for (long long i = 0; i < k; ++i){
                                char command;
                                long long a, b;
                                file >> command >> a >> b;
                                if (command == 's')
                                                cout << fenwickTree.getSolution(a - 1, b - 1) << " ";
                                else if (command == 'u'){
                                                fenwickTree.update(a - 1, b - buffer[a - 1]);
                                                buffer[a - 1] = b;
                                }
                }
                file.close();
                int lal = 0;
                cin >> lal;
                return 0;
}